Retrieving PDF File in Windows Phone 7


Introduction

In this article we are going to explore how to retrieve PDF files in Windows Phone 7. We will also see see how it is possible to do the retrieval of a PDF file in Windows Phone 7. We are going to discuss in details how to do this. To do it you should download and install the WP7 control Component'sOne control for Windows Phone 7. When you have downloaded it then use it. Now we are going to write some steps explaining how it is used and you should follow these steps which are given below.

Step 1: In this step first of all we have to open a Windows Phone application; let us see how you will open it.

  • Go to Visual Studio 2010
  • File->New->Project
  • Select the template named as silverlight for Windows Phone
  • Select the Windows Phone application
  • Give it a name as you want.   

Step_1_1fig.jpg

Step_1_2fig.jpg

Step 2: In this step we will see how you add the pdfViewer control inside the toolbox; for this you see the figure given below.

First of all you go to the Toolbox and right-click on it then click to choose an item, as shown in the figure given below.

Step_2_1fig.jpg

In this figure you just see that the chosen component is the one that is selected; click ok to show it inside the Toolbox.

Step_2_2fig.jpg

Now you just see that the control has been added to the Toolbox as shown in the figure given below.

Step_2_3fig.jpg

Step 3: In this step you will add a PDF file to the application; let's see how to add it.

Step_3_1fig.jpg

Step_3_2fig.jpg


Step_3_3fig.jpg

Step 4: In this step we will see how to change the property of the PDF file named BuildAction; let's see how you will do that:

Step_4_1fig.jpg

In the given figure you will change the property named BuildAction whose value is None by default and change it to Resource as shown in the figures given below.

Step_4_2fig.jpg      Step_4_2_2fig.jpg

Step 5: In this step you will see the code for the MAinPAge.xaml file which is shown below.

Code:

<phone:PhoneApplicationPage

    x:Class="Mypdf.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True" xmlns:my="clr-namespace:C1.Phone.PdfViewer;assembly=C1.Phone.PdfViewer" Loaded="PhoneApplicationPage_Loaded">

    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">

        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock x:Name="PageTitle" Text="Pdf App" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}">

               <TextBlock.Foreground>

                  <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                     <GradientStop Color="Black" Offset="0" />

                     <GradientStop Color="#FFA1FFFF" Offset="1" />

                  </LinearGradientBrush>

               </TextBlock.Foreground>

            </TextBlock>

        </StackPanel>

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Grid.Background>

                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                    <GradientStop Color="Black" Offset="0" />

                    <GradientStop Color="#FFFFD5D5" Offset="1" />

                </LinearGradientBrush>

            </Grid.Background>

            <my:C1PdfViewer HorizontalAlignment="Left" Margin="12,21,0,0" Name="MyPdfViewer1" VerticalAlignment="Top" Width="438" />

        </Grid>

    </Grid> 

</phone:PhoneApplicationPage>

Step 6: In this step you will see the code for the MainPage.xaml.cs file which is shown below.

Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;

namespace Mypdf

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

            Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)

        {

            var MyPdf = Application.GetResourceStream(new Uri("MyPdf;component/BeggPhoneGap.pdf", UriKind.Relative));

            this.MyPdfViewer1.LoadDocument(MyPdf.Stream);

            MyPdfViewer1.FontSize = 20;

            MyPdfViewer1.Height = 550;

        }

    }

}

Step 7: In this step you will see the design of the MainPage.xaml file which is shown below.

Designimg.jpg

Step 8: Now in this step we are going to run the application by pressing F5 and the output regarding is given below.

Output 1: In this output you just see that it has read the PDF file; this shows it's front page.

Out1.jpg

Output 2: Whenever you scroll the page then you just see it shows the PDF page index and for zooming a page you will click twice or more on the page as you can see in the figure given below.

Out_2_new.jpg

Here are some other useful resources which may help you.

Sending Mail from Gmail and Save the file in PDF Format
HJ PDF in C#
How to embed HTML file in Windows Phone 7
PDF File GeneratorHow to create PDF file using itextsharp

Up Next
    Ebook Download
    View all
    Learn
    View all