Universal Windows App: Draw a PDF page to the screen

None
9/7/2016

Downloads

In this code sample , we will create a Universal Windows Application and display a PDF page that is selected by the user from the device.

We will show the relevant code snippets. You can download the full code sample above.

windows-phone

MainPage.xaml

The ManinPage.xaml that is part of a new Universal Windows Blank App is edited so that it looks like this:

<Page
    x:Class="TestWin2D.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestWin2D"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:canvas="using:Microsoft.Graphics.Canvas.UI.Xaml"
    mc:Ignorable="d" Height="Auto" Width="Auto">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <canvas:CanvasControl 
            x:Name="canvasControl" 
            Draw="canvasControl_Draw" 
            ClearColor="White" 
            DoubleTapped="canvasControl_DoubleTapped">
        </canvas:CanvasControl>
    </Grid>
</Page>

MainPage

The XAML above references TestWin2D.MainPage. This class inherits from Windows.UI.Xaml.Controls.Page. The XAML references two methods: canvasControl_Draw and canvasControl_DoubleTapped. Let's discuss these.

Method canvasControl_DoubleTapped calls helper method selectPdf. This methods asks the user to select a PDF document from the device and then draws the PDF to an offscreen canvas.

async private void selectPdf(CanvasControl sender)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.FileTypeFilter.Add(".pdf");
    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file == null) return;
    var documentStream = await file.OpenStreamForReadAsync();

    pdf.Page page = new pdf.Page(documentStream, 0);

    var scaleX = sender.ActualWidth / page.Width;
    var scaleY = sender.ActualHeight / page.Height;
    var scale = Math.Min(scaleX, scaleY); // fit page to screen

    var target = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), (float)(page.Width * scale), (float)(page.Height * scale), sender.Dpi);
    using (CanvasDrawingSession session = target.CreateDrawingSession())
    {
        await Task.Run(() => page.Draw(session, scale));
    }

    if (null != offscreenCanvas) offscreenCanvas.Dispose();
    offscreenCanvas = target;

    sender.Invalidate();
}

Method canvasControl_Draw checks if an offscreen canvas is available. If so, it draws to the screen.

private void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
    if (offscreenCanvas == null) return;
    args.DrawingSession.DrawImage(offscreenCanvas);
}