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.
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.
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;
Related InfoPath Articles:
