Implement PDF report with Java

xiaoxiao2021-03-06  56

I. Foreword In the information system of the enterprise, the report processing has always been an important role. This article will introduce a Java component generated by the PDF report --ITEXT. By generating a PDF report in the server side, the client uses a super connection to display or downloads the generated report, which is very good to solve the report processing problem of the B / S system.

Second, ITEXT Introduction

IText is a list of SourceForge, which is a famous open source site, is a Java class library for generating a PDF document. The documentation of PDF or RTF can be generated through ITEXT, but also the XML, HTML files can be converted to a PDF file.

IText installation is very convenient, after downloading ITEXT.JAR files on http://www.lowagie.com/itext/download.html - Download website, just add ITEXT.jar path in the system's classpath, in the program You can use the IText class library.

Third, establish the first PDF document

Generating a PDF document with IText requires 5 steps:

1 Establish an instance of the com.lowagie.text.Document object.

Document Document = New Document ();

2 Create a writing (Writer) to the Document object, write the document into the disk via the writing (Writer).

Pdfwriter.getInstance (Document, New FileoutputStream ("HelloWorld.pdf");

3 Open the document.

Document.open ();

4 Add content to the document.

Document.Add (New Paragraph ("Hello World"));

5 Close the document.

Document.close ();

By 5 steps above, you can generate a file of helloworld.pdf, and the file content is "Hello World". Establishing a COM.LOWAGIE.TEXT.Document object Am.lowagie.Text.Document object's build function has three, namely:

Public Document (); Public Document (Rectangle PageSize); Public Document (Rectangle PageSize, Int Marginleft, Int Marginright, Int Margintop, Int Marginbottom);

The parameter of the building function is the size of the document page. For the first build function, the size is A4, the same as Document (PageSize.a4); for the third build function, parameter marginlet, marginright, margintop, marginbottom Left, right, upper and lower margins, respectively. You can set the page size, face background color, and attributes such as page size, face background color, and page transverse / portrait. IText defines Paper types such as A0-A10, Al, Letter, Halfletter, _11x17, Ledger, Note, B0-B5, Arch_a-Arch_e, FLSA, and FLSE, or through Rectangle PageSize = New Rectangle (144, 720); customizes Paper. The page can be set to a horizontal () through the Rectangle method. Writer Object Once Document Objects are established, you need to create one or more writer (Writer) objects. By writing, the specific document can be set to the required format, such as com.lowagie.text.pdf.pdfwriter can save the document into a PDF file, com.lowagie.text.html.htmlwriter can save a document HTML file. Setting document properties Before you open, you can set the title, the subject, author, keyword, binding method, creator, producer, creation date, etc. of the document, the method of the call is: public boolean addtitle (String Title) public boolean addSubject (String subject) public boolean addKeywords (String keywords) public boolean addAuthor (String author) public boolean addCreator (String creator) public boolean addProducer () public boolean addCreationDate () public boolean addHeader (String name, String content)

The method AddHeader is invalid for the PDF document, and addheader is only valid for the HTML document and is used to add header information of the document. When the new page is generated, you can set the size of the page, the bookmark, footerfooter, and the method called:

public boolean setPageSize (Rectangle pageSize) public boolean add (Watermark watermark) public void removeWatermark () public void setHeader (HeaderFooter header) public void resetHeader () public void setFooter (HeaderFooter footer) public void resetFooter () public void resetPageCount () public void SetPageCount (int Pagen)

If you want to set the page properties of the first page, these methods must be called before the document is opened. For PDF documents, ITEXT also provides documentation display properties, which can control the display attribute of the Acrobat Reader when the SETVIEWERPREFERENCES method is called, if the single page is displayed, whether to hide the status bar, etc. In addition, IText also provides security protection for PDF files, with a STENCryption method for writing, which can set the user password, read-only, and printable properties of the document. Adding Documentates All content added to documents are in-objects, such as phrase, paragraph, table, graphic object, and more. Compare the paragraph objects, used to add a paragraph to the document. Fourth, text handling ITEXT processing text with text block (phrase) and paragraph). Text block is a minimum unit for processing text, with a string of string of format (including font, color, size). If the following code is to generate a font for Helvetica, a string with a downline: chunk chunk1 = new chunk ("this text is underlined", FontFactory.GetFont (FontFactory.helvetica, 12, font.underline);

The phrase (phrase) consists of one or more text blocks, the phrase (phrase) can also set fonts, but is invalid for the text blocks (CHUNK) in which the font is set. Through the phrase member function add, a text block is added to the phrase, such as: phrase6.add (chun); paragraph) by one or more text blocks (Chunk) or phrases ( Phrase, which is equivalent to the paragraph concept in the Word document, can also set the font size, color, etc. of the paragraph, color and other properties. Alternatively, the first line of segments can be set, aligned (left align, right alignment, and alignment). By function setAlignment, the alignment of paragraphs can be set. The parameter 1 of SetAlignment is aligned in the center, and 2 is right align, 3 is left align, and the default is left aligned. 5. Table Treatment ITEXT Processing Table as: com.lowagie.text.pdf.pdfptable, for relatively simple table processing, you can use com.lowagie.text.table, but if you want to process Complex form, this requires com.lowagie.text.pdf.pdfptable for processing. Here, you will explain the class com.lowagie.text.table. The constructor of class com.lowagie.text.Table has three: 1Table (int columns, int rows) 3Table (Properties Attributes) Parameters Columns, Rows, Attributes are columns, rows, table properties, respectively . You must specify the number of columns of the table when you create a table, and for the number of rows, you can use it. After establishing a table, you can set the properties of the table, such as: border width, border color, liner between padding space, and the like of the pitch of cells). By a simple example, the code is as follows:

