Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active August 23, 2021 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/270127e116b65804b0b56cac17196c66 to your computer and use it in GitHub Desktop.
Save aspose-cloud/270127e116b65804b0b56cac17196c66 to your computer and use it in GitHub Desktop.
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