0

I have an MVC3 app (C#, razor) with an object that has all of the data I need for a dynamic PDF report that is to be viewed in the users browser. My Companies VS2010 solution already has iTextSharp.dll v 5.0.5 referenced and is used elsewhere for "stamping" values on a static PDF form.

All of the examples I find for iTextSharp are doing Static PDF with stamper(easy) or they are using 4.1.6 and they leverage iTextHandler. ITextHandler is not in v 5.0.5. I tried using HTMLWorker with no luck. My code is below

3 ways to do dynamic PDF

preferred solution: 1. I want to bind to a dynamic PDF form. By dynamic, I mean the ability to do repeating subforms, etc. I have a PDF I created in Adobe LifeCycle ES2 and saved as a XLA. I see all of the Adobe Docs referring to making connections with XML, etc. but no examples of how to actually implement this. I know this can't be hard. Example in C# please?

optional solution: 2. Use what I have now (Works w/ iTextSharp.dll 4.1.6 only) Use

optional solution: 3. Generate as HTML and start looking at HTML to PDF methods

Option 2 Code: This code is in a controller class:

    /// <summary>
    /// Returns a PDF action result. This method renders the view to a string then
    /// use that string to generate a PDF file. The generated PDF file is then
    /// returned to the browser as binary content. The view associated with this
    /// action should render an XML compatible with iTextSharp xml format.
    /// </summary>
    /// <param name="model">The model to send to the view.</param>
    /// <returns>The resulted BinaryContentResult.</returns>
    protected ActionResult ViewPdf(object model)
    {
        // Create the iTextSharp document.
        Document doc = new Document();
        // Set the document to write to memory.
        MemoryStream memStream = new MemoryStream();
        PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
        writer.CloseStream = false;
        doc.Open();

        //// Render the view xml to a string, then parse that string into an XML dom.
        string xmltext = this.RenderActionResultToString(this.View(model));

        #region  This code works with iTextSharp version 4.1.6.0 (free version of iTextSharp)
        /*
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.InnerXml = xmltext.Trim();

        // Parse the XML into the iTextSharp document.
        ITextHandler textHandler = new ITextHandler(doc);
        textHandler.Parse(xmldoc);
        */
        #endregion

        #region  This code works with iTextSharp version 5.0.5 (not free version of iTextSharp)
        HTMLWorker htmlWorker = new HTMLWorker(doc);
        StringReader reader = new StringReader(xmltext);
        htmlWorker.Parse(reader);
        #endregion

        // Close and get the resulted binary data.
        doc.Close();
        byte[] buf = new byte[memStream.Position];
        memStream.Position = 0;
        memStream.Read(buf, 0, buf.Length);

        // Send the binary data to the browser.
        return new BinaryContentResult(buf, "application/pdf");
    }
    #endregion

Option 2 Code: This code is in a view (cshtml) file:

 @model SomeModelSpecificToMyPurpose
 <?xml version="1.0" encoding="UTF-8" ?>
 <itext  creationdate="@DateTime.Today" producer="iTextSharpXML">
 <paragraph leading="18.0" font="unknown" size="16.0" align="Default">
    <chunk>RTPA Result in PDF</chunk>
</paragraph>
<paragraph leading="18.0" font="unknown" size="16.0" align="Default">
    <chunk>@DateTime.Today</chunk>
</paragraph>
<paragraph leading="18.0" font="unknown" size="10.0" align="Default">
    <chunk>Customer Name: @this.Model.Transaction.PatientFirstName</chunk><newline />
    <chunk>Address: @this.Model.Transaction.ProviderFullName</chunk><newline />
</paragraph>
<paragraph leading="18.0" font="unknown" size="10.0" align="Default">
<chunk font="unknown" size="12.0">Orders:</chunk><newline />
</paragraph>

1 Answer 1

1

LifeCycle forms and iText don't get along all that well. It'll get/set values, but that's about it.

You could do something similar by regenerating your form in code for each user, but that's a non-trivial task.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.