---------- Use IText to output a PDF file.
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? Here you will introduce IText, which is an item of a source code, you can use iText to make the output of PDF. 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 also wish to start from HelloWorld
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:
/ *
* 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 ();
}
}
}
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 = "I love to drink coffee";
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.