none
Producing a PDF from an InfoPath web-fillable form

    Question

  • 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 (http://www.itextpdf.com/download.php). 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:

    System.Xml.XmlException
    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();
                xmlForm.LoadXml(xml);

                // Add the 'my' namespace of the form to an XmlNamespaceManager object
                XmlNamespaceManager xmlNameSpaceMgr =
                                new XmlNamespaceManager(
                                new NameTable());
                xmlNameSpaceMgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-10-02T08:59:49");

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

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

                    document.Open();

                    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));
                    document.Add(paragraph);

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

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

                    document.Close();

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

                    ms.Close();
                }
            }

            public void CTRL53_5_Clicked(object sender, ClickedEventArgs e)
            {
                GeneratePDF();
            }
        }
    }

     

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

     

    Thanks,

     

    ~Woodrow

    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.

    http://www.muhimbi.com/Products/PDF-Converter-for-SharePoint.aspx


    Regards, Chris

    Sunday, February 19, 2012 5:08 AM