Automated conversion of InfoPath forms to PDF

青春壹個敷衍的年華 提交于 2019-12-11 02:29:20

问题


I've been tasked with programatically opening up a set of XML files (~10k) associated with various InfoPath templates (20) and then saving the populated forms as PDF to a network share. This will be a one-time task.

I have code to cycle through the set of XML files and open them in InfoPath. However, the documented command-line parameters are not sufficient for my needs (no print / close parameters).

Can anyone provide any suggestions for startup parameters that will tell InfoPath to open a specific file, print it to PDF, and close upon printing? Is there an alternative method to achieve this goal?

We're using InfoPath 2007 and the files are hosted in MOSS 2007.


回答1:


Using the COM interop assembly for InfoPath (add a reference to Microsoft.Office.Interop.InfoPath) you can print an XML form with the following snippet:

using Microsoft.Office.Interop.InfoPath;

class Program
{
    static void Main(string[] args)
    {
        Application app = new Application();
        XDocument xdoc = app.XDocuments.Open(@"C:\tmp\Form1.xml", 1);
        xdoc.PrintOut();
        app.Quit(false);
    }
}

Printing happens using the default printer settings and I haven't found a way to change that. However, as it's a one-time task for you I assume that should not be a problem.

More of a problem could be that you don't have a way to specify the output file name of your PDF printer. Maybe you can configure your printer to use a default name, then wait until the file is printed, move it and then print the next document.




回答2:


And here is @Dirk Vollmar's answer in Powershell, using COM.

$app = New-Object -ComObject InfoPath.Application
$xdoc = $app.XDocuments.Open('C:\path\file.xml', 1)
$xdoc.PrintOut()
Start-Sleep -Seconds 2
$app.Quit($true)

See Microsoft's documentation, for more information about automating InfoPath.



来源:https://stackoverflow.com/questions/2006519/automated-conversion-of-infopath-forms-to-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!