Convert an InfoPath 2007 form into a Word 2007 document using XSLT and C#

Applies to: InfoPath 2007

Extract and use the document.xml part of a docx file to create an XSLT stylesheet to convert the XML of InfoPath forms into docx files.

ADVERTISEMENTS

Problem

You have an InfoPath form, which you would like to convert into a Word 2007 docx file.

Solution

Create a docx template file, extract the document.xml part from the docx file, and use this to create an XSLT stylesheet to convert the XML of the InfoPath form into the document.xml part of a new docx file.

Discussion

You can accomplish this functionality as follows:

  1. Open Microsoft Visual Studio 2005 and create a new InfoPath C# project.
  2. Design an InfoPath form as shown in figure 1.

    Figure 1. InfoPath form in design mode.
  3. The Main data source of the InfoPath form should resemble the following figure.

    Figure 2. Main data source of InfoPath form.
  4. Create a docx file as shown in figure 3 and save it as template.docx. This Word 2007 document will be used as a template file to base your converted InfoPath forms on.

    Figure 3. The Word 2007 document to use as a template for converting InfoPath forms.
    The text ip_firstName, ip_lastName, ip_website, and table_cell_1 are used as placeholders just to make it easier later on to locate the positions in the XSLT stylesheet where to place references to the InfoPath form fields. Note that table_cell_1 is contained within a table. This table will be used to show the items from the Repeating Table on the InfoPath form.
  5. Open Windows Explorer and browse to the template.docx file.
  6. Copy template.docx, paste it, and rename it to template.zip.
  7. Extract the files from the template.zip file.

    Figure 4. Contents of template.zip.
  8. Double-click on the word folder to open it.

    Figure 5. Contents of the word folder in template.zip.
  9. Open the document.xml file in Notepad and save it as transform.xsl in the same folder where template.docx is located.
  10. Replace
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    which is located at the beginning of the transform.xsl file with
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" />
    <xsl:template match="/">
  11. Append the following XSL tags to the end of the transform.xsl file:
    </xsl:template>
    </xsl:stylesheet>
  12. Switch to Microsoft Visual Studio 2005, go to the Solution Explorer, open the myschema.xsd file to view its code, copy all of the namespaces defined for the form (except for the default and xsd namespaces), and paste them as attributes of the xsl:stylesheet node in the transform.xsl file. The beginning xsl:stylesheet tag should now look something like:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-12-31T07:11:15"
    xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"
  13. Copy all of the namespaces defined in the w:document node in the transform.xsl file, and paste them as attributes of the xsl:stylesheet node in the transform.xsl file. The beginning xsl:stylesheet tag should now look something like:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-12-31T07:11:15"
    xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
    xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
    xmlns:w10="urn:schemas-microsoft-com:office:word"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
  14. Search for ip_firstName in the transform.xsl file and replace it with
    <xsl:value-of select="my:myFields/my:firstName" />
  15. Search for ip_lastName in the transform.xsl file and replace it with
    <xsl:value-of select="my:myFields/my:lastName" />
  16. Search for ip_website in the transform.xsl file and replace it with
    <xsl:value-of select="my:myFields/my:website" />
  17. Search for table_cell_1 in the transform.xsl file and replace it with
    <xsl:value-of select="my:name" />
  18. Search for the last </w:tblGrid> tag and append the following XSL code
    <xsl:for-each select="my:myFields/my:countries/my:country">
  19. Search for the last </w:tr> tag and append the following XSL code
    </xsl:for-each>
  20. Open the InfoPath form in design mode.
  21. Select the Data > Data Connections... menu item.
  22. Click on the Add... button on the Data Connections dialog box.
  23. Select the Receive data option button and click on the Next > button.
  24. Select the XML document option button and click on the Next > button.
  25. Click on the Browse... button to navigate to and select the XSL file you created in step 20.
    Tip: Select All Files (*.*) in the Files of type drop-down list box to be able to see the XSLT file.
  26. Click on the Next > button after you have selected the file and returned to the Data Connection Wizard dialog box.
  27. Select Include the data as a resource file in the form template or template part and click on the Next > button.
  28. Make sure that the Automatically retrieve data when form is opened checkbox is selected and then click on the Finish button.
  29. Click on the Close button to close the Data Connections dialog box.
  30. Go to the Solution Explorer window, click on the References node, and add a reference to the WindowsBase DLL that comes with the Microsoft .NET Framework 3.0.
  31. Double-click on the button to open its Properties dialog box.
  32. Click on the Edit Form Code... button and add the following code in the Clicked event handler that InfoPath created for you:
    // Define variables for the word template to use and file to create
    string wordTemplateFilePath = @"C:\InfoPath\ConvertToWord2007\template.docx";
    string wordPrintFilePath = @"C:\InfoPath\ConvertToWord2007\wordprint.docx";

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

    // 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();

    // Open the new docx file in Word 2007
    Process proc = new Process();
    proc.EnableRaisingEvents = false;
    proc.StartInfo.FileName = "winword";
    proc.StartInfo.Arguments = wordPrintFilePath;
    proc.Start();
    NOTE: Remember to change the path to the template.docx file as required by your own situation.
  33. Add the following using statements to your form's code file:
    using System.IO;
    using System.IO.Packaging;
    using System.Diagnostics;
    using System.Xml.Xsl;
  34. Give your InfoPath form Full Trust by going to Tools > Form Options and selecting the Security and Trust tab. And sign your InfoPath form with a digital certificate.
  35. Build your InfoPath project.

You should now have a fully functional InfoPath form with a button that when clicked on will open a Word 2007 document containing the information that was filled out on the form.

 


Related InfoPath Articles:

 

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. 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.

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