1: Table Table = New Table (3); 2: Table.setBorderwidth (1); 3: Table.setBorderColor (New Color (0, 0, 255)); 4: Table.SetPadding (5); 5: Table. Setspacing (5); 6: Cell Cell = New Cell ("HEADER"); 7: Cell.SetHeader (TRUE); 8: Cell.SetColspan (3); 9: Table.Addcell (Cell); 10: Table.endHeaders (); 11: Cell = New Cell ("Example Cell with Colspan 1 and RowsPan 2"); 12: Cell.setrowsPan (2); 13: Cell.SetBorderColor (New Color (255, 0, 0)); 14: Table.addcell (Cell); 15: Table.Addcell ("1.1"); 16: Table.Addcell ("2.1"); 17: Table.Addcell ("1.2"); 18: Table.Addcell ("2.2") 19: Table.Addcell ("cell test1"); 20: Cell = new cell ("big cell"); 21: Cell.setrowsPan (2); 22: Cell.SetColspan (2); 23: Table.Addcell Cell); 24: Table.Addcell ("cell test2"); operation results are as follows:

Header Example Cell with Colspan 1 And RowsPan 2 1.12.1 1.22.2 Cell Test1big Cell Cell Test2

The code 1-5 is used to create a table, such as the code, established a table of 3 columns, and set the border width to 1, the color is blue, the bus distance is 5. The code 6-10 line is used to set the table of the table, the 7th line Cell.setHeader (TRUE); the cell is displayed as the header information; Chapter 8 Cell.SetColspan (3); specified the cell Take 3 columns; when adding head information to the table, it is important to note that once the header information is added, the endheaders () method must be called, such as the 10th line, otherwise the header information will not display again when the table is crosspage. . The code 11-14 is a cell that adds a width to a list, a cell that accounts for two lines. When adding a cell (Cell) to the table, press from top to right, from top to bottom. After the 11-line code is executed, the table of 2 lines 2 column appears in the lower right of the table. This is when adding a cell to the form, first filled this blank, then one line, 15-24 line code description this Add sequence. 6. Image Processing ITEXT Processing Table Class is com.lowagie.text.image, the image format supported by ITEXT supports: GIF, JPEG, PNG, WMF, etc. For different image formats, ITEXT is automatically Identify image format. An example of GIF, JPG, and PNG images are obtained by the following code.

Image gif = image.getinstance ("vonnegut.gif"); image jpeg = image.getInstance ("mykids.jpg"); image png = image.getInstance ("Hitchcock.png");

The position of the position image of the image is primarily referring to the positional relationship between the image, the image, and the location of the text in the document. ITEXT is processed by function public void setAlignment (int alignment), parameter alignment is image.right, image.middle, image.Left, center, center, left alignment; when the parameter alignment is image.textwrap, image.underlying Refers to the text winding pattern display, graphic as a background of text. These two parameters can be combined to achieve the desired effect, such as the effect of setAlignment (Image.right | Image.textWrap) is image right, and the text is displayed around the image. The size and rotation of the image If the image does not appear in the original size in the document, it can be set by the following function: public void scaleabsolute (int newwidth, int newheight) public void scalepercent (int init percent) public void scalepercent (int init percentx, int percenty

Function Public Void Scaleabsolute (int newwidth, int newheight) Directly sets the display size; function public void scalepercent (int init "Sets the display proportion, such as the scalepercent (50) indicates the size of the displayed size 50%; and the function scalepercent INT percentX, int initY) The image is high and wide. If the image is displayed in the document after the image needs to rotate, it can be set by the function public void setrotation (double R), and the parameter R is an arc, and if the rotation angle is 30 degrees, the parameter r = math.pi / 6. Seven, Chinese processing default ITEXT font settings do not support Chinese fonts, you need to download Far East Font Bag ITextasian.jar, otherwise you cannot output Chinese fonts in the PDF document. By the following code, you can use Chinese in the document:

Basefont bfchinese = basefont.createfont ("stsong-light", "unigb-ucs2-h", basefont.not_embedded; com.lowagie.text.font fontChinese = new com.lowagie.text.font (Bfchinese, 12, CoM. Lowagie.text.Font.Normal); Paragraph Pragraph = New Paragraph ("Hello", FontChinese;

Eight, the postmediate iText has many advanced features, which will not be introduced here, refer to the published documentation when developing. In general, IText is a set of components that make PDF in a Java environment. Because IText supports the development of JSP / JavaBean, this makes a good resolution in the B / S application. Since IText is not specifically designed to make reports, all reports, formats need to be implemented by writing code, relative to those professional support visual design, programming has a certain degree of increase.

转载请注明原文地址:https://www.9cbs.com/read-111742.html

New Post(0)