Using Java to generate PDF documents

Source: Internet
Author: User


Now the use of PDF documents has become more and more common, it is in the document compression, security and so on, the performance is very good, about its benefits I do not say here more. So how do you use the Java language Development application to output documents in PDF format? Here to introduce Itext, it is a source code development project, you can use itext easy to achieve the output of the PDF. In this article I will show you how to use Itext to generate PDF documents.

HelloWorld

As a program developer, the HelloWorld program is not unfamiliar, almost every language or application will always give a helloworld example to introduce to you. When we begin to introduce itext, we may as well start from HelloWorld







Now run the code above (remember to put Itext.jar in your classpath before that), and if it's all right, you'll see a file called 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 easy? Of course, we can not simply output a string in the actual application of the finished, but also to do a lot of work to output more complex PDF, below let us begin to learn more about Itext other features.

More complex settings

Analyzing the construction method of document, we find that there are two other than the parameterless constructs in our previous example:

/*

* Created on 2004-1-3, creating 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 location and load the document object into the output object

Pdfwriter.getinstance (Doc, New FileOutputStream ("C:/hello.pdf"));

Open Document Object

Doc.open ();

Add text "Hello World"

Doc.add (New Paragraph ("HelloWorld"));

Close document objects, freeing 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 sets the page size of the document, and the second sets the page margins in addition to the page size of the document. Let me give you an example.

Rectangle psize=new Rectangle (144,90);

Background color of the document

Psize.setbackgroundcolor (Color.Blue);

Create a Document object and set his initialization size

Document Doc=new document (psize);



Rectangle psize=new Rectangle (144,90);

Background color of the document

Psize.setbackgroundcolor (Color.Blue);



Create a Document object, set initialization size and margins

Document Doc=new document (psize,5,5,5,5);

Modify the code in the first example and then run it, and you can see that the output of the 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 a number of commonly used pages for us, such as: A0-A10, LEGAL, letter, etc. These are all placed in the Com.lowagie.text.PageSize class, where you can refer to the page information directly by calling the static method in PageSize. Like what:

pagesize.a4;



Set font

Using Itext to set the font of text, how to show Chinese to Chinese programmers is the most critical issue. Luckily Itext has a special package to set up Asian country fonts You can download this package from Http://itext.sourceforge.net/downloads/iTextAsian.jar. Then put it directly into your classpath. How do I set the font?



Basefont Bfchinese = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded);

Font Fontchinese = new Font (Bfchinese, font.normal);

In the above code to set the display of Chinese fonts, you can only use the following code to package Chinese into the PDF

String title = "I Like to drink coffee";

Paragraph t = new Paragraph (title, Fontchinese);

Doc.add (t);



If you think this setting is very troublesome, oh, then you have to expand its source code, set the font all in that basefont inside.




Edit Table

The table in Itext is very much like the use of tables in HTML but it has a cell that represents a lattice, and basically the table in here is consistent with the Table object in swing, such as the one in the previous code for the table:

Define a table

Table table = new Table (2);

Set Table borders

Table.setborderwidth (1);

Cell cell = new cell ("Matrix III");

Cell.setheader (TRUE);

Disaggregated

Cell.setcolspan (2);

Cell.setbackgroundcolor (Color.Blue);

Table.addcell (cell);





Place picture

Now you must know how to add a picture to the document, yes, just declare an image object, and the image used here is the same as the image usage in AWT.

Define a picture

Image jpeg = image.getinstance ("c:/matrix.jpg");

Center picture

Jpeg.setalignment (Image.align_center);

End

About the use of itext I would like to introduce these, more in-depth things only people refer to the source code of their own experience.






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.