Introduction
While developing Silverlight business applications you often have to deal with PDF documents and present these documents to users of your app in some way. That means you have to create your own, or purchase a Silverlight PDF Viewer control capable of handling large PDF documents and it also should make you able to do it in the restricted environment where Silverlight apps live. With the latest Silverlight release you may create trusted apps, but it’s not always the case, and most of the apps continue to run as untrusted on users’ side.
What Trusted vs. Untrusted actually means for PDF viewer? PDF is mostly about text and its representation, and for correct viewing you need fonts. Untrusted apps can’t access fonts installed on user’s system, and therefore they have to solve this problem somehow.
Apitron PDF components for Silverlight can work in all trust modes, making you able to create, process and render PDF documents in a truly platform independent way if needed. They provide API for font resolving and have fallback mechanisms which you can use in untrusted mode to provide you own fonts for document processing.
Apitron PDF Viewer control for Silverlight we are going to cover in this article is a simple control aiming to help you implement PDF preview in your Silverlight apps. It’s based on Apitron PDF Rasterizer.NET component, and is free to use if you have an application license for Apitron PDF Rasterizer. Get it as Nuget package or download from our website, it’s very extensible and you can upgrade it yourself, integrating search, bookmarks and other features coming with the Apitron PDF Rasterizer API it’s based on (we can provide you with the sources if needed).
Sample project
Download Apitron PDF Controls package from our website (link). Go to samples folder and open PDFControlsSilverlightSampleApp solution. This sample shows how to use Apitron PDF Viewer control for Silverlight.
Pic. 1 Sample Silverlight app that uses
Apitron PDF Viewer control
|
The gray toolbar and the area below the “Open PDF File…” button represent the PDF viewer control. Once the app is loaded you can set the currently displayed document using its Document property. You can navigate between pages directly using built-in arrows, or programmatically using document’s DocumentNavigator property.
If you have used Apitron PDF Rasterizer before, you might be already familiar with this navigation approach. If not, you can read about it in this blog post. Current page text box can also navigate you to the desired page, just type the number in and press enter. The viewer also features zooming and “fit-to-width” display mode, and once we publish sources on GitHub, you’ll be able to modify it as you wish by adding any required functionality.
Here is the code for “Open File…” click handler, showing how to load PDF document and set it for preview.
private void OnOpenFile(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*.pdf | *.pdf";
bool? dialogResult = openFileDialog.ShowDialog();
if (dialogResult!=null && dialogResult==true)
{
pdfViewer.Document = new Document(openFileDialog.File.OpenRead());
}
}
The complete sample also contains the code for registering own fonts for non-trusted mode. See the image below, it demonstrates the app opened in browser:
Pic. 2 Apitron PDF Viewer control usage
sample (running application)
|
Conclusion
In this article we have shown how one may use Apitron PDF Viewer Silverlight control for PDF preview implementation. But it’s only the part of the story. You actually can create your own custom viewer controls based on the rendering engine it employs – the Apitron PDF Rasterizer library.
It’s provides you with an ability to render PDF documents, search and highlight text, work with bookmarks and links for all supported platforms. You may use it for creation of Windows apps (mobile, desktop, web), iOS and Android apps (via Xamarin), Mono apps, and apps running on every system .NET CLR implementation exists for. And if you need to create and process PDF documents, we have Apitron PDF Kit for .NET - ready to use and powerful PDF manipulation library.
Contact us if you have any questions, need help, or just want to learn more.
Downloadable version of this article can be found by the following link [PDF].
Downloadable version of this article can be found by the following link [PDF].
No comments:
Post a Comment