Quick Guide for iTextSharp and Quick Guide for itextsharp

Source: Internet
Author: User

Quick Guide for iTextSharp and Quick Guide for itextsharp
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:

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 = 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:

// 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:

The Code has been downloaded.
Where can I download Song Ci's "FAQ list"?

It's very easy to download the "". Search Baidu for the first result of "".
Reference: www.baidu.com/..8&cl;3

Who can give the itextsharp50 User Manual a lot more than before, how to generate a word before it can use the RTF, now no

There is no manual for this version, and the RTF part is independent. You can find the relevant library.

Related Article

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.