MovieHistory2.java in Java

Generate QR-Code in Java MovieHistory2.java

Listing 5.19 MovieHistory2.java
Quick Response Code Printer In Java
Using Barcode generator for Java Control to generate, create QR Code image in Java applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Recognizer In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
class HeaderFooter extends PdfPageEventHelper { Phrase[] header = new Phrase[2]; int pagenumber; public void onOpenDocument(PdfWriter writer, Document document) { header[0] = new Phrase("Movie history"); } public void on(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { header[1] = new Phrase(title.getContent()); pagenumber = 1; } public void onStartPage(PdfWriter writer, Document document) { pagenumber++; } public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); switch(writer.getPageNumber() % 2) { case 0: ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header[0], rect.getRight(), rect.getTop(), 0); break; case 1:
Code 3 Of 9 Creator In Java
Using Barcode encoder for Java Control to generate, create Code 39 Extended image in Java applications.
www.OnBarcode.com
Generating UPC Symbol In Java
Using Barcode encoder for Java Control to generate, create UPC A image in Java applications.
www.OnBarcode.com
Adds header for even pages
QR Code ISO/IEC18004 Drawer In Java
Using Barcode printer for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
Barcode Generation In Java
Using Barcode generation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Table, cell, and page events
2D Creator In Java
Using Barcode creation for Java Control to generate, create 2D Barcode image in Java applications.
www.OnBarcode.com
UPC-E Supplement 5 Drawer In Java
Using Barcode maker for Java Control to generate, create UPC - E0 image in Java applications.
www.OnBarcode.com
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header[1], rect.getLeft(), rect.getTop(), 0); break; } ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase( String.format("page %d", pagenumber)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0); } }
Encode QR Code JIS X 0510 In None
Using Barcode encoder for Microsoft Word Control to generate, create QR Code ISO/IEC18004 image in Word applications.
www.OnBarcode.com
Read Quick Response Code In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Adds header for odd pages
Barcode Scanner In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Scan Data Matrix 2d Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Adds footer with page number
Encoding EAN / UCC - 13 In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create European Article Number 13 image in ASP.NET applications.
www.OnBarcode.com
Make EAN13 In None
Using Barcode printer for Word Control to generate, create EAN 13 image in Microsoft Word applications.
www.OnBarcode.com
There are no surprises in this code sample. You define two member variables:
Make PDF417 In None
Using Barcode drawer for Software Control to generate, create PDF 417 image in Software applications.
www.OnBarcode.com
Code 39 Full ASCII Scanner In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
header An array with two Phrase objects. One is set in onOpenDocument(), and it s valid for the full document. The other varies depending on the current chapter. It s set in the on() method. pagenumber A custom page number that is reset to 1 every time a new chapter starts. It s augmented in the onStartPage() method.
USS Code 128 Creator In Java
Using Barcode creation for BIRT reports Control to generate, create Code 128B image in BIRT applications.
www.OnBarcode.com
Code 39 Extended Printer In None
Using Barcode generation for Office Word Control to generate, create Code 39 Extended image in Word applications.
www.OnBarcode.com
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.2. 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. This will only work if you ve defined that specific page boundary between steps 2 and 3 in the PDF creation process. Otherwise, the getBoxSize() method will return null. Why is it not advised to add content in the onStartPage() method You ll remember from section 5.2.4 that iText ignores newPage() calls when the current page is empty. This method is executed or ignored when you call it explicitly from your code, but it s also invoked implicitly from within iText on multiple occasions. It s important that it s ignored for empty pages; otherwise you d end up with plenty of unwanted new pages that are unintentionally left blank. If you add content in an onStartPage() method, there s always a risk of having unwanted pages. Consider it more safe to reserve the onEndPage() method for adding content.
Printing Barcode In .NET
Using Barcode creation for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
UPC - 13 Decoder In Visual Basic .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
In the next example, you ll put the page number in the header, and you ll add the total number of pages.
Solving the page X of Y problem
An example of a page X of Y header is shown in figure 5.12. Retrieving the value of X is easy. You have access to the PdfWriter object in the onEndPage() method, so you can get the page number with getPageNumber(). But how can you retrieve the value for Y There s no way of knowing the total number of pages when the headers for the first pages are written. You only know the value of Y for sure when iText has finished writing the last page.
Adding page events to PdfWriter
Solving the page
X of Y problem with page events
There are two ways to solve this problem. One solution will be discussed in the next chapter. 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.4.2, I explained that iText only writes a PdfTemplate to the OutputStream when you explicitly use the releaseTemplate() method. 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.
Copyright © OnBarcode.com . All rights reserved.