Discussion

1
Replies
1932
Views
sasnt Member since 2016 9 posts
PEGA
Posted: 3 years ago
Last activity: 3 years 5 months ago

Convert a Word Document into a PDF Using a Script

Below is a script you may use to convert any file that can be opened via Microsoft Word into a PDF. I have tested it with a txt file as well as an actual Word document, however other types may work as well. It requires a reference be added to the script component. You must add a reference to Microsoft.Office.Interop.Word. The script accepts a path to your file and creates a PDF file of the same name in the same location (it will return this value). The script code is below, however it is also attached as a text file.

Name: SaveAsPDF
Parameters: string fileName
Result type: string

Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDocument = appWord.Documents.Open(fileName);
string saveFileName = Path.GetDirectoryName(fileName);
saveFileName = Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName) + ".pdf");
wordDocument.ExportAsFixedFormat(saveFileName, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
appWord.Documents.Close();
appWord.Quit();
return saveFileName;

***Updated by Moderator: Marissa to change to a Discussion post & add FAQ group tag***

Robotic Process Automation Developer Knowledge Share
Share this page LinkedIn