Mobile Zone is brought to you in partnership with:

Michael Crump is a Microsoft MVP, INETA Community Champion, and an author of several .NET Framework eBooks. He speaks at a variety of conferences and has written dozens of articles on .NET development. He currently works at Telerik with a focus on our XAML control suite. You can visit his blog at: MichaelCrump.Net or follow him on Twitter at: @mbcrump Michael is a DZone MVB and is not an employee of DZone and has posted 113 posts at DZone. You can read more from them at their website. View Full User Profile

Displaying PDF Files in Windows Phone 7 Mango

06.25.2011
| 14231 views |
  • submit to reddit

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”.

 

SNAGHTML49ac440

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" />
</Grid>

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:

// Constructor
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var resource = Application.GetResourceStream(new Uri("WP7PDFReader;component/C1XapOptimizer.pdf", UriKind.Relative));
this.c1PdfViewer1.LoadDocument(resource.Stream);

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!

References
Published at DZone with permission of Michael Crump, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)