getAllChildren().get(0).getAllChildren(); StringBuffer stringBuffer = new StringBuffer(); for (PdfOutline bookmark: bookmarks) { showTitle(bookmark, stringBuffer); }. Tags: iText 7building blocksChapter 6tutorialactionsdestinationsBookmarks drawn by the PDF viewer that renders annotations on top of the existing content. Interactivity within PDF documents is enabled though Anchors (links) and Bookmarks. Following earlier articles in my iTextSharp series, this.

Author: Samukasa Malashura
Country: Sri Lanka
Language: English (Spanish)
Genre: Life
Published (Last): 22 March 2014
Pages: 407
PDF File Size: 13.36 Mb
ePub File Size: 4.9 Mb
ISBN: 490-4-59996-883-9
Downloads: 20632
Price: Free* [*Free Regsitration Required]
Uploader: Grole

We briefly mentioned that clickable areas are defined using Link annotationsand we referred to chapter 6 —this chapter— when we explained that createURI created only one of many types of actions.

Itextsahrp the examples that follow, we’ll bookmarrk some more types, and we’ll also learn about different types of destinations that can be used in a link. Finally, we’ll also use those actions and destinations to create outlines, better known as bookmarks. If bookmrk look at the AbstractAction class, you notice that it has a method named secAction.

When you use this method on a building block, you can define actions that will be triggered when clicking on its content. This is an alternative to using the Link object. The setAction method doesn’t make sense for every building block. Please consult the appendix to find out for which objects the setAction method can be used.

In the original example, we used a Link object so that you could jump to the corresponding IMDB page when clicking the title. The link on the first page is marked “Go to bookmwrk page”; the link on the last page is marked iteextsharp to first page”, and that’s exactly what the links do when you click them. We used named actions to achieve this; see the NamedAction example. The createNamed method accepts a PdfName as a parameter. You can boolmark one of the following values:.

PrevPage — the action allows you to jump to the previous page in the document. You could create these names yourself, for instance new PdfName “PrevPage”but it’s always better to use the names that are predefined in the PdfName class. However, any document using such a non-standard action isn’t portable.

These named actions allow us to navigate through a document, but they are rather limited, aren’t they? If we want to create a table of contents that allows us to jump to a specific page, we need a GoTo action.

Chapter 6: Creating actions, destinations, and bookmarks

If we’d click on a line, we’d jump to the corresponding page. To achieve this, we keep track of the titles and the page numbers on which these titles appear. Most of the code repeats what we’ve done before to render the TXT itextxharp to a PDF, but these are the new lines that interest us the most:.

The key is a String we’ll use for the title. They value is an Integer we’ll use for the page number. We get the current page number using the getNumberOfPage method.

We add a Paragraph saying “Table of Contents”. We use a DottedLine as the tab leader. We use the key of each entry as well as the corresponding value to construct a Paragraph with the title and the page number as content.

We also use the page number bookmak create a GoTo action that jumps to that specific page. We’ll take a closer look at these classes later on in this chapter. What’s more important right now, is that there are two problems with this example, one problem is worse than the other. The link jumps to another page in the document and shows this page in full.


A more elegant solution would be to jump to the start itextdharp the actual title. The link doesn’t always jump to the correct page.

We store the page number of the last page in the document at the moment we add the title. That’s the page number of the current page. However, we’re also using the setKeepWithNext method. This method forwards the title to a new page if the first paragraph of the chapter doesn’t fit the current page.

In that case, our TOC points at the wrong page, more specifically at the page just before the one we need. We’ll fix these two problems in the next example. Instead of an explicit destination, we’ll use named destinations for a change. The fact that the page numbers are now correct is the only visible difference.

Bookmark examples | iText Developers

The other difference is that we now used named destinations. We create those destinations by using the setDestination method. This method is defined in the ElementPropertyContainer and can be existinb on many building blocks see appendix.

The key is a String that we’ll use for a unique name.

The value is no longer a page number, but another SimpleEntry. The key of this second key-value pair will be the title of the chapter; the value will be the corresponding page number. We know that this page number will be wrong in some cases. We will use a custom ParagraphRenderer to update the page number. We pass the titlePage entry as a parameter so that the renderer can update the page number. This entry contains the unique name and another entry with the title and the page number.

Note that we define a destination named “toc” for that paragraph line We get the value of each entry line 42 to construct the content of each line in the table of contents: We make the line clickable by adding a GoTo action that jumps to a location in the document based on a name.

Internally, iText will map that name with a specific position —aka an explicit destination— in the document. Because of this, you can use the createGoTo method passing that name as a parameter to create a link to that specific building block.

How to import bookmark from XML to existing pdf?

We will even be able to use that name outside of the PDF document, but let’s take a look at the UpdatePageRenderer before we do so. The entry object contains a title and a page number. That page number could be wrong if the title is moved to the next page. We can only itextsnarp if that happens when the title paragraph itfxtsharp rendered. Only at that moment, a layout decision will be made. The easiest way to update the page number in the entry object, is to override the layout method as is done in line When we click on the first link, the PDF we created in the previous example is opened on the first bookmsrk in a new viewer window.

When we click on the second link, the same document is opened on the table of contents page in the current window, replacing the document with the two links. We use two Link objects to achieve this in the RemoteGoto example. The first parameter is the name of the file we created in the previous example.

We expect it to be in the same directory as the file we refer from. In line 5 and 6, we use another createGoToR method to create a link to a named destination in another document.


Dxisting second parameter is the name we used when we added the paragraph “Table of Contents”. The third parameter indicates that we want to open the document in the current PDF viewer window.

Add Page bookmark to an existing PDF using iTextSharp in C# – CodeProject

There are many other variations of the createGoToR method, but they are all similar to one of the two methods that were just explained.

There’s a short answer to this question: It is a common misconception that the boolean parameter indicating whether or not the PDF should be opened in the current window or in a new window, can also be used in the context of a browser.

This isn’t the case. There is a clear separation between the PDF viewer and the browser. The PDF viewer is usually a closed container that doesn’t have access to the browser functionality. Those are two separate technologies. Many methods, such as methods that communicate with a server, are restricted, but you also have some extra methods that are specific to PDF. We won’t go into detail regarding the JavaScript functionality in PDF, but we’ll create a simple PDF that shows an alert when you click a link; see figure 6.

We create the Link that allows us to trigger this alert in the JavaScript example. All of these other methods are out of scope in the context of this tutorial, but we’ll look at one more action example, the ChainedActions example. It explains how to chain actions. In line 1, we create the same JavaScript action as in the previous example.

We chain a remote GoTo action to this JavaScript action using the next method in line 2. Now when we click the word “here”a Boo alert will be triggered first; then another PDF will open in a new window. The createSubmitForm method is one of the many PdfAction methods we didn’t discuss. We mention it here because of a common use case for the next method.

It is not unusual to validate fields that were filled in manually before submitting a form. This validation could be done using JavaScript. The submit action could be the last action in edisting validation chain. While we were talking about actions, we mentioned the concept of destinations a couple of times. We also explained that links are actually annotations.

In the next couple bopkmark examples, we’ll spend some more time on these concepts. The PdfExplicitDestination class can be used to create a destination to a specific page, using specific coordinates if needed. That’s a great question, but the answer might require being read more than once. The PdfNamedDestination class is offered should you need it, but it is recommended that you use the PdfStringDestination class.

Using a PDF string as name is also the default way used by iText when you use the setDestination method. We’ll discover another way to create named destinations once we discuss bookmarks, but first, we’ll create a couple of explicit destinations in the ExplicitDestinations example. These links are added in lines, and respectively.