Discussion

1
Replies
2034
Views
Close popover
Thomas Sasnett (ThomasSasnett)
PEGA
Lead Robotics Architect u2013 Pega Consulting
Pegasystems Inc.
US
ThomasSasnett Member since 2016 10 posts
PEGA
Posted: April 20, 2017
Last activity: January 23, 2018

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