[Reprint] Using itext to operate PDF files

Source: Internet
Author: User
Use itext to operate PDF files

I wrote an article some time agoArticlePig0045 replied to Java's PDF file content, saying that you can use itext directly. I am very grateful because there were not many PDF files processed in the past and I didn't know about this component, pig0045 introduced me to a new method. Thank you! I'm so glad to hear that Google's itext is very powerful in generating PDF files, and does not seem to involve much in reading PDF files (Maybe I haven't found it. If you have any different points of view, let's discuss it.).
At the same time, I also checked the relevant components of the Java field for PDF, and found that there are many open-source stuff that can be used to operate pdf. Well, it's good. We will give a rough introduction to the following: xpdf, itext, and product_box. If you are interested, you can check them by yourself.
Okay. After returning to the topic of the article and checking itext, I also read the related APIs and made some simple demos. Although there are many introductions on the internet, but write it again.ProgramRecord it in your own language to make a deeper understanding.
Preparations: Download the latest itextjarpackage at http://www.lowagie.com/itext/and put it in classpath.
I. Hello WorldIt is very simple to create a PDF file in itext. To demonstrate its simplicity, let's create a helloworld to let everyone experience it.
In this PDF file, we only display a sentence "Hello world". The implementation method is as follows:

// Create a Document Object
Document Doc =   New Document ();
Try   ... {
// Define the output location and load the document object into the output object
Using writer. getinstance (Doc, New Fileoutputstream ( " /Root/hellow.gov.cn " ));
// Open Document Object
Doc. open ();
// Add the text "Hello World"
Doc. Add ( New Paragraph ( " Helloworld " ));
// Close document objects and release resources
Doc. Close ();
}   Catch (Filenotfoundexception E) ... {
E. printstacktrace ();
}   Catch (Effecentexception E) ... {
E. printstacktrace ();
}

How is it? Is it easy? Next weCodeFor more information.
Document is the first class to be used to create a PDF file. Once a document is created and the content to be written into the PDF file, a writer is required, and the writer is such a writer. Paragraph indicates an indented section. Run the above program and we will see the hello.pdf file in the/rootdirectory.

However, in actual work, we cannot do such a simple job. The following describes common operations.

2. Set the marginsIf you want to define the page size and margin, you can use the document constructor:

Public Document ();

Public Document (rectangle pagesize );

Public Document (rectangle pagesize,
Int Marginleft,
Int Marginright,
Int Margintop,
Int Marginbottom );

As follows:

Rectangle psize = New Rectangle ( 144 , 90 );
// Document background color
Psize. setbackgroundcolor (color. Blue );
// Create a Document Object and set the initialization size and margins
Document Doc = New Document (psize, 5 , 5 , 5 , 5 );

In the above example we set the document size through rectangle, in fact, itext has defined for us many common pages, such as: A0-A10, we can directly call, as follows:

Document Doc = New Document (pagesize. A4, 5 , 5 , 5 , 5 );


3. Set the fontItext allows you to set the font, size, and color of the displayed text, as follows:

// Add the text "Hello World" and set the font to bold, the size to 20, and the color to red.
Doc. Add ( New Paragraph ( " Helloworld " , Fontfactory. getfont (
Fontfactory. courier, 20 , Font. Bold, New Color ( 255 , 0 , 0 ))));

To implement Chinese support, we need to go to the idea (I tried several times and could not download it, so I won't talk about it here ).

4. edit a tableThe table in PDF is similar to the table in HTML, but its cell is cell. The following Code adds a 2*2 table. The code is very simple and I will not explain it much.

Document Doc =   New Document ();
Try   ... {
// Define the output location and load the document object into the output object
Using writer. getinstance (Doc, New Fileoutputstream ( " /Root/hellow.gov.cn " ));
// Open Document Object
Doc. open ();
Table t =   New Table ( 2 , 2 );
T. setbordercolor ( New Color ( 0 , 0 , 255 )); // Set the border color to blue
T. setpadding ( 5 );
T. setspacing ( 5 ); // Set cell boundaries
T. setborderwidth ( 1 ); // Set Border Width
Cell C1 =   New Cell ( " Header1 " );
C1.setheader ( True );
T. addcell (C1 );
C1 =   New Cell ( " Header2 " );
T. addcell (C1 );
T. endheaders ();
T. addcell ( " 1.1 " );
T. addcell ( " 1.2 " );
Doc. Add (t );
// Close document objects and release resources
Doc. Close ();
}   Catch (Filenotfoundexception E) ... {
E. printstacktrace ();
}   Catch (Effecentexception E) ... {
E. printstacktrace ();
}

5. Insert an imageInserting images is similar to inserting images in swing. For details, refer:

Document Doc =   New Document ();
Image JPEG;
Try   ... {
Using writer. getinstance (Doc, New Fileoutputstream ( " /Root/hellow.gov.cn " ));
Doc. open ();
JPEG = Image. getinstance ( " /Root/1.gif " );
// Center Image
JPEG. setalignment (image. align_center );
Doc. Add (JPEG );
Doc. Close ();
}   Catch (Badelementexception E) ... {
E. printstacktrace ();
}   Catch (Malformedurlexception E) ... {
E. printstacktrace ();
}   Catch (Ioexception E) ... {
E. printstacktrace ();
}   Catch (Effecentexception E) ... {
E. printstacktrace ();
}

Now, I have finished introducing it. You are welcome to make a brick.

From: http://blog.csdn.net/hbcui1984/archive/2007/06/05/1638843.aspx

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.