MSInfoPath_SaveAsPDF

Microsoft provides add-ins to the Office products to support saving into PDF or XPS file formats. These add-ins include an API to perform this as well.

The add-ins may be found at:
Save as PDF: http://www.microsoft.com/downloads/details.aspx?FamilyId=F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en
Save as XPS: http://www.microsoft.com/downloads/details.aspx?FamilyId=BCE8F991-F0A4-47A0-866B-2FD84A329E02&displaylang=en
Save as PDF or XPS (single package): http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en

Once installed, add a reference to the Microsoft.Office.Interop.[product]
Note: multiple product versions are available as references: version 12 = Office 2007, version 11 = Office 2003
Language:
C#
Keywords:
Office
Code Snippet

using System;
using System.IO;
using MSInfoPath = Microsoft.Office.Interop.InfoPath;

/***
* must install: addin for SaveAsPDF for Office 2007
* must add COM references for Microsoft.Office.Interop.InfoPath
***/

public void MakeInfoPathPDF(string SourceDoc, string DestDoc)
{
    //for all missing types.
     object Unknown = Type.Missing;

    MSInfoPath.
Application newInfoPathAppn = new Microsoft.Office.Interop.InfoPath.Application();
    MSInfoPath.
XDocument newInfopath = null;

    object src_doc = SourceDoc;

    // set the behavior to default
    //   Open method: http://msdn.microsoft.com/en-us/library/bb229795.aspx
    //   XdDocumentVersionMode Enumeration: http://msdn.microsoft.com/en-us/library/bb229725.aspx
    int openBehavior = (int)Microsoft.Office.Interop.InfoPath.XdDocumentVersionMode.xdFailOnVersionOlder;

    newInfopath = newInfoPathAppn.XDocuments.Open(src_doc, openBehavior);

    //specify the format.
    //   ExportFormat Enumeration: http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.exportformat.aspx
    //   formats include: "Mht", "Pdf", "Xps"
    string format = "Pdf";

    // Save it in the target format.
    newInfopath.View.Export(DestDoc, format);

    newInfoPathAppn.Quit(
false);
}


Created 2012-02-11
comments powered by Disqus
Login