Quantcast
Channel: PDF tips & tricks
Viewing all articles
Browse latest Browse all 125

PDF viewer sample, free WPF application with bookmarks support

$
0
0
     In Apitron PDF Rasterizer for .NET 3.5.0 we’ve added support for bookmarks that many documents contain to simplify navigation. Using classes from  Apitron.PDF.Rasterizer.Navigationnamespace you may easily find and render pages linked by bookmarks.

    To show this functionality in action and to help you get familiar with it, we have created a sample C# WPF application that demonstrates usage patterns and provides you with code snippets which you may use in your own applications. It also can be a good base for your custom viewer if you wish to create one, showing yet another way to convert pdf file to images.

    Download component package, unzip it, and open the \Samples\WpfPdfViewer folder. You are ready to check the app out. Open the sample project, run it and select the file to open(File->Open).


Here are a few screenshots of the actual app (with PDF specification loaded):

Pic.1 Folded bookmarks tree

Pic.2 Unfolded bookmarks tree
You can navigate either by using Pages tab or Bookmarks tab, zoom in and out, scroll etc.

The code 

    In order to get WPF  databinding working with our Documentclass, we wrapped it with a custom view model implementing INotifyPropertyChangedinterface. See DocumentViewModel class in our sample. 

    Access to document bookmarks is provided by Document.Bookmarksproperty, while navigation through the document has been implemented using DocumentNavigatorclass.


You may use DocumentNavigator class as follows:


      1)       go to page referenced by bookmark and render it
///<summary>
/// Renders the page referenced by bookmark as an <see cref="ImageSource"/>
///</summary>
///<param name="bookmark">The bookmark.</param>
///<returns>Image source that represents current page, otherwise null.</returns>
publicImageSource RenderPageReferencedByBookmark(Bookmark bookmark)
{
BitmapSourcebitmapSource = null;

if( document.Navigator.GoToBookmark(bookmark) )
{
Pagepage = document.Navigator.CurrentPage;

intimageWidth = (int)page.Width;
intimageHeight = (int)page.Height;

byte[] image = page.RenderAsBytes(imageWidth, imageHeight, newRenderingSettings());

bitmapSource = BitmapSource.Create( imageWidth, imageHeight, 72, 72, PixelFormats.Bgr32, null, image, 4 * imageWidth );
}

returnbitmapSource;
}


      2)       use its MoveForward, MoveBackward, Move, GoToPage and respond to Navigated event


      3)       retrieve its state by checking CurrentPage or CurrentIndex properties



    Another nice thing that you may get out of the sample is a TreeView template that was used for bookmarks tree.

     <TreeView Background="LightGray"Name="Bookmarks"ItemsSource="{Binding Bookmark.Children}"SelectedItemChanged="OnBookmarkSelectionChanged">
               <TreeView.ItemTemplate>
                  <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                     <TextBlock Text="{Binding Title}"/>
                  </HierarchicalDataTemplate>
               </TreeView.ItemTemplate>
     </TreeView>


         If you have any questions or suggestions regarding this or other code samples feel free to contact us or leave a comment here.






Viewing all articles
Browse latest Browse all 125

Trending Articles