![]() Unfortunately, however, Adobe does not offer a free SDK that you can download and use; you have to pay to license the API, and a fair amount of work is usually required to get it up and running. ITextSharp alleviates this problem. ITextSharp is a port of the iText Java PDF library that gives you the ability to add PDF functionality to your applications. Using iTextSharp, you can create and read PDF files without any costs or proprietary software, so you can deliver the functionality your users expect. Using iTextSharp To start, you’ll need to add a couple of using statements to your code: using iTextSharp.text; using iTextSharp.text.pdf; Then create a Document object: Document pdfDocument = new Document( ); Next, create an instance of the PdfWriter and point it to where you want to save this document. 4.12 Creating PDFs with iTextSharp Adobe's PDF format is one of the most widely. Using iTextSharp, you can create and read PDF files without any costs. A chunk is simply any piece of text with a consistent style; using it, you can specify. In this example I’ll save to a file on the hard drive, but you can save it to any valid IO stream: PdfWriter.GetInstance(pdfDocument, new FileStream('C: WDPT.PDF', FileMode.Create)); Open the document and add some content: pdfDocument.Open( ); pdfDocument.Add(new Paragraph('Here is a test of creating a PDF')); Then close it. You don’t need to flush the stream or actually write out the document. Close( ) does it all for you in one call: pdfDocument.Close( ); The document created can be seen in. Figure 4-25. A simple PDF Of course, your requirements will rarely be so simple. ITextSharp offers a wide range of features to create more complex PDFs. In the previous example, we used a paragraph to add text to the document, but you can also use phrases and chunks to create the text you want. A chunk is simply any piece of text with a consistent style; using it, you can specify independent fonts and colors. A phrase is a collection of chunks that includes a leading separator (the amount of vertical space between lines). Chunks and phrases can be added to paragraphs or added directly to documents. Let’s create a couple of chunks with different fonts: Chunk c = new Chunk('Some text in Verdana n', FontFactory.GetFont('Verdana', 12 )); Chunk c2 = new Chunk('More text in Tahoma', FontFactory.GetFont('Tahoma', 14)); and then create a paragraph and add those chunks: Paragraph p = new Paragraph( ); p.Add(c); p.Add(c2); You can see the results in, where both chunks have their respective fonts. Figure 4-26. Chunks with fonts iTextSharp also provides support for working with images and embedding those images in your documents. Images can be added through URLs: Image image = Image.GetInstance( 'or from the filesystem: Image image = Image.GetInstance('home_tarsier.jpg'); PNG, GIF, JPEG, and WMF images can be loaded in this way. You can now add the image to the document or paragraph with the following code: Document pdfDocument = new Document( ); PdfWriter.GetInstance(pdfDocument, new FileStream('C: WDPT.PDF', FileMode.Create)); pdfDocument.Open( ); Image image = Image.GetInstance( 'Chunk c = new Chunk('Check out this wicked graphic: n', FontFactory.GetFont('Verdana', 12 )); Paragraph p = new Paragraph( ); p.Add(c); p.Add(image); pdfDocument.Add(p); pdfDocument.Close( ); The generated PDF is shown in. Manipulating PDFs with iTextSharp On a recent project, I was given the requirement of assembling large policy packets for an insurance client. The client had each piece of the policy either in a static PDF or in a file that would be dynamically converted to PDF. We wanted to get to an end result where the packet would be a single PDF, so it would be easier to store and would require only one print job, so there was no chance of part of it being lost. The problem was that the packet was different based on each individual policy, so we had to find a way to dynamically combine anywhere from 3 to 20 PDFs into one document. ITextSharp was the solution. ITextSharp makes it easy to merge multiple PDFs into a single document, using some code posted at. Simply download the code, and you’ll be able to easily concatenate multiple PDFs into a single PDF. ![]() You can compile the code as a Console application and use it as-is, or you can modify it and use it directly in your application. Arduino lcd display projects 16x2. Additional code for splitting PDFs, creating PDFs with four pages per page sheet to make handouts, and encrypting PDFs is available from. We intended this article in order to make global MVC development community aware of the technique to generate PDF file from HTML with the help of iTextSharp. ITextSharp is a.net PDF library that enables developers to produce PDF file. How it makes the things possible, let’s find out.
0 Comments
Leave a Reply. |