Footer Header utils: import nt; import Element; import ; import Text . When creating PDF documents, the first thing we usually do, is create a header and footer for every page. Adding an image to the header, helps. This page will provide the tutorial for how to add header and footer in every page of PDF using iText in java. iText provides.
|Published (Last):||3 September 2011|
|PDF File Size:||10.33 Mb|
|ePub File Size:||4.65 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:.
Adding page events to PdfWriter (iText 5)
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: The text should be put under the actual content of the page, as ni 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.
Page events for headers and footers
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 abd 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. Anr we discussed form XObjects in section 3. Otherwise the object is kept in memory until you close the Document. Headet 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 ifext of 30 pt x addibg 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. The third footef is special: 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 inn 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 addint 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: 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.
Adding Header and Footer in PDF using iText in Java – Memorynotfound
The main constructor of this class heade two parameters: 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 also discovered two other important objects: 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 un boundaries, you used a second series of page events to solve a number of common issues: Part 3 will teach you these essential iText skills.
Overview of the page boundaries iText 5. Adding page events to PdfWriter iText 5. We have already discussed seven methods of the PdfPageEvent interface; four more methods involving the document and its pages remain: