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.Navigation
namespace 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).
The sources are also available at codeplex.
The sources are also available at codeplex.
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 Document
class, we wrapped it with a custom view model implementing INotifyPropertyChanged
interface. See DocumentViewModel class
in our sample.
Access to document bookmarks is provided by Document.Bookmarks
property, while navigation through the document has been implemented using DocumentNavigator
class.
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>
public
ImageSource RenderPageReferencedByBookmark(Bookmark bookmark)
{
BitmapSource
bitmapSource = null;
if
( document.Navigator.GoToBookmark(bookmark) )
{
Page
page = document.Navigator.CurrentPage;
int
imageWidth = (int)page.Width;
int
imageHeight = (int)page.Height;
byte[]
image = page.RenderAsBytes(imageWidth, imageHeight, new
RenderingSettings());
bitmapSource = BitmapSource.Create( imageWidth, imageHeight, 72, 72, PixelFormats.Bgr32, null,
image, 4 * imageWidth );
}
return
bitmapSource;
}
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.
|
Impressive! Is it possible to implement copy/paste functionality ?
ReplyDeleteHello, yes, but we didn't release the corresponding product yet. Sign up for our newsletter or follow us on twitter and we'll keep you updated.
Delete