Displaying PDF Files in Windows Phone 7 Mango
Several people have asked me if it is possible to display a PDF File in a Windows Phone 7 application after they saw me do in a Silverlight application. The answer is yes and it is a lot easier than you might expect. Let’s get started.
Head over to ComponentOne’s WP7 Controls site and click “Download CTP” from the right side.
Note: The download is completely FREE.
After you download it, then double click the installer and walk through the next few screens.
Once it is installed then fire up Visual Studio 2010 and select “New Project” then “Silverlight for Windows Phone” –> “Windows Phone Application”
You can give it a name of something like “WP7PDFReader” if you wish.
On the next screen, you should leave it at 7.1 (which is Mango)
Once Visual Studio 2010 loads then you will want to switch to your design surface and look for C1PdfViewer in your toolbox. If you don’t see it then you can easily add it to your Toolbox by right clicking and selecting “Choose Items…”
Look under Windows Phone Components and select C1PdfViewer and select “OK”.
You are going to want to click and drag the C1PdfViewer to your MainPage.xaml ContentPanel.
You can clean up the margins and Vertical/Horizontal Alignments if you wish to make it fill the page. My xaml looks like the following:
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<my:C1PdfViewer Name="c1PdfViewer1" />
I’m going to add a PDF file to my project by right clicking the project and selecting “Add Existing Item”.
Please note that you do not have to add a PDF to your project you could download it from a URL.
I am now going to add it as a “Resource” in the properties as shown below:
Now DoubleClick on the MainPage.xaml.cs and add the following code snippet:
Loaded += new RoutedEventHandler(MainPage_Loaded);
void MainPage_Loaded(object sender, RoutedEventArgs e)
var resource = Application.GetResourceStream(new Uri("WP7PDFReader;component/C1XapOptimizer.pdf", UriKind.Relative));
Now go ahead and launch the application and you should see the PDF inside your Windows Phone 7 Application!
Now you can:
Tap and drag your finger to move the location of the PDF file or Double-tap on the PDF file to zoom in or out.
Thanks for reading and be sure to follow me on Twitter or subscribe to my blog for more Silverlight/WP7/SharePoint 2010 information!
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)