Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Render Word Documents to HTML using a REST API in C#
string MyClientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
string MyClientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
string MyStorage = "";
var configuration = new Configuration(MyClientId, MyClientSecret);
// initialize API
var fileApi = new FileApi(configuration);
// save all rendered HTML pages
foreach (var page in response.Pages)
{
// create download file request
var downloadFileRequest = new DownloadFileRequest(page.Path, MyStorage);
// download file
var file = fileApi.DownloadFile(downloadFileRequest);
// save file in working directory
using (var fileStream = System.IO.File.Create("C:\\Files\\" + page.Path))
{
file.Seek(0, SeekOrigin.Begin);
file.CopyTo(fileStream);
}
}
// initialize API
var apiInstance = new ViewApi(configuration);
// input file path
var fileInfo = new GroupDocs.Viewer.Cloud.Sdk.Model.FileInfo();
fileInfo.FilePath = "sample.docx";
// define view options
var viewOptions = new ViewOptions();
viewOptions.FileInfo = fileInfo;
viewOptions.ViewFormat = ViewOptions.ViewFormatEnum.HTML; // output view format
// create view request
var request = new CreateViewRequest(viewOptions);
// create view
var response = apiInstance.CreateView(request);
// initialize API
var apiInstance = new ViewApi(configuration);
// input file path
var fileInfo = new GroupDocs.Viewer.Cloud.Sdk.Model.FileInfo();
fileInfo.FilePath = "sample.docx";
// define view options
var viewOptions = new ViewOptions();
viewOptions.FileInfo = fileInfo;
viewOptions.ViewFormat = ViewOptions.ViewFormatEnum.HTML; // output view format
// define render options
viewOptions.RenderOptions = new RenderOptions
{
// render comments
RenderComments = true,
// render specific range of pages
StartPageNumber = 1,
CountPagesToRender = 2,
};
// create view request
var request = new CreateViewRequest(viewOptions);
// create view
var response = apiInstance.CreateView(request);
// initialize API
var apiInstance = new ViewApi(configuration);
// input file path
var fileInfo = new GroupDocs.Viewer.Cloud.Sdk.Model.FileInfo();
fileInfo.FilePath = "sample.docx";
// define view options
var viewOptions = new ViewOptions();
viewOptions.FileInfo = fileInfo;
viewOptions.ViewFormat = ViewOptions.ViewFormatEnum.HTML; // output view format
// define watermark
viewOptions.Watermark = new Watermark
{
Text = "This is a sample watermark!",
Size = 100,
Color = "Red",
Position = Watermark.PositionEnum.Diagonal
};
// create view request
var request = new CreateViewRequest(viewOptions);
// create view
var response = apiInstance.CreateView(request);
// initialize API
var apiInstance = new FileApi(configuration);
// Open file in IOStream from local/disc.
var fileStream = File.Open(@"C:\Files\Sample.docx", FileMode.Open);
// create file upload request
var request = new UploadFileRequest("sample.docx", fileStream, MyStorage);
// upload file
var response = apiInstance.UploadFile(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment