Chapter 7: Handling events; setting viewer preferences and printer properties

This book is meant for developers who want to create PDF documents from scratch in a programmatic way, using source code as opposed to using a template. We started with a chapter about fonts. In the chapters that followed, we discussed the default behavior of every element: Paragraph, Text, Image, and so on. We discovered that these elements can be used in a very intuitive way, but also that we can change their default behavior by creating custom renderer implementations –which isn't always trivial, depending on what you want to achieve. In the previous chapter, we discussed interactivity. We introduced actions and added links and bookmarks that help us navigate through a document.

We'll use this final chapter to introduce a couple of concepts that weren't discussed before. iText creates a new page automatically when elements don't fit the current page, but what if we want to add a watermark, background, header or footer to every page? How do we know when a new page is created? We'll need to look at the IEventHandler interface to find out. In the previous chapter, we changed a viewer preference so that the bookmarks panel is open by default. We'll look at some other viewer preferences that can be set. Finally, we'll learn how to change the settings of the PdfWriter, for instance to create a PDF of a version that is different from the default PDF version used by iText.

Implementing the IEventHandler interface

In previous examples, we used the rotate() method to switch a page from portrait to landscape. For instance, when we created PDF with tables in chapter 5, we created our Document object like this new Document(pdf, PageSize.A4.rotate()). In figure 7.1, we also see pages that are rotated, but they are rotated with a different purpose. In chapter 5, we wanted to take advantage of the fact that the width of the page is greater than the height when using landscape orientation. When using the rotate() method, it was our purpose to rotate the page, but not its content as is done in figure 7.1.

Figure 7.1: Pages with different orientations
Figure 7.1: Pages with different orientations

In the EventHandlers example, we create four A6 pages to which we add content as if the page is in portrait. We change the rotation of the page at the page level in an IEventHandler. As defined in the ISO standard for PDF, the rotation of a page needs to be a multiple of 90. This leaves us four possible orientations when we divide the rotation by 360: portrait (rotation 0), landscape (rotation 90), inverted portrait (rotation 180) and seascape (rotation 270).

  1. public static final PdfNumber PORTRAIT = new PdfNumber(0);
  2. public static final PdfNumber LANDSCAPE = new PdfNumber(90);
  3. public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180);
  4. public static final PdfNumber SEASCAPE = new PdfNumber(270);

We create a PageRotationEventHandler that allows us to change the rotation of a page, while we are creating a document.

  1. protected class PageRotationEventHandler implements IEventHandler {
  2. protected PdfNumber rotation = PORTRAIT;
  3. public void setRotation(PdfNumber orientation) {
  4. this.rotation = orientation;
  5. }
  6. @Override
  7. public void handleEvent(Event event) {
  8. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  9. docEvent.getPage().put(PdfName.Rotate, rotation);
  10. }
  11. }

The default orientation will be portrait (line 2), but we can change this default using the setRotation() method (line 4-6). We override the handleEvent() method that is triggered when an event occurs. We can get the PdfPage instance of the page on which the event is triggered from the PdfDocumentEvent. This PdfPage object represents the page dictionary. One of the possible entries of a page dictionary, is its rotation. We change this entry to the current value of rotation (line 9) every time the event is triggered.

The following snippet shows how we can introduce this event handler in the PDF creation process.

  1. public void createPdf(String dest) throws IOException {
  2. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  3. pdf.getCatalog().setPageLayout(PdfName.TwoColumnLeft);
  4. PageRotationEventHandler eventHandler =
  5. new PageRotationEventHandler();
  6. pdf.addEventHandler(
  7. PdfDocumentEvent.START_PAGE, eventHandler);
  8. Document document = new Document(pdf, PageSize.A8);
  9. document.add(new Paragraph("Dr. Jekyll"));
  10. eventHandler.setRotation(INVERTEDPORTRAIT);
  11. document.add(new AreaBreak());
  12. document.add(new Paragraph("Mr. Hyde"));
  13. eventHandler.setRotation(LANDSCAPE);
  14. document.add(new AreaBreak());
  15. document.add(new Paragraph("Dr. Jekyll"));
  16. eventHandler.setRotation(SEASCAPE);
  17. document.add(new AreaBreak());
  18. document.add(new Paragraph("Mr. Hyde"));
  19. document.close();
  20. }

We create an instance of the PageRotationEventHandler (line 4-5). We declare this eventHandler as an event that needs to be triggered every time a new page is started (PdfDocumentEvent.START_PAGE) in the PdfDocument (line 6-7). We create a PDF with tiny pages (line 8). We add a first paragraph (line 9) on a page that will use the default orientation. The START_PAGE event has already happened, when we change this default to inverted portrait (line 10). Only when a new page is created, after introducing a page break (line 11), the new orientation will become active. In this example, we repeat this a couple of times to demonstrate every possible page orientation.

There are four types of events that can be triggered:

  • START_PAGE– triggered when a new page is started,

  • END_PAGE– triggered right before a new page is started,

  • INSERT_PAGE– triggered when a page is inserted, and

  • REMOVE_PAGE– triggered when a page is removed.

We'll try all of these types in the next handful of examples.

Adding a background and text to every page

We have created many documents in which we rendered a novel by Robert Louis Stevenson to PDF. We reused the code of one of these examples to create the PDF shown in figure 7.2, and we introduced an event handler to create a lime-colored background for the odd pages and a blue-colored background for the even pages. Starting on page 2, we also added a running header with the title of the novel and a footer with the page number.

