Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create PDF from form data and save it

Tags:

c#

asp.net

pdf

I am using editable pdf files (created by Nitro PDF Software) in my application. These pdf files have a lot of editable fields (like textboxes) and one button (like submit).

Whenever a user opens that pdf file, enters the text, and clicks the submit button they get redirected to an aspx page.

How do I get all the static and dynamic values that are posted to this page, and create another pdf file with the entered data? And how do I save the created pdf file?

like image 825
hmk Avatar asked Feb 22 '12 12:02

hmk


3 Answers

From Wikipedia, PDF Interactive elements there are two possibilities for integrating data and PDFs (there are also links to the specifications):

  • AcroForms (also known as Acrobat forms), introduced in the PDF 1.2 format specification and included in all later PDF specifications.
  • Adobe XML Forms Architecture (XFA) forms, introduced in the PDF 1.5 format specification. The XFA specification is not included in the PDF specification, it is only referenced as an optional feature. Adobe XFA Forms are not compatible with AcroForms.

For compatibility issues I would go for AcroForms. In that case I would use XFDF, because it is XML and therefore easier to parse. I never used Nitro, but when you build a PDF form you usually provide a "Save" button and choose at action for this button "Send/Post form to server" with the data format XML which is just XFDF.

This works only when the PDF is viewed in the browser. So the typical use case is: have an empty PDF template on the server, before returning the PDF to the user mix your data into the PDF, the user enters data in the form (PDF is opened in the browser via a plugin or natively in Chrome), then the user presses the save buttons which posts a xml on the server. The next time the user asks for his PDF, you took the recent data and mix it again with the template.

So only two questions are open:

  • how to generate a XFDF:
    very easy, see http://wiki.developerforce.com/page/Adobe_XFDF or Parsing XML using XDocument for an example of the file structure
  • how to mix a XFDF with the PDF: This can be done with itext, there are several examples on stackoverflow, eg. https://stackoverflow.com/a/6274311/734687

See the complete process here: http://itextpdf.com/book/chapter.php?id=9 . This example updates the PDF with the form dynamically at run time. Since iText is used there is no difference between Java and C#.

Be aware that previous versions of iText (Java up to 2.1.7 and C# up to 4.1.6) were distributed under the Mozilla Public License or the LGPL, while current versions are distributed under the Affero General Public License. Thats explains why the older versions are still used.

like image 135
ChrLipp Avatar answered Sep 28 '22 11:09

ChrLipp


User following code for Generate PDF in asp.net:

There's a full code sample below to get you started. // Code

using System;
using System.IO;
using System.Diagnostics;

using iTextSharp.text;
using iTextSharp.text.pdf;

public class iTextDemo 
{
 public static void Main() 
 {
  Console.WriteLine("iText Demo");

  // step 1: creation of a document-object
  Document myDocument = new Document(PageSize.A4.Rotate());

  try 
  {

   // step 2:
   // Now create a writer that listens to this doucment and writes the document to desired Stream.

   PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));

   // step 3:  Open the document now using
   myDocument.Open();

   // step 4: Now add some contents to the document
   myDocument.Add(new Paragraph("First Pdf File made by Salman using iText"));

  }
  catch(DocumentException de) 
  {
   Console.Error.WriteLine(de.Message);
  }
  catch(IOException ioe) 
  {
   Console.Error.WriteLine(ioe.Message);
  }

  // step 5: Remember to close the documnet

  myDocument.Close();
 }
}
like image 39
ankit rajput Avatar answered Sep 28 '22 12:09

ankit rajput


Try open source library http://pdfsharp.codeplex.com/, sample can be found here http://www.pdfsharp.net/wiki/.

like image 38
Denis Besic Avatar answered Sep 28 '22 10:09

Denis Besic