Exclusive: Use itext jar to create PDF in Java

Source: Internet
Author: User

[Csdn.net compilation in April 3] The PDF generation function is very common in today's enterprise applications. However, using Java to implement this function is not so easy, Because Java does not provide the APIS required to process PDF files by default. But now with itext jar, it is not difficult to implement this function.

 

Itext is a free Java-PDF library that allows you to create PDF files on the fly (dynamic. Itext is an ideal choice for developers who need dynamic PDF document generation or operation functions to improve their applications. Itext is not a user terminal tool. That is to say, you don't need to build itext into your own program just like using Acrobat or other PDF tools, it can automatically generate and operate PDF files.

 

ItextHas the following features:

Transfer PDF files to your browser

Use XML files or databases to generate dynamic documents

Supports various PDF interaction Functions

Add bookmarks, page numbers, and watermarks

Split, connect to the pdf page, or perform operations such as pan scaling on them

Automatically fill in the PDF form

Add a digital watermark to a PDF file

 

System Requirements

JDK 1.4 or later

 

Obtain Method

Can download again here: http://www.lowagie.com/iText/download.html

Core File is: iText-2.1.5.jar

 

Use itextGenerate PDFSimple Example

Add itext. jar to your class path. Copy the code at the bottom to a file named generateapps.java, compile and execute the code, and create a file named testbench on drive C.

Import java. Io. file;

Import java. Io. fileoutputstream;

Import java. Io. outputstream;

Import java. util. date;

 

Import com. lowagie. Text. Document;

Import com. lowagie. Text. Paragraph;

Import com.lowagie.text=. writable writer;

 

Public class generatepdf {

 

Public static void main (string [] ARGs ){

Try {

Outputstream file = new fileoutputstream (new file ("C: // test.pdf "));

 

Document document = new document ();

Using writer. getinstance (document, file );

Document. open ();

Document. Add (new paragraph ("Hello Kiran "));

Document. Add (new paragraph (new date (). tostring ()));

 

Document. Close ();

File. Close ();

 

} Catch (exception e ){

 

E. printstacktrace ();

}

}

}

 

In the above Code, we created a document class to represent our PDF document. At the same time, we passed the outputstream object to outputstream through the getinstance () method. Therefore, in the above example, we created a file for output and imported the output to this file.

 

UseOutputstreamInHTTPGenerate in requestPDF

Sometimes we want to add the PDF generation function to the network application. You can click a button to save the PDF file to get the PDF file. Therefore, you need to dynamically create a PDF file and send it to the client browser.

 

The following code uses the struts action class to dynamically create a PDF file and send it to the browser.

 

Package net. viralpatel. Struts. helloworld. Action;

 

Import java. util. date;

 

Import javax. servlet. http. httpservletrequest;

Import javax. servlet. http. httpservletresponse;

 

Import org. Apache. Struts. action. Action;

Import org. Apache. Struts. Action. actionform;

Import org. Apache. Struts. Action. actionforward;

Import org. Apache. Struts. Action. actionmapping;

 

Import com. lowagie. Text. Document;

Import com. lowagie. Text. Paragraph;

Import com.lowagie.text=. writable writer;

 

/**

* @ Author kiranravi_hegde

*

*/

Public class extends helloworldaction extends action {

 

Public actionforward execute (actionmapping mapping, actionform form,

Httpservletrequest request, httpservletresponse response)

Throws exception {

 

Document document = new document ();

Try {

Response. setcontenttype ("application/pdf ");

Using writer. getinstance (document, response. getoutputstream ());

Document. open ();

Document. Add (new paragraph ("Hello Kiran "));

Document. Add (new paragraph (new date (). tostring ()));

} Catch (exception e ){

E. printstacktrace ();

}

Document. Close ();

Return NULL;

 

}

}

 

After analyzing the above Code, we will find that the response. getoutputstream () object is passed to the getinstance () method. The output document generated by itext is directly output by response. Do not forget to set content typeApplication/PDF

SetPDFAttributes

When generating a PDF file, you can also set different attributes, such as the author name, question, and file description. The following code generates some attributes:

 

Document. addauthor ("Kiran Hegde ");

Document. addcreationdate ();

Document. addcreator ("itext library ");

Document. addtitle ("Hello World pdf ");

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.