pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation
PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance

PDF Content Extraction
PDF Navigation

PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2014-04-13

Working with built-in navigation in PDF files (Bookmarks, C# sample)

Paragraph 12.3 of the PDF specification describes available document-level navigation objects that can be used inside the PDF file to allow quick jumps between articles, paragraphs, or sections of the document. These objects may or may not be present in PDF file, and handling them requires a right tool.

We wrapped up this long story in our Apitron.PDF.Rasterizer.Navigation namespace.
The core object that handles all navigation tasks is DocumentNavigator class, it can be obtained by using the Navigator property of the created Document object:

[C#]

Document pdfDocument = new Document(inputStream);
DocumentNavigator navigator = pdfDocument.Navigator;

Using this navigator object you may navigate between pages of the document by passing page objects, bookmarks, links and search results - objects defined or found in PDF document. When navigation occurs, the navigator’s CurrentPage property contains the page that passed object refers to. So it’s becomes easier to track and render the “current page” instead of implementing own navigation mechanism.


It’s also possible to navigate back and forward using corresponding member functions, and because the navigator class also raises a Navigated event it’s easy to handle and reflect the current page change. It might be extremely useful for custom viewers, thus we used it a lot in our viewer app for Windows Phone as well as in our free Windows Forms PDF Viewer control.

The code below demonstrates simple navigation and corresponding event handling task for the PDF document:1
  1.          We load a PDF document first into a class-level field
  2.          Subscribe to navigator’s Navigated event
  3.         Now we can call MoveForward member from anywhere and handle the navigation in our DocumentNavigationHandler

[C#]

public void LoadDocument(string pathToPdfFile)
{
    using (FileStream inputStream = new FileStream(pathToPdfFile, FileMode.Open))
    {
        // this object represents a PDF document
        pdfDocument = new Document(inputStream);

        // subscribe to Navigator's events
        pdfDocument.Navigator.Navigated += DocumentNavigationHandler;
    }
}
  

private void DocumentNavigationHandler(object sender, NavigatedEventArgs eventargs)
{
    // get the navigator and print current page index
    DocumentNavigator navigator = (DocumentNavigator)sender;

    Console.WriteLine(string.Format("Navigated to page: {0}", navigator.CurrentIndex));
}

public void MoveForward(Document pdfDocument)
{
    // just move forward
    if (pdfDocument != null)
    {
        pdfDocument.Navigator.MoveForward();
    }
}

Handling PDF bookmarks 

A nice wpf viewer sample post describes in details how to work with bookmarks in PDF file, but in short - if you want to enumerate document’s bookmarks just use its Bookmarks property. Every Bookmark object then can be navigated using document navigator by using its GoToBookmark method.

[C#]
public void GoToFirstBookmark()
{
    // navigate to first bookmark in bookmarks collection
    pdfDocument.Navigator.GoToBookmark(pdfDocument.Bookmarks.Children[0]);
}

Handling PDF search results 

In our previous blogpost we showed how to use classes from Apitron.PDF.Rasterizer.Search namespace to perform search tasks for PDF documents.  That sample just saved found pages to bitmaps, but what if you want to navigate within a document using the search results?

It’s dead simple. Just use the GoToSearchResult member of the document navigator and pass the SearchResultItem that you’ve got by doing the search. See the code below.

[C#]
private void DoSearch(SearchIndex pdfSearchIndex)
{
    // just invoke the search and pass our search handler
    pdfSearchIndex.Search(MyPDFSearchHandler, "apitron");
}

private void MyPDFSearchHandler(SearchHandlerArgs handlerArgs)
{
    // navigate to first found item in our pdf document
    if(handlerArgs.ResultItems.Count > 0)
    {
        pdfDocument.Navigator.GoToSearchResult(handlerArgs.ResultItems[0]);

        // indicate that search should be stopped
        handlerArgs.CancelSearch = true;
    }
}

Supported platforms

The API described works on all supported platforms, including Android and iOS (using Xamarin), Windows Phone, WinRT. You can read more about the Apitron.PDF.Rasterizer for .NET component here.


No comments:

Post a Comment