Quick Guide to itextsharp

Source: Internet
Author: User
Preface

Recently, the company has been using itext to develop PDF reports. After a while, we found that the itext class library is huge, but as a programmer, we don't need to know all the classes and methods, we only need to know how to use it.

Therefore, this article shows you how to quickly use itextsharp (itext. Net version) for development.

Itextsharp development procedure

Before rapid development, we should first understand the following four classes:

Class Meaning
Paragraph Text in reports
Image Images in reports
Pdfptable Table
Pdfpcell Cell

After knowing these four classes, it is the development step:

1. add content to the cell sequence pcell class.

2. Add the cell sequence pcell to the pdfptable.

3. Add the pdfptable table to the document.

In the preceding steps, the most important step is to add content to javaspcell. The content in javaspcell can be divided into the following three situations:

Text Paragraph
Image Image
Table Pdfptable

Next, let's go straight to the topic to see how to add content to javaspcell. Because reports require data, I have obtained some data from Baidu resolution statistics. The following is the URL:

Add text to pdfpcell

The text is represented by paragraph. Note the font before adding the text, because we use a Chinese font. If we use the default English font for rendering, it will be garbled, therefore, we must first define the Chinese font:

BaseFont BF_Light = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

After the font is defined, add text as follows:

// Set the font and size P = new paragraph (fields [I], new font (bf_light, 13); cell = new pdfpcell (P ); // set cell attributes // cell. border = rectangle. no_border; If (rownum = 0) {Cell. backgroundcolor = basecolor. gray;} if (I = maincolumn-1) {Cell. horizontalalignment = element. align_right;} // Add a cell table. addcell (cell );

In fact, it is easy to add, that is, the top two lines of code, and the Code for setting the javaspcell attribute is usually more, because we generally need to set its background color, horizontal to it, and border.

Add images and tables to mongopcell

After you know how to add text to mongopcell, it is much easier to add images and tables. You just need to pass the image and pdfptable as the constructor parameters of mongopcell:

// Image = image. getinstance (ImagePath); cell = new pdfpcell (image, true); table. addcell (cell); // table pdfptable basetable = getbasetable (); cell = new partition pcell (basetable); table. addcell (cell );

The following is:

Itextsharp plotting

With the aforementioned mongopcell operations, you can implement some common PDF reports. Now we can further achieve the following results:

Drawing is required here, and the drawing class is contentbyte. This is similar to the drawing board. We can draw straight lines and text directly on it:

// Draw a canvas. savestate (); canvas. setlinewidth (2f); canvas. moveTo (100,100); canvas. lineto (200,200); canvas. stroke (); canvas. restorestate (); // text columntext. showtextaligned (canvas, element. align_right, new phrase ("julyluo test", new font (bf_light, 10), 100, 20, 0 );

We need to note that coordinates are required for both the draw line and the text, and the specific code should be placed in the middle of savestate and restorestate during the draw line, this will not cause disorder in the drawing status.

If we want to draw the image in a cell, but we know that the drawing requires coordinates, but the coordinates are not exposed in pdfpcell, we need the interface ipdfpcellevent in itextsharp.

public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)

This interface is used to expose cells after they are added to the document. Obviously, the coordinates can be obtained through the postion parameter, and the canvases parameter can be used to obtain the canvas.

To draw a picture, create a class that implements the ipdfpcellevent interface, and draw lines and text in the celllayout method:

Public void celllayout (export pcell cell, rectangle position, export contentbyte [] canvases) {export contentbyte cb = canvases [pdfptable. backgroundcanvas]; inclucontentbyte cbline = canvases [pdfptable. linecanvas]; cbline. savestate (); CB. savestate ();............ CB. setlinewidth (0.4f); cbline. setlinewidth (0.4f); // y axis CB. moveTo (leftx, bottomy); CB. lineto (leftx, topy); CB. stroke (); // float yaxisetextlinetwidth = 3f; float yaxistextspaceadjust = 2.5f; For (float y = yscalenum; y <Ymax; y + = yscalenum) {float ypoint = bottomy + (yscale * Y); CB. moveTo (leftx, ypoint); CB. lineto (leftx-yaxisetextlinetwidth, ypoint); CB. stroke ();} // y axis text for (floa T y = yscalenum; y <Ymax; y + = yscalenum) {float ypoint = bottomy + (yscale * Y); columntext. showtextaligned (CB, element. align_right, new phrase (string. format ("{0 }%", Y), new font (bf_light, 5), leftx-yaxisetextlinetwidth, ypoint-yaxistextspaceadjust, 0);} // X axis CB. moveTo (leftx, bottomy); CB. lineto (righx, bottomy); CB. stroke ();......... CB. Stroke (); Cb. restorestate (); cbline. restorestate ();}

Finally, associate the class with the corresponding pdfpcell:

// Painting class, associated with the cell resolutionchart chart = new resolutionchart (filename, Ymax, yscale); cell. cellevent = chart;

The following is:

In summary, we use itextsharp for development. If the report only contains text and images, we can use javaspcell as a class. But if you want to draw some bar charts and bar charts, these charts need coordinates to be presented. We can get the coordinates through the ipdfpcellevent interface, then draw the corresponding graph, and finally download the code.

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.