Use Word 2007 to programmatically convert an InfoPath 2007 form to PDF

Use the Word 12 (= Word 2007) object model and the 2007 Microsoft Office Add-in: Microsoft Save as PDF to programmatically export an InfoPath 2007 form to PDF.

ADVERTISEMENTS

Problem

You want to programmatically convert an InfoPath 2007 form to PDF or automate the conversion of InfoPath 2007 forms to PDF documents.

Solution

Use the Word 12 (= Word 2007) object model and the 2007 Microsoft Office Add-in: Microsoft Save as PDF to programmatically export an InfoPath 2007 form to PDF.

While Save / print / convert / export an InfoPath form to PDF is a solution limited to converting InfoPath forms to PDF using the InfoPath 2007 client application, the solution described in this article can be modified and used to convert InfoPath browser-enabled forms to PDF using Word 2007 and the Office Save as PDF add-in.

Note: If you want to convert InfoPath 2010 forms to PDF through Word in SharePoint 2010, you can do so by first converting an InfoPath form to a Word document, and then using Word Automation Services in SharePoint to convert the Word document into a PDF document.

Discussion

In Convert an InfoPath form into a Word 2007 document using XSLT and C# I explained how you can convert an InfoPath 2007 form into a Word 2007 document. You can combine that solution with the solution described in Saving Word 2007 Documents to PDF and XPS Formats to export the Word 2007 document that was the result of the InfoPath to Word conversion into a PDF document.

The final code would look something like the following (refer to the two aforementioned articles for background information):

// Create a temporary file name to use
string fileName = Guid.NewGuid().ToString();

// Define variables for the word template to use and word and pdf files to create
string wordTemplateFilePath = @"C:\InfoPath\PrintToPDFviaWord\template.docx";
string wordPrintFilePath = String.Concat(@"C:\", fileName, ".docx");
string pdfFilePath = String.Concat(@"C:\", fileName, ".pdf");

// Copy the template to create a new docx file
File.Copy(wordTemplateFilePath, wordPrintFilePath);

// Crack open the package
Package packWordPrint = Package.Open(wordPrintFilePath, FileMode.Open, FileAccess.ReadWrite);

// Retrieve the document.xml part of the new docx file
PackagePart part = packWordPrint.GetPart(new Uri("/word/document.xml", UriKind.Relative));

// Retrieve the xsl to use to transform the InfoPath form into document.xml
DataSource ds = this.DataSources["transform"];
XslCompiledTransform trans = new XslCompiledTransform();
trans.Load(ds.CreateNavigator());

// Create a StreamWriter to be able to write to the stream of the part
using (StreamWriter partStream = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write)))
{
// Transform the InfoPath form and save the XML into the stream for the part
trans.Transform(this.MainDataSource.CreateNavigator(), null, partStream);

// Close the stream of the part
partStream.Close();
}

// Write changes to the package
packWordPrint.Flush();

// Close the package
packWordPrint.Close();

// Instantiate word application and document objects
ApplicationClass wordApp = new ApplicationClass();
Document word = null;

// Define document location paths to use
object paramSourceDocPath = wordPrintFilePath;
string paramExportFilePath = pdfFilePath;

// Define variables for parameters
object paramMissing = Type.Missing;
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;

try
{
// Open the word document
word = wordApp.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);

if (word != null)
{
// Export the word document to pdf
word.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM,
paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts,
paramUseISO19005_1, ref paramMissing);
}
}
catch (Exception ex)
{
// Respond to the error
}
finally
{
if (word != null)
{
word.Close(ref paramMissing, ref paramMissing, ref paramMissing);
word = null;
}

if (wordApp != null)
{
wordApp.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApp = null;
}
}

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

// Open the PDF file in Adobe Acrobat
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "AcroRd32";
proc.StartInfo.Arguments = pdfFilePath;
proc.Start();

// Delete the temporary word document
if (File.Exists(wordPrintFilePath))
{
File.Delete(wordPrintFilePath);
}

Namespaces imported:

using System.IO;
using System.IO.Packaging;
using System.Xml.Xsl;
using System.Diagnostics;
using Microsoft.Office.Interop.Word;

 
 Subscribe for updates via RSS or Email

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services