Figure 7.2: Colored background and running header
Figure 7.2: Colored background and running header

For this TextWatermark example, we added an END_PAGE event for a change.

  1. pdf.addEventHandler(
  2. PdfDocumentEvent.END_PAGE,
  3. new TextWatermark());

This choice for the END_PAGE event has an impact on the TextWatermark class.

  1. protected class TextWatermark implements IEventHandler {
  2. Color lime, blue;
  3. PdfFont helvetica;
  4. protected TextWatermark() throws IOException {
  5. helvetica = PdfFontFactory.createFont(FontConstants.HELVETICA);
  6. lime = new DeviceCmyk(0.208f, 0, 0.584f, 0);
  7. blue = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f);
  8. }
  9. @Override
  10. public void handleEvent(Event event) {
  11. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  12. PdfDocument pdf = docEvent.getDocument();
  13. PdfPage page = docEvent.getPage();
  14. int pageNumber = pdf.getPageNumber(page);
  15. Rectangle pageSize = page.getPageSize();
  16. PdfCanvas pdfCanvas = new PdfCanvas(
  17. page.newContentStreamBefore(), page.getResources(), pdf);
  18. pdfCanvas.saveState()
  19. .setFillColor(pageNumber % 2 == 1 ? lime : blue)
  20. .rectangle(pageSize.getLeft(), pageSize.getBottom(),
  21. pageSize.getWidth(), pageSize.getHeight())
  22. .fill().restoreState();
  23. if (pageNumber > 1) {
  24. pdfCanvas.beginText()
  25. .setFontAndSize(helvetica, 10)
  26. .moveText(pageSize.getWidth() / 2 - 120, pageSize.getTop() - 20)
  27. .showText("The Strange Case of Dr. Jekyll and Mr. Hyde")
  28. .moveText(120, -pageSize.getTop() + 40)
  29. .showText(String.valueOf(pageNumber))
  30. .endText();
  31. }
  32. pdfCanvas.release();
  33. }
  34. }

We create color objects (line 2) and a font (line 3) in the constructor (line 4-8), so that we can reuse these objects every time the event is triggered.

The PdfDocumentEvent (line 11) gives us access to the PdfDocument (line 12) and the PdfPage (line 13) on which the event was triggered. We get the current page number (line 14) and the page size (line 15) from the PdfPage. In this example, we will add all the content using low-level PDF functionality. We need a PdfCanvas object to do this (line 16-17). We draw the background using a rectangle() and fill() method (line 18-22). For pages with page number greater than 1 (line 23), We create a text object marked by beginText() and endText() with two snippets of text that are positioned using the moveText() method and added with showText() method (line 24-30).

As we add this content after the current page has been completed and right before a new page is created, we have to be careful not to overwrite already existing content. For instance: to create a colored background, we draw an opaque rectangle. If we do this after we have added content to the page, this content won't be visible anymore: it will be covered by the opaque rectangle. We can solve this by creating the PdfCanvas using the page.newContentStreamBefore() method. This will allow us to write PDF syntax to a content stream that will be parsed before the rest of the content of the page is parsed.

In iText 5, we used page events to add content when a specific event occurred. It was forbidden to add content in an onStartPage() event. One could only add content to a page using the onEndPage() method. This often led to confusion among developers who assumed that headers needed to be added in the onStartPage() method, whereas footers needed to be added in the onEndPage() method. Although this was a misconception, we fixed this problem anyway. Actually, in the case of this example, it would probably be a better idea to add the back ground in the START_PAGE event. We can use page.getLastContentStream() to create the content stream needed for the PdfCanvas object.

In the next example, we'll add a header using a START_PAGE event and a footer using an END_PAGE event. The footer will show the page number as well as the total number of pages.

Solving the "Page X of Y" problem

In figure 7.3, we see a running header that starts on page 2. We also see a footer formatted as "page X of Y" where X is the current page and Y the total number of pages.

Figure 7.3: Page X of Y footer
Figure 7.3: Page X of Y footer

The event handlers in the PageXofY example are added like this:

  1. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  2. pdf.addEventHandler(PdfDocumentEvent.START_PAGE,
  3. new Header("The Strange Case of Dr. Jekyll and Mr. Hyde"));
  4. PageXofY event = new PageXofY(pdf);
  5. pdf.addEventHandler(PdfDocumentEvent.END_PAGE, event);

Instead of using low-level PDF operators to create the text object, we use the showTextAligned() method that was introduced when we talked about the Canvas object. See for instance the handleEvent implementation of the Header class.

  1. protected class Header implements IEventHandler {
  2. String header;
  3. public Header(String header) {
  4. this.header = header;
  5. }
  6. @Override
  7. public void handleEvent(Event event) {
  8. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  9. PdfDocument pdf = docEvent.getDocument();
  10. PdfPage page = docEvent.getPage();
  11. if (pdf.getPageNumber(page) == 1) return;
  12. Rectangle pageSize = page.getPageSize();
  13. PdfCanvas pdfCanvas = new PdfCanvas(
  14. page.getLastContentStream(), page.getResources(), pdf);
  15. Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
  16. canvas.showTextAligned(header,
  17. pageSize.getWidth() / 2,
  18. pageSize.getTop() - 30, TextAlignment.CENTER);
  19. }
  20. }

