Use itext to operate PDF files

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

I wrote an article "Java reading PDF file content" some time ago. pig0045 replied to me, saying that I could use itext directly. I am very grateful because there were not many PDF files processed in the past, I don't know about this component. pig0045 introduced me to a new method. Thank you! I'm so glad that I immediately Googled an itext and found that itext is very powerful in generating PDF files, but it does not seem to involve much in reading PDF file content (maybe I did not find it, if you have different opinions, let's talk about 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, however, if you write the program again and record it in your own language, your understanding will be more profound.
Preparations: download the latest itextjarpackage at http://www.lowagie.com/itext/and put it in classpath.
1. It is very simple to create a PDF file with Hello worlditext. To demonstrate its simplicity, let's create a helloworld for you to experience.
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/helloworkflow "));
// Open the Document Object
Doc. open ();
// Add the text "Hello World"
Doc. Add (new paragraph ("helloworld "));
// Close the Document Object and release the resource
Doc. Close ();
} Catch (filenotfoundexception e )...{
E. printstacktrace ();
} Catch (incluentexception e )...{
E. printstacktrace ();
}

How is it? Is it easy? The above code is explained below.
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 page margins. If you want to define the page size and margins, 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 );
// Background color of the document
Psize. setbackgroundcolor (color. Blue );
// Create a Document Object and set the initialization size and margins
Document Doc = new document (psize, 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 );


3. Set the font itext to set the font, size, and color of the displayed text, as shown below:

// Add the text "Hello World", 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 the table in PDF and the table in HTML, but the cell is cell. The following Code adds a 2*2 table. The code is very simple and I will not explain it more.

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/helloworkflow "));
// Open the 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); // sets the cell boundary.
T. setborderwidth (1); // you can specify the 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 the Document Object and release the resource
Doc. Close ();
} Catch (filenotfoundexception e )...{
E. printstacktrace ();
} Catch (incluentexception e )...{
E. printstacktrace ();
}

5. Insert an image into the image is similar to that in swing. For details, refer:

Document Doc = new document ();
Image JPEG;
Try ...{
Using writer. getinstance (Doc, new fileoutputstream ("/root/helloworkflow "));
Doc. open ();
JPEG = image. getinstance ("/root/1.gif ");
// Center the 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 (incluentexception e )...{
E. printstacktrace ();
}

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

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.