Display PDFs in your Windows Phone 7 application

Display PDFs in your Windows Phone 7 application

I just downloaded and was experimenting with the CTP of ComponentOne Studio for Windows Phone. Normally I don’t give these third party control libraries much interest as they’re usually expensive and generally just add another dependency to your application that you have no control over. This is particularly true with mobile applications where performance is so critical.

Anyhow, the one thing that caught my eye in this control library is that there is a PDF Viewer control. There is full documentation available online, including details of this control. What interested me was the following:

View and Save PDF Files

The C1PdfViewer control can be used to view and save PDF files on the Windows Phone device. C1PdfViewer has no external dependency on Adobe Reader to view or save files. Content is parsed and rendered as native XAML elements.

That it awesome that they’re rendering the PDF as native XAML.

After downloading the CTP I was a little lost to start with as I couldn’t see the controls appear within Visual Studio. However they ship a cool Sample Explorer which I was able to use to play around with their samples.

image

After taking a look at the sample I decided to see how easy it was to create a simple application that displays a PDF. Rather than loading the PDF from a resource within the application which is what the sample demonstrates, I decided to load a test PDF located at  http://www.blhr.org/media/documents/test.pdf

The XAML for MainPage is:

<phone:PhoneApplicationPage
    x_Class=”PDFViewer.MainPage”
   
   
   
   
    Loaded=”MainPageLoaded”>

    <Grid>
            <c1Pdf:C1PdfViewer x_Name=”pdfViewer”
                               ViewMode=”FitWidth”
                               Visibility=”Collapsed” />
    </Grid>
</phone:PhoneApplicationPage>

And the code to load the PDF is quite simple:

public partial class MainPage : PhoneApplicationPage {
    public MainPage() {
        InitializeComponent();
    }
    private WebClient wc = new WebClient();
    private void MainPageLoaded(object sender, RoutedEventArgs e) {
        wc.OpenReadCompleted += WcOpenReadCompleted;
        wc.OpenReadAsync(new Uri(http://www.blhr.org/media/documents/test.pdf));
    }
    void WcOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
        pdfViewer.LoadDocument(e.Result);
        pdfViewer.Visibility = Visibility.Visible;
    }
}

Running this displays the pdf in the application:

image

Warning: There are some definite limitations with this control that are outlined both on the website and within the documentation. For example my first test PDF didn’t render (http://www.education.gov.yk.ca/pdf/pdf-test.pdf) throwing an exception relating to do with the compression format. The guidance is currently to only use this control for rendering PDFs that you have control over.

 

Leave a Reply

Your email address will not be published. Required fields are marked *