This time, we use the getLastContentStream() method (line 14). As we use this class to create a START_PAGE event, the header will be the first thing that is written in the total content stream of the page.

The "Page X of Y" footer confronts us with a problem we've already solved once in chapter 2. In the JekyllHydeV8 example, we wanted to add the total number of pages of the document on the first page. However, at the moment we wrote that first page, we didn't know the total number of pages in advance. We used a placeholder instead of the final number, and we instructed iText not to flush any content to the OutputStream until all pages were created. At that moment, we used a TextRenderer to replace the place holder with the total number of pages, and we recreated the layout using the relayout() method.

There is one major disadvantage with this approach: it requires that we keep a lot of content in memory before we flush it to the OutputStream. The more pages, the more we'll risk an OutOfMemoryException. We can solve this problem by using a PdfFormXObject as placeholder.

A form XObject is a snippet of PDF syntax stored in a separate stream that is external to the content stream of a page. It can be referred to from different pages. If we create one form XObject as a placeholder, and we add it to multiple pages, we have to update it only once, and that change will be reflected on every page. We can update the content of a form XObject as long as it hasn't been written to the OutputStream. This is what we'll do in the PageXofY class.

  1. protected class PageXofY implements IEventHandler {
  2. protected PdfFormXObject placeholder;
  3. protected float side = 20;
  4. protected float x = 300;
  5. protected float y = 25;
  6. protected float space = 4.5f;
  7. protected float descent = 3;
  8. public PageXofY(PdfDocument pdf) {
  9. placeholder =
  10. new PdfFormXObject(new Rectangle(0, 0, side, side));
  11. }
  12. @Override
  13. public void handleEvent(Event event) {
  14. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  15. PdfDocument pdf = docEvent.getDocument();
  16. PdfPage page = docEvent.getPage();
  17. int pageNumber = pdf.getPageNumber(page);
  18. Rectangle pageSize = page.getPageSize();
  19. PdfCanvas pdfCanvas = new PdfCanvas(
  20. page.getLastContentStream(), page.getResources(), pdf);
  21. Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
  22. Paragraph p = new Paragraph()
  23. .add("Page ").add(String.valueOf(pageNumber)).add(" of");
  24. canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
  25. pdfCanvas.addXObject(placeholder, x + space, y - descent);
  26. pdfCanvas.release();
  27. }
  28. public void writeTotal(PdfDocument pdf) {
  29. Canvas canvas = new Canvas(placeholder, pdf);
  30. canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
  31. 0, descent, TextAlignment.LEFT);
  32. }
  33. }

We define a member-variable name placeholder in line 2, and we initialize this PdfFormXObject in the constructor of our IEventHandler implementation (line 9-10). The other member-variables in line 3-7 are there for our convenience. They reflect the dimension of the placeholder (side is the side of the square that defines the placeholder), the position of the footer (x and y), the space between the "Page X of" and "Y" part (space) of the footer, and the space we will allow under the baseline of the "Y" value (descent).

Lines 14 to 21 are identical to what we had in the Header class. We create the "Page X of" part of the footer in line 21 and 22. We add this Paragraph to the left of the coordinates x and y (line 24). We add the placeholder at the coordinates x + space and y - descent. We release the Canvas, but we don't release the placeholder yet. Once the complete document is generated, we call the writeTotal() method, right before we close the document.

  1. document.add(div);
  2. event.writeTotal(pdf);
  3. document.close();

