ITEXT User Guide
---------- Use IText to output a PDF file.
Foreword
Today, the use of PDF format documents has become more common. It is very good in terms of document compression, security, etc. I will not say more about it. So how do you use Java language to develop applications to output a document in PDF format? The answer is IText, it is a project of a source code, you can use IText easy to implement the PDF output. This article I will introduce you how to use IText to generate a PDF document.
HelloWorld
As a program developer, it is not unfamiliar for the HelloWorld program, almost every language or application will always give you an example of HelloWorld to introduce you. When we start to introduce IText, we may wish to start from HelloWorld.
/ *
* CREATED ON 2004-1-3, create the first Hello World program
* /
Package test1;
Import java.io.filenotfoundexception;
Import java.io.fileoutputstream;
Import com.lowagie.text. *;
Import com.lowagie.text.pdf. *;
Public class helloworld {
Public static void main (String [] args) {
// Create a document object
Document doc = new document ();
Try {
/ / Define the output position and put the document object into the output object.
Pdfwriter.getInstance (DOC, New FileoutputStream ("C: / Hello.pdf"));
// Open the document object
Doc.open ();
/ / Add to text "Hello World"
Doc.add (New Paragraph ("HelloWorld");
// Close the document object, release resources
Doc.close ();
} catch (filenotfoundexception e) {
E.PrintStackTrace ();
} catch (documentexception e) {
E.PrintStackTrace ();
}
}
}
Now run the above code (remember to put itext.jar in your classpath before this), if everything is normal, you will see a file named hello.pdf in "C: /". Open this file and see what? Yes, there is a line of characters "HelloWorld" in the document, as shown below.
How is it simple? Of course, we can't just have a simple output of a string when practical application, but also make a lot of work, output more complex PDF, let us start further understanding other features of IText.
More complex settings
Analyze the Document constructor, we found that there are two in addition to the parameter construct in our previous example:
Public Document (); Public Document (Rectangle PageSize); Public Document (Rectangle PageSize, Int Marginleft, Int Marginright, Int Margintop, Int Marginbottom);
The first page size sets the document, the second, in addition to the page size of the setup document, also sets the page margin. Let me give an example below.
Rectangle psize = new Rectangle (144, 90);
// Document background color
Psize.setBackground; Color.Blue;
// Create a document object and set his initialization size
Document Doc = New Document (psize); Rectangle Psize = New Rectangle (144, 90);
// Document background color
Psize.setBackground; Color.Blue;
// Create a document object, set the initialization size and margin
Document Doc = New Document (psize, 5, 5, 5, 5);
Put the code in the first example and then modify the above method and then run, you can see the output PDF document will look like this, the document becomes small and the background is blue:
In the above example we set the size of the document through Rectangle, in fact IText has defined many commonly used pages, such as: A0-A10, Legal, Letter, etc., these are placed in com.lowagie.text.pageSize In this class, you can directly reference the page information by calling the static methods in PageSize. such as:
PageSize.a4;
Set font
Use IText to set the font of the text, how to display Chinese is the most important issue for our China's programmers. Fortunately, there is a special package in ITEXT to set up the fonts of Asian countries. You can download this package from http://itext.sourceforge.net/downloads/itextasian.jar. Then put it directly in your classpath. How to set fonts?
Basefont bfchinese = basefont.createfont ("stsong-light", "unigb-ucs2-h", basefont.not_embedded;
Font FontChinese = New Font (Bfchinese, 12, Font.Normal);
Set the display of Chinese fonts in the code above, you can pack Chinese plus PDF with the following code.
String title = "The latest movie: Matrix Revolution";
Paragraph T = New Paragraph (Title, FontChinese);
Doc.Add (t);
If you feel that this is very troublesome, huh, you have to extend its source code, set the font to the BaseFont.
Edit form
The table in ITEXT is very similar to the use of the table in HTML. However, it has a cell represents a lattice. Basically, the Table object in Table and Swing is consistent, such as the table in the top of the table:
/ / Define a form
Table Table = New Table (2);
/ / Set the table border
Table.setBorderwidth (1);
Cell Cell = New Cell ("Matrix III");
Cell.SetHeader (TRUE);
//categorize
Cell.SetColspan (2);
Cell.SetBackgroundColor (color.blue);
Table.Addcell (Cell);
Place a picture
Now you must know how to add a picture to the document, oleep as long as you declare an Image object, the image of Image and AWT is the same.
// Define a picture
Image jpeg = image.getInstance ("c: /matrix.jpg");
// Picture
JPEG.SETALIGNMENT (image.align_center);
The PDF file output after running is:
End
I will introduce these, and more in-depth things have experienced by the use of ITEXT. Of course, some of them may be wrong. Which friend has any suggestions, please contact me. Mail: WAFD@hotmail.com
Resources:
ITEXT site
http://www.lowagie.com
IText Chinese Show package
Http://itext.sourceforge.net/downloads/itextasian.jar.
Source code of this article