Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Gist contains code snippets related to Converting Word document to PDF format using C#
// get client credentials from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// create Configuration instnace by passing Client ID and Client secret details
var config = new Configuration { ClientId = clientID, ClientSecret = clientSecret };
// create WordsApi object
var wordsApi = new WordsApi(config);
// input docx file
string fileName = "file-sample_1MB.docx";
// output format as PDF
string format = "pdf";
// resultant file name
string outputfile = "result.pdf";
// load DOCX file content from local drive
using (var file = System.IO.File.OpenRead("/Users/nshahbaz/Downloads/" + fileName))
{
// Upload original document to Cloud Storage
wordsApi.UploadFile(new UploadFileRequest(file, fileName, null));
}
// upload the file to Cloud Storage
try
{
GetDocumentWithFormatRequest request = new GetDocumentWithFormatRequest(fileName,format,null,null,null,null,outputfile);
wordsApi.GetDocumentWithFormat(request);
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
This Gist contains code snippets related to Converting Word document to PDF format using C#
// get client credentials from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// create Configuration instnace by passing Client ID and Client secret details
var config = new Configuration { ClientId = clientID, ClientSecret = clientSecret };
// create WordsApi object
var wordsApi = new WordsApi(config);
// input word file
string fileName = "test_multi_pages.doc";
// name of resultant file
string outputfile = "result.pdf";
try
{
// upload word file from local storage and specify output format
ConvertDocumentRequest request = new ConvertDocumentRequest(System.IO.File.OpenRead("/Users/nshahbaz/Downloads/" + fileName),"pdf",outputfile);
// perform conversion operation
var output = wordsApi.ConvertDocument(request);
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
// get client credentials from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// create Configuration instnace by passing Client ID and Client secret details
var config = new Configuration { ClientId = clientID, ClientSecret = clientSecret };
// create WordsApi object
var wordsApi = new WordsApi(config);
// input Word document
string fileName = "test_multi_pages.doc";
// load file from local system
using (var file = System.IO.File.OpenRead("/Users/nshahbaz/Downloads/" + fileName))
{
// Upload original document to Cloud Storage
wordsApi.UploadFile(new UploadFileRequest(file, fileName, null));
}
// create SaveOptionsData object
SaveOptionsData saveOptionsData = new SaveOptionsData()
{
SaveFormat = "pdf",
FileName = "myResultant.pdf",
AllowEmbeddingPostScriptFonts = true,
ZipOutput = false,
UpdateLastSavedTimeProperty = true,
UpdateSdtContent = true,
UpdateCreatedTimeProperty = true,
UpdateLastPrintedProperty = true
};
try
{
// create SaveAsRequest by passing input Word file and saveOptionsData
SaveAsRequest request = new SaveAsRequest(fileName, saveOptionsData);
// initialize the Word to PDF conversion operation
var output = wordsApi.SaveAs(request);
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment