Itext Study Notes

Source: Internet
Author: User
Tags border color image png processing text rfc adobe acrobat reader

 

Dynamically create PDF files in Web Applications

Original article: Sean C. Sullivan Translation: gagaghost
In a recent logistics project, the customer asked us to create a web site that allows users to query shipment information from a legacy system. There are three main requirements:
1. shipment information must be returned in pdf format;
2. pdf files must be downloaded in a browser;
3. pdf files must be read using Adobe Acrobat Reader;
Although our team has a lot of J2EE Web application development experience, we have little experience in PDF document processing. We need to find a pure Java class library that can generate complex PDF documents in Web applications on the server side. Finally, we found that itext (http://www.lowagie.com/iText/) can fully meet our needs.

1. itext class library

Itext is an open-source pure Java class library that creates and processes PDF documents. Bruno lowagie and Paulo Soares lead the project. The itext API allows Java developers to create PDF documents programmatically. Itext provides many features:

  • Support for PDF and FDF documents
  • Various page sizes
  • Horizontal and vertical layout
  • Margins
  • Table
  • Broken word
  • Page header
  • Footer
  • Page number
  • Bar Code
  • Font
  • Color
  • Document Encryption
  • JPEG, GIF, PNG, and WMF images
  • Ordered and unordered list
  • Shadow
  • Watermark
  • Document Template
    Itext is an open source library. When writing this article, itext can use Mozilla Public License and lgpl under two license protocols. For more information, see the itext website. In this article, you will see the itext API application. We will explain how to use itext and servlet to dynamically generate PDF documents in server applications.

    2. Start (Getting Started)

    First, you need an itext JAR file. Visit the itext site and download the latest version. When writing this article, the latest version is to make 0.99. The itext site provides API documentation and a comprehensive guide.
    In addition to itext, we also need to use servlet. If you are not familiar with Servlet, you can learn it through Jason Hunter's book Java Servlet programming. You need a J2EE application server or a servlet engine that can run independently. Open source software tomcat, jetty and JBoss are good choices. Assume that you are using Jakarta Tomcat 4.1.

    1. itext API

    The itext API is easy to use. By using itext, you can create custom PDF documents. The itext library consists of the following packages:
    Com. lowagie. servlets
    Com. lowagie. Text
    Com.lowagie.text.html
    Com. lowagie. Text. Markup
    Com.lowagie.text.pdf
    Com.lowagie.text.html. Codec
    Com.lowagie.text=. hyphenation
    Com.lowagie.text=. WMF
    Com. lowagie. Text. rtf
    Com. lowagie. Text. xml
    Com. lowagie. Tools
    To generate the authorization file, you only need two packages: com.lowagie.textand com.lowagie.text.pdf.
    These itext classes are used in our example:
    Com.lowagie.text.html. writable writer
    Com. lowagie. Text. Document
    Com. lowagie. Text. headerfooter
    Com. lowagie. Text. Paragraph
    Com. lowagie. Text. Phrase
    Com. lowagie. Text. Table
    Com. lowagie. Text. Cell
    The key classes are document and plain writer. These two classes are frequently used when creating PDF documents. Document is an object-based description of a PDF document. You can add content to the document by calling the methods provided by the document class. The upload writer object is associated with the document through the java. Io. outputstream object.

    3. Use itext in Web Applications

    In the design phase, you must decide how to use itext. We used the following technologies to develop our web applications.

    1. A Technology

    Create a PDF file on the server file system. The application uses Java. Io. fileoutputstream to write the file to the server file system. You can use the http get method to download the object.

    2. Skill B

    Use Java. Io. bytearrayoutputstream to create a PDF file in the memory. The application sends the PDF file byte to the client through the servlet output stream.
    Because the application does not need to write files to the file system, this can ensure normal operation in the cluster service environment, so I prefer to use B technology. If your application runs in a cluster and the server cluster does not provide session affinity, a technology may fail.

    3. Example: Servlet

    Our example application consists of a class: servlet. This servlet adopts the B technique. The output stream outputstream is Java. Io. bytearryoutputstream. Bytearrayoutputstream is used to store PDF file bytes in memory. When servlet receives an HTTP request, it dynamically generates a PDF file and sends it to the client.
    The servlet class extends the javax. servlet. http. httpservlet class and imports two itext packages: com.lowagie.textand com.lowagie.text.pdf.
    Doget Method
    Most servlets cover a method in the dopost and doget methods. Our servlet is no different. The javasservlet class overwrites the doget method. The servlet generates a PDF file after receiving an http get request.
    In the core part, the servlet doget method does the following:
    1. Create a bytearrayoutputstream object containing the PDF file bytes;
    2. Set the HTTP response header content on the reponse object;
    3. Get the servlet output stream;
    4. Write the document bytes to the servlet output stream;
    5. Refresh the servlet output stream;
    Generateappsdocumentbytes Method
    Generateacrodocumentbytes is used to create PDF documents. The three most important objects in this method are the Document Object, bytearrayoutputstream object, and javaswriter object. Pdfwriter uses bytearrayoutputstream to associate a document.
    Document Doc = new document ();
    Bytearrayoutputstream baospdf = new bytearrayoutputstream ();
    Specified writer docwriter = NULL;
    Docwriter = Specified writer. getinstance (Doc, baospdf );
    //...
    Add the content to the document using the add method.
    Doc. Add (new paragraph (
    "This document was created by a class named :"
    + This. getclass (). getname ()));

    Doc. Add (new paragraph (
    "This document was created on"
    + New java. util. Date ()));

    After adding the content, close the document and author writer objects.
    Doc. Close ();
    Docwriter. Close ();
    When the document is closed, the bytearrayoutputstream object is returned to the caller.
    Return baospdf;
    Bytearrayoutputstream contains all the bytes of the PDF document.
    HTTP Response Header
    In this application, we only focus on four HTTP Response Headers: Content-Type, content-disposition, Content-Length, and cache-control. If you have never used an HTTP header, refer to the HTTP 1.1 Standard.
    Studying the doget method in servlet, you will notice that it is important and subtle to set the HTTP Response Header before any data is written to the servlet output stream.
    Let's describe the meaning of each response header in more detail.
    Content-Type

    In servlet, httpservletresponse has a parameter indicating the content type contained in the response. For PDF files, the content type is application/pdf. If the servlet does not set the type, it is difficult for the web browser to decide how to process the file.
    Servlet uses the following code to set the content type:
    Resp. setcontenttype ("application/pdf ");
    Content-Disposition
    The content-Disposition Header is provided to the browser to determine the HTTP Response content. After the browser reads the header information, it can determine:

  • The HTTP response contains a file;
  • File name included in the response;
  • Whether the file is displayed in the main browser window or viewed by external applications;
    RFC 2183 provides a complete explanation of the content-Disposition Header.
    By appropriately setting the content-Disposition value, the servlet can indicate whether the browser displays the file embedded or treats it as an attachment.
    Example 1. Embedded display of a file
    Content-Disposition: inline; filename?foobar=
    Example 2. append a file to response
    Content-Disposition: attachment; filename?foobar=
    The following pseudo code illustrates how to set the header information:
    Public void doget (httpservletrequest req, httpservletresponse resp)
    {
    //...
    Resp. setheader (
    "Content-disposition ",
    "Inline; filename?foobar= ");
    //...
    }
    Cache-control
    Depending on the features of your application, you can make the browser cache or not cache the PDF file you are generating. Server applications can have multiple HTTP headers to control content caching. Below are some examples:
  • Cache-control: No-Cache
  • Cache-control: No-store
  • Cache-control: Must-revalidate
  • Cache-control: Max-age = 30
  • Pragma: No-Cache
  • Expires: 0
    For a comprehensive explanation of the cache-control header, see the HTTP 1.1 Standard.
    Servlet sets cache-control to max-age = 30. This header tells the browser that the Maximum Cache Time for this file is 30 seconds.
    Content-Length
    The Content-Length header must be set to a byte value in the PDF file. If Content-Length is not set correctly, the browser may not display the file correctly. The following is the sample code:
    Bytearrayoutputstream baos = getbytearrayoutputstream ();
    Resp. setcontentlength (baos. Size ());
    Send PDF documents to Web browsers
    Servlet sends the PDF file to the client by writing the byte stream to the servlet output stream. It calls the getoutputstream method of the httpservletresponse object to obtain the output stream. The getoutputstream method returns an object of the javax. servlet. servletoutputstream type.
    Servletoutputstream SOS;
    SOS = resp. getoutputstream ();
    Baos. writeto (SOS );
    SOS. Flush ();
    After writing all the data to the stream, call the flush () method to send all the bytes to the client.
    Packaging and deployment
    To run the volume servlet in Tomcat, You need to package the application in the war file. The itext JAR file (itext-0.99.jar) must be placed under the lib directory of the war file. If you forget to package the itext JAR file, the servlet will report a java. Lang. noclassdeffounderror error and stop running.
    Run the application
    After deploying the war file, you have prepared the test servlet. Jakarta Tomcat listens for requests on port 8080.
    Request http: // hostname: 8080/servlet/createpdf in the browser. Servlet will execute and return a PDF file in the browser.

    4. solutions other than itext

    Itext provides many underlying APIs for generating PDF documents. However, it is not effective for any application.
    In my daily work, I use itext in combination with Microsoft Word and Adobe Acrobat. First, our team designed a shipping form using Microsoft Word. Then, we use Acrobat to convert a Word document into a PDF document. Then, we use the itext template function to load the PDF file into our application. From here, it is quite easy to enter the data in the form and output the final PDF document.
    For reports-based Web applications such as jasperreports, it provides high-level abstraction than itext.

    5. Summary

    When your application needs to dynamically create PDF files, the itext class library is a good solution. You can enhance and expand the code in this article to experience the itext capability. Soon, thanks to the comprehensive PDF documentation, you will be impressed by your colleagues and customers.

    6. Other resources

    Http://www.lowagie.com/iText/
    Www.jpedal.org
    Www.w.box.org
    Xml.apache.org/fop
    HTTP 1.1 protocol specification
    RFC 2183
    Dmoz.org/computers/data_formats/document/publishing/pdf
    Www.planetpdf.com
    Www.mongozone.com

  •  

     

     

     

    PDF itext Introduction

     

    I. Preface
    In an enterprise's information system, report processing has always played an important role. This article introduces itext, a Java component used to generate PDF reports. By using JSP or JavaBean on the server side to generate a PDF report, the client displays or downloads the generated report through a super connection, which effectively solves the Report Processing Problem of the B/S system.

    II. Introduction to itext

    Itext is a well-known open source site, SourceForge, and a Java class library used to generate PDF documents. Using itext, you can not only generate PDF or RTF documents, but also convert XML and HTML files into PDF files.

    Itext installation is very convenient, download itext on the http://www.lowagie.com/iText/download.html-download website. after the JAR file, you only need to add itext. jar path, you can use the itext class library in the program.

    3. Create the first PDF document

    Five steps are required to generate a PDF document using itext:

    ① Create an instance of the COM. lowagie. Text. Document Object.
    Document document = new document ();

    ② Create a writer to associate with the document object. The writer can write the document to the disk.

    Using writer. getinstance (document, new fileoutputstream ("helloworld. pdf "));

    ③ Open the document.

    Document. open ();

    ④ Add content to the document.

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

    ⑤ Close the document.

    Document. Close ();

    The preceding five steps generate a helloworld. pdf file with the content "Hello World ".

    Create an instance of the COM. lowagie. Text. Document Object

    The com. lowagie. Text. Document Object has three construction functions:

    Public document ();
    Public document (rectangle pagesize );
    Public document (rectangle pagesize,
    Int marginleft,
    Int marginright,
    Int margintop,
    Int marginbottom );

    The pagesize parameter of the build function is the size of the document page. For the first build function, the page size is A4, the same as document (pagesize. a4). For the third build function, the parameters marginleft, marginright, margintop, and marginbottom are left, right, top, and bottom margins respectively.

    The pagesize parameter allows you to set attributes such as page size, surface background color, and page horizontal/vertical. Itext defines paper types such as A0-A10, Al, letter, halfletter, _ 11x17, ledger, note, B0-B5, ARCH_A-ARCH_E, FLSA and flse, you can also use rectangle pagesize = new rectangle (144,720); to customize the paper. You can use the rectangle method rotate () to set the page to horizontal.

    Writer object

    Once a document object is created, one or more writer objects need to be associated. Html. htmlwriter can save a document as an HTML file.

    Set document attributes

    Before opening a document, you can set attributes such as the title, topic, author, keyword, binding method, creator, producer, and creation date of the document. The called methods are as follows:

    Public Boolean addtitle (String title)
    Public Boolean addsubject (string subject)
    Public Boolean addkeywords (string keywords)
    Public Boolean addauthor (string author)
    Public Boolean addcreator (string creator)
    Public Boolean addproducer ()
    Public Boolean addcreationdate ()
    Public Boolean addheader (string name, string content)

    The addheader method is invalid for the PDF document. The addheader is only valid for the HTML document and is used to add the document header information.
    Before a new page is generated, you can set the page size, bookmarks, headerfooter, and other information. The call method is as follows:

    Public Boolean setpagesize (rectangle pagesize)
    Public Boolean add (watermark)
    Public void removewatermark ()
    Public void setheader (headerfooter header)
    Public void resetheader ()
    Public void setfooter (headerfooter footer)
    Public void resetfooter ()
    Public void resetpagecount ()
    Public void setpagecount (INT pagen)

    To set page properties on the first page, these methods must be called before the document is opened.

    For PDF documents, itext also provides the document display attributes. You can call the setviewerpreferences method of the writer to control the display attributes of Acrobat Reader when the document is opened, such as whether to display a single page, whether to display a full screen, and whether to hide status entries.

    In addition, itext provides security protection for PDF files. Using the setencryption method of writer, you can set the user password, read-only, printable, and other attributes of a document.

    Add document content

    All content added to the document is in the unit of objects, such as phrase, paragraph, table, and graphic objects. Commonly used is a paragraph (paragraph) object, used to add a paragraph of text to the document.
    Iv. Text Processing

    In itext, text is processed using text blocks (chunk), phrases (phrase), and paragraphs (paragraph.
    A chunk is the smallest unit for processing text. It consists of a string with a format (including font, color, and size. The following code generates a character string with a font of Helvetica, a size of 10, and an underscore:

    Chunk chunk1 = new chunk ("this text is underlined", fontfactory. getfont (fontfactory. Helvetica, 12, Font. Underline ));

    A phrase (phrase) is composed of one or more chunks. A phrase (phrase) can also set the font, but it is invalid for a chunk in which the font is set. You can use the phrase (phrase) member function add to add a text block (chunk) to the phrase (phrase), for example, phrase6.add (chunk );

    A paragraph (paragraph) consists of one or more chunks or phrases (phrase). It is equivalent to the Section concept in a Word document and can also set attributes such as the font size and color of a paragraph. You can also set the indent and alignment of the first line of a paragraph (left-aligned, right-aligned, and center-aligned ). You can use the setalignment function to set the paragraph alignment. The setalignment parameter 1 is centered, 2 is right, and 3 is left alignment. The default value is left alignment.

    V. Table Processing

    The class for processing tables in itext is com. lowagie. text. table and COM. lowagie. text. PDF. pdfptable. For simple table processing, you can use Com. lowagie. text. table, but if you want to process complex tables, this requires COM. lowagie. text. PDF. pdfptable. Com. lowagie. Text. Table is described here.

    The constructor class com. lowagie. Text. Table has three types:

    ① Table (INT columns)
    ② Table (INT columns, int rows)
    ③ Table (properties attributes)

    The columns, rows, and attributes parameters are the number of columns, number of rows, and table attributes of the table. When creating a table, you must specify the number of columns in the table. You do not need to specify the number of rows.

    After creating a table, you can set attributes of the table, such as the Border width, border color, and padding (padding space. The following code uses a simple example to describe how to use a table:

    1: Table = new Table (3 );
    2: Table. setborderwidth (1 );
    3: Table. setbordercolor (new color (0, 0,255 ));
    4: Table. setpadding (5 );
    5: Table. setspacing (5 );
    6: cell = new cell ("Header ");
    7: cell. setheader (true );
    8: cell. setcolspan (3 );
    9: Table. addcell (cell );
    10: Table. endheaders ();
    11: Cell = new cell ("example cell with colspan 1 and rowspan 2 ");
    12: cell. setrowspan (2 );
    13: cell. setbordercolor (new color (255, 0, 0 ));
    14: Table. addcell (cell );
    15: Table. addcell ("1.1 ");
    16: Table. addcell ("2.1 ");
    17: Table. addcell ("1.2 ");
    18: Table. addcell ("2.2 ");
    19: Table. addcell ("cell test1 ");
    20: Cell = new cell ("big cell ");
    21: cell. setrowspan (2 );
    22: cell. setcolspan (2 );
    23: Table. addcell (cell );
    24: Table. addcell ("cell Test2 ");

    The running result is as follows:

    Header
    Example cell with colspan 1 and rowspan 2 1.1 2.1
    1.2 2.2
    Cell test1 Big Cell
    Cell Test2

    Line 1-5 of the Code is used to create a new table. As shown in the code, a table with three columns is created, and the Border width is set to 1, the color is blue, and the padding is 5.

    Line 6-10 of the Code is used to set the table header and Line 4 of cell. setheader (true); displays the cell as the header information; 8th rows of cells. setcolspan (3); specify that the cell occupies three columns. When adding table header information, note that once the table header information is added, you must call the endheaders () method, for example, Row 3. Otherwise, the header information is not displayed after the table is displayed across pages.

    Line 11-14 of the Code adds a cell with a column width and a row length to the table.

    When a cell is added to a table, it is added from left to right and from top to bottom. For example, after executing 11 lines of code, there is a blank space in two rows and two columns in the lower right corner of the table. When adding cells to the table, fill in the blank space first, and then start another row, lines 15-24 indicate the order of adding.

    Vi. Image Processing

    The class for processing tables in itext is com. lowagie. text. currently, itext supports GIF, JPEG, PNG, and WMF formats. For different image formats, itext uses the same constructor to automatically recognize the image format. The following code obtains examples of GIF, JPG, and PNG images.

    Image GIF = image. getinstance ("vonnegut.gif ");
    Image JPEG = image. getinstance ("mykids.jpg ");
    Image PNG = image. getinstance ("hitchcock.png ");

    Image Position

    The image position mainly refers to the alignment of the image in the document, and the relationship between the image and the text. In itext, the public void setalignment (INT alignment) function is used for processing. The alignment parameter is image. right, image. middle, image. left indicates the right alignment, center, and left alignment. textwrap, image. underlying indicates that the text is displayed in a circular image and the image is displayed as the background of the text. These two parameters can be combined to achieve the expected effect. For example, the effect displayed by setalignment (image. Right | image. textwrap) is the right alignment of the image, and the text is displayed around the image.

    Image Size and Rotation

    If the image is not displayed in the original size in the document, you can use the following function to set it:

    Public void scaleabsolute (INT newwidth, int newheight)
    Public void scalepercent (INT percent)
    Public void scalepercent (INT percentx, int percenty)

    The public void scaleabsolute (INT newwidth, int newheight) function directly sets the display size. The public void scalepercent (INT percent) function sets the display proportion, such as scalepercent (50) indicates that the displayed size is 50% of the original size, while the function scalepercent (INT percentx, int percenty) indicates the display ratio of the image height and width.

    If the image needs to be rotated after a certain angle, you can use the public void setrotation (double r) function to set the parameter r to radian. If the rotation angle is 30 degrees, then the parameter r = math. PI/6.

    VII. Chinese Processing

    The default itext font setting does not support Chinese fonts. You need to download the Far East character package itextasian. Jar. Otherwise, you cannot output Chinese fonts to the PDF file. The following code can be used in Chinese:

    Basefont bfchinese = basefont. createfont ("stsong-light", "UniGB-UCS2-H", basefont. not_embedded );
    Com. lowagie. Text. Font fontchinese = new COM. lowagie. Text. Font (bfchinese, 12, Com. lowagie. Text. Font. Normal );
    Paragraph pragraph = new paragraph ("hello", fontchinese );

    8. Post Calculation

    Itext also has many advanced functions, which will not be described here. For details, refer to the released documents. In general, itext is a set of good PDF components in the Java environment. Because itext supports JSP/JavaBean development, the report problem in B/S applications can be well solved. Because itext is not designed specifically for report production, all the content and formats in the report must be implemented by writing code. Compared with professional report software that supports visual design, the programming workload increases to a certain extent.

     

     

    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.