Producing a PDF from an InfoPath web-fillable form


  • Forum Community,


    I would like to call a method from the code editor in InfoPath for the purposes of creating a PDF document. I'm using iTextSharp ( I've added references to the iTextSharp DLL to my form. I have an InfoPath form titled "STD681.xsn", and I added managed code written in C#. The error message that I'm receiving is listed below:

    Data at the root level is invalid. Line 2, position 2.
       at System.Xml.XmlTextReaderImpl.Throw(Exception e)
       at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
       at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
       at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
       at System.Xml.XmlTextReaderImpl.Read()
       at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
       at System.Xml.XmlDocument.Load(XmlReader reader)
       at System.Xml.XmlDocument.LoadXml(String xml)
       at STD681.FormCode.GeneratePDF()
       at STD681.FormCode.CTRL53_5_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)


    My source code is listed below:

    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    using System.Text;
    using System.IO;
    using System.Security.Cryptography;
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    namespace STD681
        public partial class FormCode
            // Member variables are not supported in browser-enabled forms.
            // Instead, write and read these values from the FormState
            // dictionary using code such as the following:
            // private object _memberVariable
            // {
            //     get
            //     {
            //         return FormState["_memberVariable"];
            //     }
            //     set
            //     {
            //         FormState["_memberVariable"] = value;
            //     }
            // }

            // NOTE: The following procedure is required by Microsoft InfoPath.
            // It can be modified using Microsoft InfoPath.
            public void InternalStartup()
                ((ButtonEvent)EventManager.ControlEvents["CTRL53_5"]).Clicked += new ClickedEventHandler(CTRL53_5_Clicked);

            private void GeneratePDF()
                // Retrieve XML of InfoPath form
                XPathNavigator root = this.MainDataSource.CreateNavigator();
                string xml = root.SelectSingleNode("/my:myFields", this.NamespaceManager).ToString();
                // Load XML of InfoPath form into an XmlDocument object
                XmlDocument xmlForm = new XmlDocument();

                // Add the 'my' namespace of the form to an XmlNamespaceManager object
                XmlNamespaceManager xmlNameSpaceMgr =
                                new XmlNamespaceManager(
                                new NameTable());
                xmlNameSpaceMgr.AddNamespace("my", "");

                // Retrieve the values of the InfoPath form fields using XPath
                string agency = xmlForm.SelectSingleNode("/my:myFields/my:name_of_agency",
                string period = xmlForm.SelectSingleNode("/my:myFields/my:for_the_period",
                string org_unit = xmlForm.SelectSingleNode("/my:myFields/my:organization_unit",

                // Using iTextSharp to construct a PDF document
                // Create a document-object
                Document document = new Document(PageSize.A4);

                // Create a writer that listens to the document
                // and directs a XML-stream to a MemoryStream
                using (MemoryStream ms = new MemoryStream())
                    PdfWriter.GetInstance(document, ms);


                    Font defaultFont = FontFactory.GetFont(
                        FontFactory.HELVETICA, 10, Font.NORMAL);
                    Font labelFont = FontFactory.GetFont(
                        FontFactory.HELVETICA, 10, Font.BOLD);

                    // Add agency
                    Paragraph paragraph = new Paragraph(15F);
                    paragraph.Add(new Chunk("Name of Agency: ", labelFont));
                    paragraph.Add(new Chunk(agency, defaultFont));

                    // Add period
                    paragraph = new Paragraph(15F);
                    paragraph.Add(new Chunk("For the Period: ", labelFont));
                    paragraph.Add(new Chunk(period, defaultFont));

                    // Add org_unit
                    paragraph = new Paragraph(15F);
                    paragraph.Add(new Chunk("Organization Unit: ", labelFont));
                    paragraph.Add(new Chunk(org_unit, defaultFont));


                    // Return the InfoPath form as a PDF document
                    byte[] data = ms.ToArray();
                    //Response.Clear(); //This code needs to be tweaked because "Response" is not recognized; disregard for now.
                    //Response.ClearHeaders(); //This code needs to be tweaked because "Response" is not recognized; disregard for now.
                    //Response.ClearContent(); //This code needs to be tweaked because "Response" is not recognized; disregard for now.
                    //Response.Buffer = true; //This code needs to be tweaked because "Response" is not recognized; disregard for now.
                    //Response.ContentType = "application/pdf"; //This code needs to be tweaked because "Response" is not recognized; disregard for now.
                    //Response.BinaryWrite(data); //This code needs to be tweaked because "Response" is not recognized; disregard for now.
                    //Response.End(); //This code needs to be tweaked because "Response" is not recognized; disregard for now.


            public void CTRL53_5_Clicked(object sender, ClickedEventArgs e)


    Could someone please suggest how I might go about resolving the above exception?





    Tuesday, January 24, 2012 10:14 PM

All replies

  • Sorry i cant help with your code problem, have you looked at this solution before:

    Muhimbi can do this for you as well as many other file formats, its likely going to be cheaper and more advanced than the tool you can build on your own.

    Basically you create a view in InfoPath and this is what it prints out for you.

    It also integrates with workflow as well.

    Regards, Chris

    Sunday, February 19, 2012 5:08 AM
  • Hello my friend!

    I know that this answer is a little bit late but I do think it is very important to never leave a person without an answer.

    As far as I know your command    "string xml = root.SelectSingleNode("/my:myFields", this.NamespaceManager).ToString();"   will not take the XML structure as a string, but it takes thee values the XML contains - such as the values in texboxes.

    To retrieve the XML structure you should use:

    string xml = root.SelectSingleNode("/my:myFields", this.NamespaceManager).OuterXml;

    and only then, you can load the xml using:

     xmlForm.LoadXml(xml); (So the exception actually happened here).

    I hope I helped somehow to someone ; )

    Wednesday, June 17, 2015 8:03 AM