Skip to content

Instantly share code, notes, and snippets.

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