Created
May 26, 2024 13:16
Add Image in Word Document with NET REST API. https://kb.aspose.cloud/words/net/add-image-in-word-document-with-net-rest-api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using Aspose.Words.Cloud.Sdk; | |
using Aspose.Words.Cloud.Sdk.Model; | |
using Aspose.Words.Cloud.Sdk.Model.Requests; | |
namespace WordsSample.Words | |
{ | |
public class WordFileOperations | |
{ | |
public void AddImageInWordFile() | |
{ | |
var wordsApi = new WordsApi("Client ID", "Client Secret"); | |
string output = "Output.docx"; | |
var request = new InsertDrawingObjectOnlineRequest(); | |
request.Document = File.OpenRead("Sample.docx"); | |
request.DestFileName = output; | |
request.DrawingObject = new DrawingObjectInsert() | |
{ | |
RelativeHorizontalPosition = DrawingObjectInsert.RelativeHorizontalPositionEnum.Margin, | |
Left = 0f, | |
RelativeVerticalPosition = DrawingObjectInsert.RelativeVerticalPositionEnum.Margin, | |
Top = 0f, | |
Width = 0f, | |
Height = 0f, | |
WrapType = DrawingObjectInsert.WrapTypeEnum.Inline, | |
}; | |
request.ImageFile = File.OpenRead("image.png"); | |
var task = wordsApi.InsertDrawingObjectOnline(request); | |
task.Wait(); | |
var result = task.Result; | |
if (result.Document.TryGetValue(output, out var stream)) | |
{ | |
stream.Position = 0; | |
using (var fileStream = File.Create(output)) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment