Discussion
Pegasystems Inc.
US
Last activity: 23 Jan 2018 6:21 EST
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***