Generate PDF files with Itextsharp tips

Source: Internet
Author: User

Recently the company is doing a project, need to export data into a PDF file, and then search the Web to find open source components are quite many, with more is itextsharp,itextsharp by the Java Itext evolved, and provide rich features, Able to make forms, insert pictures, etc., this is the official website: http://sourceforge.net/projects/itextsharp/, the following is a brief introduction to the use of itextsharp experience.

First create a WinForm application, and add a itextsharp reference to the button's click event Write on the code that generates the PDF,

      New Document (pagesize.a4,0,0,0,0); // create an object for a PDF document, set the paper size to A4, and the margins to 0
PageSize.A4.Rotate (); When you need to set the PDF paper to landscape, use PageSize.A4.Rotate ()
 PDFWriter write = pdfwriter.getinstance (doc, new  FileStream (@ " e:\pdffile.pdf  " , FileMode.OpenOrCreate, FileAccess.Write)); //  Create an object to write to the PDF,  Basefont Bsfont = Basefont.createfont (@ " c:\ windows\fonts\simsun.ttc,0    = new  font (bsfont); //   Docpdf.open ();  //Open 
      docpdf. Add (New Paragraph ("First PDF file", font));//write a phrase into a PDF

DocPDF. Close ();//Closed

The above code simply writes a simple string to the PDF file, and if you want to make a table with Itextsharp, you can use Pdfptable, pdfpcell,pdfptable to create a table, Pdfpcell to create a cell, Using this method to create a table is a bit like the way Npoi creates Excel, and also adds cells to the table. Here is a brief introduction.

The code that created the PDF object above does not change

Then create a table pdfptable Tablerow1 = new pdfptable (New float[]{20,20,20}), and the following parameter indicates that the table has a column width of 20 and a column number of 3 columns

Add cells to a table

 for (int0; i++) // represents the creation of a 3-column, 9-row table            {               new Pdfpcell (new  Paragraph (i.tostring (), font));                             // Tablerow1. Addcell (               tablerow1. Addcell (cell); // add cells to a table                           }           docpdf.add (TABLEROW1);//Add a table to a PDF document

The resulting PDF file

From the results you can see that when you add a table in a loop, the direction you add is left-to-right and then added. But if we're going to design a table that's more complex, it's different, like the need to cross-row or cross-column, and set the height of the table,

Docpdf.newpage ();//A new page showsPdfptable tablerow2=NewPdfptable (New float[] { -, -, - }); Cell=NewPdfpcell (NewParagraph ("table cross-line row1 col1", font)); Cell. Minimumheight= 40F;//set the height of the tableCell. Rowspan =2;//cell boast 2 linesTablerow2.           Addcell (cell); Cell=NewPdfpcell (NewParagraph ("table cross-line Row1 col2", font)); Cell. Minimumheight= 40F;//set the height of the tableTablerow2.           Addcell (cell); Cell=NewPdfpcell (NewParagraph ("table cross-line Row1 col3", font)); Cell. Minimumheight= 40F;//set the height of the tableTablerow2.           Addcell (cell); Cell=NewPdfpcell (NewParagraph ("table cross-line Row2 col3", font)); Cell. Minimumheight= 40F;//set the height of the tableTablerow2.           Addcell (cell); Cell=NewPdfpcell (NewParagraph ("table cross-line Row2 col3", font)); Cell. Minimumheight= 40F;//set the height of the tableTablerow2.           Addcell (cell); Docpdf.add (TABLEROW2);

The resulting table is as follows:

From the results we are not hard to find cell. RowSpan can set the cross-columns of the table across rows corresponding to the colspan used to set the table.

The above is just my own in the use of itextsharp when some simple experience, hope to help everyone.

Specific examples can be found on the official website, very detailed http://itextpdf.com/book/examples.php

Generate PDF files with Itextsharp tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.