In this writeTotal() method, we add the total number of pages at the coordinate x = 0; y = descent (line 30-31). This way, the "Page X of Y" text will always be nicely aligned with `x' and 'y' as the coordinate that has "Page X of" to the left and "Y" to the right.

Adding a transparent background image

In figure 7.4, we've added a transparent image in the background of each page of text. You could use this technique to add watermarks to a document.

Figure 7.4: Transparent background image
Figure 7.4: Transparent background image

Let's take a look at the TransparentImage class in the ImageWatermark example.

  1. protected class TransparentImage implements IEventHandler {
  2. protected PdfExtGState gState;
  3. protected Image img;
  4. public TransparentImage(Image img) {
  5. this.img = img;
  6. gState = new PdfExtGState().setFillOpacity(0.2f);
  7. }
  8. @Override
  9. public void handleEvent(Event event) {
  10. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  11. PdfDocument pdf = docEvent.getDocument();
  12. PdfPage page = docEvent.getPage();
  13. Rectangle pageSize = page.getPageSize();
  14. PdfCanvas pdfCanvas = new PdfCanvas(
  15. page.getLastContentStream(), page.getResources(), pdf);
  16. pdfCanvas.saveState().setExtGState(gState);
  17. Canvas canvas = new Canvas(pdfCanvas, pdf, page.getPageSize());
  18. canvas.add(img
  19. .scaleAbsolute(pageSize.getWidth(), pageSize.getHeight()));
  20. pdfCanvas.restoreState();
  21. pdfCanvas.release();
  22. }
  23. }

Note that we store the Image object as a member-variable; this way, we can use it as many times we want and the bytes of the image will be added to the PDF document only once.

Creating a new Image instance of the same image in the handleEvent would result in a bloated PDF document. The same image bytes would be added to the document as many times as there are pages. This was already explained in chapter 3.

We also reuse the PdfExtGState object. This is a graphics state object that is external to the content stream. We use it to set the fill opacity to 20%.

In this example, we use a mix of PdfCanvas and Canvas. We use PdfCanvas to save, change, and restore the graphics state. We use Canvas to add the image resized to the dimensions of the page.

In this example, we didn't want the background image to appear for the table of contents. See figure 7.5.

Figure 7.5: Removing a specific handler
Figure 7.5: Removing a specific handler

We achieve this by removing the event handler, right before we add the table of contents.

  1. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  2. Image img = new Image(ImageDataFactory.create(IMG));
  3. IEventHandler handler = new TransparentImage(img);
  4. pdf.addEventHandler(PdfDocumentEvent.START_PAGE, handler);
  5. Document document = new Document(pdf);
  6. ... // Code that adds the text of the novel
  7. pdf.removeEventHandler(
  8. PdfDocumentEvent.START_PAGE, handler);
  9. document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
  10. ... // code that adds the TOC
  11. document.close();

We can remove a specific handler, using the removeEventHandler() method. We can remove all handlers using the removeAllHandlers() method. That's what we're going to do in the next example.

Insert and remove page events

To obtain the PDF shown in figure 7.6, we took an existing PDF generated by one of the examples in the previous chapter. We inserted one page to be the new page 1. We removed all pages starting with the third chapter. As you can see, the bookmarks were updated accordingly.

Figure 7.6: Insert and remove page events
Figure 7.6: Insert and remove page events

The AddRemovePages example uses the INSERT_PAGE event to add content to the inserted page, and the REMOVE_PAGE method to write something to the System.out. At some point, we remove all handlers.

  1. public void manipulatePdf(String src, String dest) throws IOException {
  2. PdfReader reader = new PdfReader(src);
  3. PdfWriter writer = new PdfWriter(dest);
  4. PdfDocument pdf = new PdfDocument(reader, writer);
  5. pdf.addEventHandler(
  6. PdfDocumentEvent.INSERT_PAGE, new AddPageHandler());
  7. pdf.addEventHandler(
  8. PdfDocumentEvent.REMOVE_PAGE, new RemovePageHandler());
  9. pdf.addNewPage(1, PageSize.A4);
  10. int total = pdf.getNumberOfPages();
  11. for (int i = 9; i <= total; i++) {
  12. pdf.removePage(9);
  13. if (i == 12)
  14. pdf.removeAllHandlers();
  15. }
  16. pdf.close();
  17. }
  18. protected class AddPageHandler implements IEventHandler {
  19. @Override
  20. public void handleEvent(Event event) {
  21. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  22. PdfDocument pdf = docEvent.getDocument();
  23. PdfPage page = docEvent.getPage();
  24. PdfCanvas pdfCanvas = new PdfCanvas(page);
  25. Canvas canvas = new Canvas(pdfCanvas, pdf, page.getPageSize());
  26. canvas.add(new Paragraph().add(docEvent.getType()));
  27. }
  28. }
  29. protected class RemovePageHandler implements IEventHandler {
  30. @Override
  31. public void handleEvent(Event event) {
  32. PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  33. System.out.println(docEvent.getType());
  34. }
  35. }

In this example, we have an AddPageHandler (line 18-28) and a RemovePageHandler (line 29-35). We declare these handlers to the PdfDocument as INSERT_PAGE and REMOVE_PAGE event respectively (line 5-8). The AddPageHandler will be triggered only once, when we add a new page (line 9). The remove page will be triggered four times. We remove all pages from page 9 to the total number of pages. We do this by removing page 9 over and over again (line 12), until no pages are left. As soon as we've removed page 12, we remove all handlers (line 13-14), which means that the event is triggered after we removed pages 9, 10, 11, and 12.

In the next example, we're going to define page labels.

Page labels

Figure 7.7 shows a document with 38 pages. In the toolbar above the document, Adobe Acrobat shows that we're on page "i" or page 1 of 38. We have opened the Page Thumbnails panel to see a thumbnail for each page. We see that the first three pages are number i, ii, iii. Then we have 34 pages numbered from 1 to 34. Finally, we have a page with page label TOC.

Figure 7.7: Page labels
Figure 7.7: Page labels

These page labels aren't part of the actual content. For instance: you won't see them when you print the document. They are only visible in the PDF viewer –that is: if the PDF viewer supports page labels. The PDF in figure 7.7 was created using the PageLabels example.

  1. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  2. PdfPage page = pdf.addNewPage();
  3. page.setPageLabel(PageLabelNumberingStyleConstants
  4. .LOWERCASE_ROMAN_NUMERALS, null);
  5. Document document = new Document(pdf);
  6. document.add(new Paragraph().add("Page left blank intentionally"));
  7. ... // add some more pages left blank intentionally
  8. page = pdf.getLastPage();
  9. page.setPageLabel(PageLabelNumberingStyleConstants
  10. .DECIMAL_ARABIC_NUMERALS, null, 1);
  11. ... // add content of the novel
  12. document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
  13. p = new Paragraph().setFont(bold)
  14. .add("Table of Contents").setDestination("toc");
  15. document.add(p);
  16. page = pdf.getLastPage();
  17. page.setPageLabel(null, "TOC", 1);
  18. ... // add table of contents
  19. document.close();

We change the page label style three times in this code snippet:

  1. We create the first page in line 2 and we set the page label style for this page to LOWERCASE_ROMAN_NUMERALS. We don't define a prefix. All the pages that follow this first page will be numbered like this: i, ii, iii, iv, v,... until we change the page label style. This happens in line 9.

  2. In line 8, we get the last page that was added so far, and we change the page labels to DECIMAL_ARABIC_NUMERALS in line 9. Once more we don't define a prefix, and we tell the current page to start the page count with 1. We didn't really have to do this, because the page count always restart when you change the page labels. You can use this method if you don't want that to happen. For instance: we could pass 4 instead of 1 if we want the first page that follows the pages with Roman numerals to be page 4.

  3. In line 17, we change the page label style to null. This means that no numbering will be used, even if we pass a value for the first page after the page label change. In this case, we do pass a prefix. That's the page label we see when we reach the table of contents of our document. The prefix can be combined with a page number, for instance if you have Arabic numerals for page numbers and the prefix is "X-", then the pages will be numbered as "X-1", "X-2", "X-3", and so on.

In this example, we had to manually open the Page Thumbnails panel to see the thumbnail overview of all the pages. We could have instructed the document to open that panel by default. In the next example, we'll change the page display and the page mode.

Page display and page mode

The file page_mode_page_layout.pdf is almost identical to the file with the page labels we created in the previous example, but when we open it, we see that the panel with the page thumbnails is open by default. This is the page mode. We also see that the first page only takes half of the space that is available horizontally and that it's pushed to the right. At the bottom, we see that the second and third page are shown next to each other. This is the page layout.

Figure 7.8: Page layout and page mode
Figure 7.8: Page layout and page mode

The PageLayoutPageMode example is identical to the previous example, except for the following lines.

  1. pdf.getCatalog().setPageLayout(PdfName.TwoColumnRight);
  2. pdf.getCatalog().setPageMode(PdfName.UseThumbs);

We get the catalog from the PdfDocument. The catalog is also known as the root dictionary of the PDF file. It's the first object that is read when a parser reads a PDF document.

We can set the page layout for the document with the setPageLayout() method using one of the following parameters:

  • PdfName.SinglePage— Display one page at a time.

  • PdfName.OneColumn— Display the pages in one column.

  • PdfName.TwoColumnLeft— Display the pages in two columns, with the odd-numbered pages on the left.

  • PdfName.TwoColumnRight— Display the pages in two columns, with the odd-numbered pages on the right.

  • PdfName.TwoPageLeft— Display the pages two at a time, with the odd-numbered pages on the left.

  • PdfName.TwoPageRight— Display the pages two at a time, with the odd-numbered pages on the right.

We can set the page mode for the document with the setPageMode() method using one of the following parameters

  • PdfName.UseNone— No panel is visible by default.

  • PdfName.UseOutlines— The bookmarks panel is visible, showing the outline tree.

  • PdfName.UseThumbs— A panel with pages visualized as thumbnails is visible.

  • PdfName.FullScreen— The document is shown in full screen mode.

  • PdfName.UseOC— The panel with the optional content structure is open.

  • PdfName.UseAttachments— The attachments panel is visible.

We haven't discussed optional content yet, nor attachments. That's something we'll save for another tutorial.

When we use PdfName.FullScreen, the PDF will try to open in full screen mode. Many viewers won't do this without showing a warning first.

Figure 7.9: Warning before switching to full screen mode
Figure 7.9: Warning before switching to full screen mode

The warning shown in figure 7.9 was triggered by the PDF created with the FullScreen example.

  1. pdf.getCatalog().setPageMode(PdfName.FullScreen);
  2. PdfViewerPreferences preferences = new PdfViewerPreferences();
  3. preferences.setNonFullScreenPageMode(
  4. PdfViewerPreferencesConstants.USE_THUMBS);
  5. pdf.getCatalog().setViewerPreferences(preferences);

In this example, we also create a PdfViewerPreferences instance (line 2). We set the viewer preference that tells the viewer what to do when we exit full screen mode. The possible values for the setNonFullScreenPageMode() are:

  • PdfViewerPreferencesConstants.USE_NONE— No panel is opened when we return from full screen mode.

  • PdfViewerPreferencesConstants.USE_OUTLINES— The bookmarks panel is visible, showing the outline tree.

  • PdfViewerPreferencesConstants.USE_THUMBS— A panel with pages visualized as thumbnails is visible.

  • PdfViewerPreferencesConstants.USE_OC— The panel with the optional content structure is open.

We used PdfViewerPreferencesConstants.USE_THUMBS which means that we see the PDF as shown in figure 7.10.

Figure 7.10: Viewer after exiting full screen mode
Figure 7.10: Viewer after exiting full screen mode

Let's take a look at some other viewer preferences that are available in the PDF specification.

Viewer preferences

When we open the PDF shown in figure 7.11, we don't see a menu bar, we don't see a tool bar, we see the title of the document in the top bar, and so on.

Figure 7.11: Different viewer preferences at work in one document
Figure 7.11: Different viewer preferences at work in one document

The ViewerPreferences example shows us which viewer preferences have been set for this document.

  1. public void createPdf(String dest) throws IOException {
  2. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  3. PdfViewerPreferences preferences = new PdfViewerPreferences();
  4. preferences.setFitWindow(true);
  5. preferences.setHideMenubar(true);
  6. preferences.setHideToolbar(true);
  7. preferences.setHideWindowUI(true);
  8. preferences.setCenterWindow(true);
  9. preferences.setDisplayDocTitle(true);
  10. pdf.getCatalog().setViewerPreferences(preferences);
  11. PdfDocumentInfo info = pdf.getDocumentInfo();
  12. info.setTitle("A Strange Case");
  13. Document document = new Document(pdf, PageSize.A4.rotate());
  14. document.add(new Paragraph("Mr. Jekyl and Mr. Hyde"));
  15. document.close();
  16. }

All of these preferences expect true or false as a parameter; false being the default value if no preference is chosen.

  • Line 4: with setFitWindow(), we tell the viewer to resize the document's window to fit the size of the first displayed page.

  • Line 5: with setHideMenubar(), we tell the viewer to hide the menu bar; that is the bar with menu items such as File, Edit, View,...

  • Line 6: with setHideToolbar(), we tell the viewer to hide the tool bar; that is the bar with the icons that give us direct access to some features also available through the menu items.

  • Line 7: with setHideWindowUI(), we tell the viewer to hide all user interface elements such as scroll bars and other navigational controls.

  • Line 8: with setCenterWindow(), we tell the viewer to position the document's window in the center of the screen.

  • Line 9: with setDisplayDocTitle(), we tell the viewer to show the title of the document in the title bar.

Setting the title in the title bar requires that we define a title in the metadata. We do this in line 11-12. We'll have a closer look at metadata in a moment.

You can also use the PdfViewerPreferences class to define the predominant reading order of text using the setDirection() method, the view area using the setViewArea() and setViewClip() method. We won't do that in this tutorial, we'll skip to some printer preferences.

Printer preferences

The mechanism of viewer preferences can also be used to set some printer preferences. For instance: we can select the area that will be printed by default using the setPrintArea() and the setPrintClip() method. Specific printer settings can be selected using the setDuplex() and setPickTrayByPDFSize() method. You can select a default page range that needs to be printed using the setPrintPageRange() method.

Figure 7.12 shows the default settings in the Print Dialog after using the setPrintScaling() and setNumCopies() method.

Figure 7.12: Printer preferences
Figure 7.12: Printer preferences

The values in the screen shot correspond with the code of the PrinterPreferences example.

  1. PdfViewerPreferences preferences = new PdfViewerPreferences();
  2. preferences.setPrintScaling(
  3. PdfViewerPreferencesConstants.NONE);
  4. preferences.setNumCopies(5);
  5. pdf.getCatalog().setViewerPreferences(preferences);

Although PDF viewers nowadays offer many print-scaling options, the PDF specification only allows you to choose between NONE (no print scaling; the actual size of the document is preserved) and APP_DEFAULT (the default scaling of the viewer application). We set the number of copies to 5, which is reflected in the Print Dialog.

As you can see, setting a printer preference doesn't enforce the preference that is chosen. For instance, if we set the number of copies to 5, a user can easily change this to any other number in the dialog. In PDF 2.0, an extra viewer preference, named /Enforce will be introduced. Its value will be an array. A PDF 2.0 viewer should check the entries and enforce all the viewer preferences that are present in that array. The Draft specification only provides a way to enforce the print scaling so far. We haven't implemented this /Enforce preference in iText yet, but we'll do so as soon as the PDF 2.0 standard aka ISO-32000-2 is released.

Once in a while, we get the question if it's possible to set a viewer preference to open a document at a certain page. It's possible to jump to a specific page when opening a document, but that isn't achieved using a viewer preference. We need an open action to do this.

Open action and additional actions

The PDF document shown in figure 7.13 jumps straight to the last page when we open it. But there's more: when we leave the last page, we get a message saying "Goodbye last page!"

Figure 7.13: Document opens on last page and page says goodbye when we leave it
Figure 7.13: Document opens on last page and page says goodbye when we leave it

When we go to the first page, the document shows another alert: "This is where it starts!"

Figure 7.14: The first page says "This is where it starts!"
Figure 7.14: The first page says "This is where it starts!"

Finally, when we close the document, it says: "Thank you for reading".

Figure 7.15: The document says "Thank you for reading" upon closing it
Figure 7.15: The document says "Thank you for reading" upon closing it

The action that jumps to the last page is an open action; all the other actions are additional actions with respect to events triggered on the document or on a page. The Actions example shows us what it's all about.

  1. public void createPdf(String dest) throws IOException {
  2. PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  3. pdf.getCatalog().setOpenAction(
  4. PdfDestination.makeDestination(new PdfString("toc")));
  5. pdf.getCatalog().setAdditionalAction(PdfName.WC,
  6. PdfAction.createJavaScript("app.alert('Thank you for reading');"));
  7. pdf.addNewPage().setAdditionalAction(PdfName.O,
  8. PdfAction.createJavaScript("app.alert('This is where it starts!');"));
  9. Document document = new Document(pdf);
  10. PdfPage page = pdf.getLastPage();
  11. page.setAdditionalAction(PdfName.C,
  12. PdfAction.createJavaScript("app.alert('Goodbye last page!');"));
  13. document.close();
  14. }

Let's start with the open action (line 3-4). This action is added to the catalog using the setOpenAction() method. This method accepts an instance of the PdfDestination class –in this case a link to a named destination– or of the PdfAction class.

The next action is an additional action for the document (line 5-6). This action is also added to the catalog, using the setAdditionalAction() method. The second parameter has to be a PdfAction object. The first parameter is one of the following names:

  • PdfName.WC– which stands for Will Close. This action will be performed right before closing a document.

  • PdfName.WS– which stands for Will Save. This action will be performed right before saving a document. Note that this will only work for viewers that allow you to save a document; and that save isn't the same as save as in this context.

  • PdfName.DS– which stands for Did Save. This action will be performed right after saving a document. Note that this will only work for viewers that allow you to save a document; and that save isn't the same as save as in this context.

  • PdfName.WP– which stands for Will Print. This action will be performed right before printing a document.

  • PdfName.DP– which stands for Did Print. This action will be performed right after printing a document.

The next two additional actions are actions that are added to a PdfPage object (line 7-8; line 11-12). The parameters of this setAdditionalAction() method are again an instance of the PdfAction class as the second parameter, but the first parameter has to be one of the following names:

  • PdfName.O– the action will be performed when the page is opened, for instance when a user navigates to it from the next or previous page, or by clicking a link. If this page is the first page that opens when opening a document, and if there's also an open action, the open action will be triggered first.

  • PdfName.C– the action will be performed when the page is closed, for instance when a user navigates away from it by going to the next or previous page, or by clicking a link that moves away from this page.

There are more types of additional actions, especially in the context of interactive forms. Those actions are out of the scope of this tutorial and will be discussed in a tutorial about forms. We'll finish this chapter by looking at some writer properties.

Writer properties

In one of the previous examples, we added some metadata to a PdfDocumentInfo object. We obtained this object from the PdfDocument using the getDocumentInfo() method. This PdfDocumentInfo object corresponds with the info dictionary of the PDF; that's a dictionary containing metadata in the form of key-value pairs. This is how metadata was originally stored inside a PDF, but soon it turned out that it was a better idea to store metadata as XML inside a PDF file. This is shown in figure 7.16.

Figure 7.16: PDF and metadata
Figure 7.16: PDF and metadata

The XML is added as an uncompressed stream, which allows software that doesn't understand PDF syntax to extract the XML stream anyway and interpret it. The format of the XML is defined in the eXtensible Metadata Platform (XMP) standard. This standard allows much more flexibility than a simple key-value pair dictionary.

XMP metadata

When you create a document, you can create your own XMP metadata using the XMPMeta class and then add this metadata to the PdfDocument by passing it as a parameter with the setXmpMetadata() method. But you can also ask iText to create the metadata automatically, based on the entries in the info dictionary. That's what we did in the Metadata example.

  1. public void createPdf(String dest) throws IOException {
  2. PdfDocument pdf = new PdfDocument(
  3. new PdfWriter(dest,
  4. new WriterProperties()
  5. .addXmpMetadata()
  6. .setPdfVersion(PdfVersion.PDF_1_6)));
  7. PdfDocumentInfo info = pdf.getDocumentInfo();
  8. info.setTitle("The Strange Case of Dr. Jekyll and Mr. Hyde");
  9. info.setAuthor("Robert Louis Stevenson");
  10. info.setSubject("A novel");
  11. info.setKeywords("Dr. Jekyll, Mr. Hyde");
  12. info.setCreator("A simple tutorial example");
  13. Document document = new Document(pdf);
  14. document.add(new Paragraph("Mr. Jekyl and Mr. Hyde"));
  15. document.close();
  16. }

We create a WriterProperties object (line 4) that is used as a second parameter of the PdfWriter class (line 3). We use the addXmpMetadata() method (line 5) to instruct iText to create an XMP stream based on the metadata added to the PdfDocumentInfo object: the title (line 8), the author (line 9), the subject (line 10), the keywords (line 11), and the creator application (line 12). The producer, creation time and modification time are set automatically. You can't change them.

iText 5 generated PDF 1.4 files by default. In some cases, this version was changed automatically when you used specific functionality, For instance: when using full compression, the version was changed to PDF 1.5. Full compression means that the cross-reference table and possibly some indirect objects will be compressed. That wasn't possible in PDF 1.4. iText 7 creates PDF 1.7 files (ISO-32000-1) by default. In the previous example, we changed the version to PDF 1.6 using the setPdfVersion() method on the WriterProperties.

You can also change the compression in the WriterProperties.

Compression

In one of the event handler examples, we created a document with an image as background. The size of this PDF was 134 KB. In figure 7.17, you see another version of a document with the exact same content. The size of that PDF is only 125 KB.

Figure 7.17: PDF and compression
Figure 7.17: PDF and compression

This difference in size is caused by the way some content is stored inside the PDF. For small files, without many objects, the effect of full compression won't be significant. All content streams with PDF syntax are compressed by default by iText. Starting with PDF 1.5, more objects can be compressed, but that doesn't always make sense. A fully compressed file can count more bytes than an ordinary PDF 1.4 file if the PDF consists of only a dozen objects. The effect is more significant the more objects are needed in the PDF. If you plan to create large PDF files with many pages and many objects, you should take a look at the Compressed example.

  1. PdfDocument pdf = new PdfDocument(new PdfWriter(dest,
  2. new WriterProperties().setFullCompressionMode(true)));

Once again, we use the WriterProperties object, now in combination with the setFullCompressionMode() method. There's also a setCompressionLevel() method that allows you to set a compression level ranging from 0 (best speed) to 9 (best compression), or you can set it to the default value -1.

We'll conclude this chapter with a small encryption example.

Encryption

There are two ways to encrypt a PDF file. You can encrypt a PDF file using the public key of a public/private key pair. In that case, the PDF can only be viewed by the person who has access to the corresponding private key. This is very secure.

Using passwords is another way to encrypt a file. You can define two passwords for a PDF file: an owner password and a user password.

If a PDF is encrypted with an owner password, the PDF can be opened and viewed without that password, but some permissions can be in place. In theory, only the person who knows the owner password can change the permissions.

The concept of protecting a document using only an owner password is flawed. Many tools, including iText, can remove an owner password if there's no user password in place.

If a PDF is also encrypted with a user password, the PDF can't be opened without a password. Figure 7.18 shows what happens when you try to open such a file.

Figure 7.18: A PDF that requires a password
Figure 7.18: A PDF that requires a password

The document only opens when we pass one of the two passwords, the user password in which case the permissions will be in place, or the owner password in which case we can change the permissions.

Figure 7.19: A secured PDF
Figure 7.19: A secured PDF

As we can see in the Encrypted example, the passwords and the permissions were defined using WriterProperties.

  1. byte[] user = "It's Hyde".getBytes();
  2. byte[] owner = "abcdefg".getBytes();
  3. PdfDocument pdf = new PdfDocument(new PdfWriter(dest,
  4. new WriterProperties().setStandardEncryption(user, owner,
  5. EncryptionConstants.ALLOW_PRINTING
  6. | EncryptionConstants.ALLOW_ASSEMBLY,
  7. EncryptionConstants.ENCRYPTION_AES_256)));

We define a user password and an owner password as a byte array (line 1-2). Those are the first two parameters of the setStandardEncryption() method. The third parameter can be used to define permissions. In our example, we allow printing and document assembly –that is: splitting and merging. Finally, we define the encryption algorithm: AES 256.

The possible values for the permissions are:

  • ALLOW_DEGRADED_PRINTING– Allow printing at a low resolution only,

  • ALLOW_PRINTING– Allow printing at a low as well as at high resolution,

  • ALLOW_SCREENREADERS– Allows the extraction of text for accessibility purposes,

  • ALLOW_COPY– Allow copy/paste of text and images,

  • ALLOW_FILL_IN– Allow filling out interactive form fields,

  • ALLOW_MODIFY_ANNOTATIONS– Allow the modification of text annotations and filling out interactive form fields,

  • ALLOW_ASSEMBLY– Allow the insertion, rotation and deletion of pages, as well as the creation of outline items and thumbnail images,

  • ALLOW_MODIFY_CONTENTS– Allow the modification of the document by operations other than those controlled by ALLOW_FILL_IN, ALLOW_MODIFY_ANNOTATIONS, and ALLOW_ASSEMBLY.

If you want to combine different permissions, always use the "or" (|) operator because some permissions overlap. For instance ALLOW_PRINTING sets the bit for printing as well as for degraded printing.

iText supports the following encryption algorithms, to be used for the fourth parameter:

  • STANDARD_ENCRYPTION_40– encrypt using the 40-bit alleged RC4 (ARC4) algorithm,

  • STANDARD_ENCRYPTION_128– encrypt using the 128-bit alleged RC4 (ARC4) algorithm.

  • AES_128– encrypt using the 128-bit AES algorithm,

  • AES_256– encrypt using the 256-bit AES algorithm.

You can also add one of the following extra parameters to the encryption algorithm using an "or" (|) operation:

  • DO_NOT_ENCRYPT_METADATA– if you want to avoid that the metadata will also be encrypted. Encrypting the metadata doesn't make sense if you want the metadata to be accessible for your document management system, but be aware that this option is ignored when using 40-bit ARC encryption.

  • EMBEDDED_FILES_ONLY– if you want to encrypt the embedded files only, and not the actual PDF document. For instance: if the PDF document itself is a wrapper for other documents, such as a cover note explaining that it's not possible to open the document without having the right credentials at hand immediately. In this case the PDF is an unencrypted wrapper for encrypted documents.

All of these parameters can also be used for the setPublicKeyEncryption() method, in which case the first parameter is an array of Certificate objects, the second parameter an array with the corresponding permissions, and the third parameter the encryption mode –that is: the encryption algorithm and the option to not encrypt the metadata and to encrypt only the embedded files.

With this last example, we have given away the punch line of The Strange Case of Dr. Jekyll and Mr. Hyde: "Dr. Jekyll has a secret: he changes into Mr. Hyde." But you probably already knew that after all the Jekyll and Hyde examples we've made in this book.

Summary

In this final chapter of the "iText 7: Building Blocks", we have covered the IEventHandler functionality that allows us to take action when specific events –such as starting, ending, inserting, and removing a page– occur. We looked at viewer preferences that allowed us to tell PDF viewers how to present the document when we open it. We could also use the mechanism of viewer preferences to set some printer preferences. Finally, we looked at some writer properties. We discussed these properties in the context of metadata, compression, and encryption.

We've covered a lot of ground in this tutorial. You should now have a clear understanding of the basic building blocks that are available when you want to create a document from scratch. You also know how to establish document interactivity (actions) and navigation (links, destinations, bookmarks). You know the mechanism to handle events, and you can set viewer preferences and writer properties. You're all set to create some really cool PDFs.

Obviously, this is not the definitive guide on iText. In future tutorials, we'll also take a look under the hood, examining the PDF syntax that is used to create the content stream of a page and the structure of a PDF document. We'll spend a tutorial on forms, how to create them and how to use forms as templates. We'll create a tutorial on manipulating existing documents by adding and removing content. We'll also update the old tutorial on digital signatures. We don't have an ETA on those tutorials, but make sure to check our Books page on a regular basis.

THE END