We have already discussed seven methods of the PdfPageEvent interface; four more methods involving the document and its pages remain:. This is a good place to initialize variables that will be needed throughout the document. Use this method for initializing variables or for setting parameters that are page-specific. Do not use this method to add content. This is the best place to add a header, a footer, a watermark, and so on. This is the ideal place for any finalizations and to release resources if necessary.
|Published (Last):||7 July 2008|
|PDF File Size:||15.36 Mb|
|ePub File Size:||16.36 Mb|
|Price:||Free* [*Free Regsitration Required]|
We have already discussed seven methods of the PdfPageEvent interface; four more methods involving the document and its pages remain:. This is a good place to initialize variables that will be needed throughout the document. Use this method for initializing variables or for setting parameters that are page-specific.
Do not use this method to add content. This is the best place to add a header, a footer, a watermark, and so on. This is the ideal place for any finalizations and to release resources if necessary. For instance, how can you add a page header while creating a PDF document. This event, an instance of the HeaderFooter class, will add a header and a footer to the document as shown in figure 5. First, take a look at the footer : you want to add page numbers that start with "page 1" every time a new topic begins.
The text should be put under the actual content of the page, as a centered String. As for the header, you want it to alternate between the topic title aligned to the left, and the String "Movie history" aligned to the right. The following implementation of the PdfPageEvent interface meets these requirements. These values are important primarily for the PDF consumer. The other varies depending on the current topic.
No content is added in the page event until a page has been completed. The header and footer are written to the direct content in the onEndPage method. The parameters writer and document are to be used in the same way as done in section 5. Note that you ask the writer for the art box rectangle using the getBoxSize method.
You use this rectangle to position the header and the footer. Otherwise, the getBoxSize method will return null. Consider it more safe to reserve the onEndPage method for adding content. Retrieving the value of X is easy. But how can you retrieve the value for Y? You only know the value of Y for sure when iText has finished writing the last page.
There are two ways to solve this problem. One solution will be discussed in the next topic. It involves creating the PDF in two passes. You add the content in the first pass and the header or footer in a second pass. The other solution involves a PdfTemplate object and page events. When we discussed form XObjects in section 3. Otherwise the object is kept in memory until you close the Document. This opens possibilities: you can add a template to page 1, and wait until the final page to write content to this template.
Even if the content stream of the first page has already been sent to the OutputStream, the content added to the template afterwards will still be shown on the first page. When the document is opened, you create a template with a size of 30 pt x 16 pt O.
This time, you use a table with one row and three columns to draw the header. In the first cell, you add the text for the header. This name is set using the setHeader setter method. In the second cell, you add "page X of " where X is the value returned by writer. NOTE When the document is closed, the newPage method is triggered to perform finalizattions on the current page. When newPage is called, the page number is augmented, so you need to use writer.
In the previous example, you added a header and footer with the showTextAligned method. You can define a bottom border for each cell so that the header is underlined. This is the most elegant way to add headers and footers, because the table mechanism allows you to position and align lines, images, and text. The next example extends the previous one. The main difference is one extra feature, demonstrated in figure 5. The code to create this document is almost identical to the code used in the previous example.
You only need to add one extra page event, the Watermark class. If your watermark is an image, you have options: you can add it with the PdfContent-Byte. If you create the Image object in onStartPage or onEnd-Page , your PDF will become bloated: you risk adding the same byte sequence over and over again.
This will cost you not only in performance, but also in file size. When you read a PDF document on screen, you usually hit a key, click a button, or use a scrollbar to go to the next page. But you can also let the viewer go to the next page automatically after a number of seconds, define a transition, or both. If no duration is defined, user input is expected to go to the next page.
The main constructor of this class takes two parameters: a transition type and a value for the duration of the transition. The default duration of a transition is 1 second. This is a nice example showing how onStartPage can be used to set page parameters, because you need to set the transition and duration for every page.
You created several "Hello World" examples that demonstrated the famous "five steps" that were used in every example of part 1. Two topics dealt with building blocks offered by iText, allowing you to create PDF documents using high-level concepts.
Topic 3 explained how to add content at a lower level: you added lines, shapes, and text to different direct content layers. You also discovered two other important objects: using the ColumnText object, you added high-level objects at absolute positions; with PdfTemplate you learned how to reuse content as an XObject.
Then you learned how to use the PdfPageEvent interface. Initially you added custom features to Chunk, Paragraph, topic, and Section objects. After an intermezzo about pagination, involving reordering pages, adding blank pages, and defining page boundaries, you used a second series of page events to solve a number of common issues: adding headers and footers, adding "page X of Y" to every page, adding watermarks, and even defining a duration and a transition for each page.
Part 3 will teach you these essential iText skills. Previous post: Overview of the page boundaries iText 5. Adding page events to PdfWriter iText 5.
Adding Header and Footer in PDF using iText in Java
When creating PDF documents , the first thing we usually do, is create a header and footer for every page. At the bottom of the page we can optionally include a copyright symbol followed by some text. In the bottom right corner you can find the current page number , followed by the total number of pages. In this tutorial we demonstrate how to add a header and footer in a PDF document using iText. The com. By extending from this class and overriding these methods, we can write additional data to the PDF document when these events occur. Here you can see the result.
Add Header and Footer in PDF Using iText in Java
Subscribe to RSS