Fill in PDF Form Fields Using the Open Source. I downloaded a copy of the W-4 PDF form from the. The template PDF itself is never populated and it is used. Hey everyone, im new to iTextSharp, i wanted to have a new pdf file by update the existing template pdf, and i followed this tutorial http. PDF creation using iText toolkit is a very common and is being used in various projects and assignments widely. But, sometimes there is a need to create PDF using. Using a template to programmatically create PDFs with C#. You can flatten the entire form. Yes, Full Text Search! In Part 1 we retrieved a PDF template's form field coordinates. Let's put them to use and create simple multi-page billing. Java and PDF with iText This article demonstrate how to create PDF files with Java and the iText library. In this tutorial iText version 5.0.x is used. Fields inherited from class com.lowagie.text.pdf. Creates a new template that is nothing more than a form XObject. This article, by Scott Mitchell, shows how to programmatically populate PDF form fields using ASP.NET and the free, open source iTextSharp library. Fill PDF forms in C# with i. C# - Generate and Deliver PDF Files On- Demand from a Template Using i. Text. Sharp. In this article, we will examine the specifics of this . While this in itself is not technically challenging, I recently had a need to generate customized PDF's on a per- user basis. The application in question basically provides a way for attendees of particular trainings to access their individualized certificates of completion through a link which is emailed to them. It was decided that persisting the many individual certificates which might accumulate on the server over many trainings (we do a LOT of trainings each year) made no sense whatsoever. Image by Curtis Ellis . Ordinarily, I try to provide a link to a functional code example on my Github repo. In this case, the code discussed in the article is a small piece of a functional site example, and it was not practical at the time of writing to build out an example project without losing the important bits amongst the models, controllers, views, etc. I may do a walk- through as a separate series in the future. Meanwhile, if you have questions about implementing the code below, p. In this article, we will examine the specifics of this . We will need to pull i. Text. Sharp into our project in order to do what we need to do. You can get i. Text. Sharp binaries as described in the previous post, or you can take the easier approach and use Nuget. Note that there are two versions of the i. Text. Sharp library available via Nuget – the standard i. Text. Sharp library, and thei. Text. Sharp LGPL/MPL version. The licensing of the standard i. Text. Sharp library is more restrictive, having moved to the GNU Affero General Public License (AGPL). In this project I used the LGPL/MPL version. You can quickly install i. Text. Sharp via the Nuget Package Manager Console by doing: Install i. Text. Sharp via Nuget Package Manager Console: PM> Install- Package i. Text. Sharp- LGPLFor our purposes, we are intending to merge our data into a pre- existing PDF form template, and then flatten the PDF document, including the populated form fields. We will do this entirely in memory, and then stream the result back to the caller. We will first set up a core class, Pdf. Merge. Streamer, which will take in a file path (which points to the PDF Form template), an IEnumerable< IPdf. Merge. Data> , and a System. IO. Memory. Stream. IPdf. Merge. Data is an interface which will represent an object containing a Dictionary mapping text values (the Dictionary values) to the Pdf Form fields in our template by name (the Dictionary keys). Our Pdf. Merge. Streamer class will basically consume a list of one or more merge data items, populate and flatten the template for each, and add each to a single output document (think Word Merge, but with PDF). Of course, there's more to it than that, and we'll walk through it in a moment. The basic idea was to create a generalized class which can be used to populate any PDF form template, so long as it is provided with the correct values for each field in the template. Pdf. Merge. Streamer Class: using i. Text. Sharp. text. Text. Sharp. text. System. Collections. Generic. using Training. Department. Azure. Models. namespace Training. Department. Azure. For now, though, we will leave it as is. Now let's take a look at what we are passing in. The template. Path is, obviously, a path to a file on the local file system. Which leaves our IEnumerable< IPdf. Merge. Data>. In my case I decided I wanted to be able to create different merge templates, which required different sets of merge fields, all of which would need to be mapped according to the specific data required for the merge. While we could have simply passed in a Dictionary< string, string> straight way, I decided that creating a specific interface would make the intent more clear, and also force me to write a concrete implementation for each mapping. The interface itself is straightforward: The IPdf. Merge. Data Interface: publicinterface IPdf. Merge. Data. . Let me be clear that you do not. You need one of these programs only to create the template itself, which is then saved on the server and consumed by the code in this article. However, Acrobat is a little spendy, so if you don't have access to that, you might avail yourself of the Open- Source program, Scribus. Scribus is not a PDF creation program per se, in fact it is a page layout application. However, it present an Export to PDF option, and allows us to place named form fields on our page, along with text, images, and other content. There is a small learning curve to using Scribus to produce a workable PDF form, and I will discuss that in another post. However, three suggestions to consider when using Scribus to make your form: Place form fields on their own layer in the Scribus Document. This keeps the fields separated from other content, and makes editing much easier. When you export, use use the PDF 1. Acrobat 5) setting. There seem to be issues with the other PDF settings. Be careful with font choices when using Scribus. While most fonts will appear correctly in the Scribus document itself, when exporting to PDF in the older format(s) currently supported by Scribus, they do not always display correctly on the output PDF document. Make sure to name your fields carefully when adding them to the PDF Form. The field names must match the keys you provide in your concrete implementation of IPdf. Merge. Data. Obviously, using Acrobat is preferred for creating a template for our purpose. However, I had to use Scribus to create a template for my own application, and after a little trial and error, it worked just fine. Ok, now that we have the basic pieces in place, let's walk through a simplified example of how I implemented the rest of this. This will help you see how you might create your own implementation to meet your own domain- specific requirements. In my ASP. NET MVC application, one set of my (simplified) requirements are: Training attendees should be able to download a personalized certificate via a link received by email System Users should be able to create and download a batch of certificates for all attendees for a particular training (sometimes the training sponsor wants the whole thing printed and mailed to them) System Users should be able to download individual attendee certificates (sometimes attendees don't have email, or need us to send a certificate directly to a third party. Or, have trouble downloading from the link and need the certificate sent as an actual attachment. Certificate PDF files should not be persisted on the server, but instead will be streamed directly to the client browser after generation. We won't cover all the details of building out this application here – we will focus on the implementation of the certificate generation on demand. Let's start with the methods needed on Attendee. Certificates. Controller. Among the other methods on our controller, two are of specific interest to us here, Download. Certificate and Download. Batch. These will a return File object in the response body representing an individual certificate, or a batch or certificates respectively, which will be downloaded by the client's browser. What is important to note here is that all of the processing happens in memory, and the resulting certificate is not persisted locally on the server. Note that for simplicity, I have not implemented much exception handling here, or async processing (which might be in order, since processing a batch of a few hundred certificates could take some time). We'll look at Download. Certificate first. The Download. Certificate Method of Attendee. Certificates. Controller. The idea was to create a file name with a user- friendly component, but which would be suitably unique. The method is used by both Download. Certificate and Download. Batch, and is as follows. The get. Pdf. File. Name Method: string get. Pdf. File. Name(string friendly. Name, Guid guid). We haven't discussed this one yet. The single instance is added to a list and passed to get. Attendee. Merge. Data, which performs the mapping we need for our template. As we can see, the get. Attendee. Merge. Data method uses each instance of Attendee to initialize a new instance of Attendee. Certificate. Merge. Data. Attendee. Certificate. Merge. Data is the concrete implementation we will use specifically for mapping to our PDF Form template. The private method get. Merge. Dictionary is called when the Merge. Field. Values property is accessed, and returns a Dictionary containing key- value pairs for each of my template fields. In some cases, the form template requires concatenations, and/or other manipulations of attendee data to make it suitable for presentation. From there, the attendee is passed, along with a path pointing to a locally persisted PDF form template file, to an instance of Attendee. Certificate. Streamer. This, along with the template path and a newly- created System. IO. Memory. Stream are then passed to Pdf. Merge. Streamer. The controller sets the content disposition, file name, and content type and returns a File. Content. Result, which adds our file stream to the HTTP response body, and is ultimately downloaded by the client's browser. Sometimes the training sponsor wants these printed and mailed. We have thus far examined what is actually the special case – a single certificate (in other words, a . In fact, these two methods are probably candidates for refactoring, but we won't worry about that right now. Primarily, this method accepts an IEnumerable< Attendee> as an argument, and is flagged with an . However, the first class we examined, Pdf. Merge. Streamer, in conjunction with the interface IPdf. Merge data, should get you started. Feel free to email me or comment with questions, or to point out errors. Feedback is welcome and appreciated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |