ComponentOne PdfViewer for UWP
Key Features / Step 2 of 3: Adding Code to the C1PdfViewer Application
In This Topic
    Step 2 of 3: Adding Code to the C1PdfViewer Application
    In This Topic

    In the previous step you created a new UWP-style project and added a C1PDFViewer control to the application. In this step you'll continue by adding a PDF document to the application, and code to display the PDF file in the C1PdfViewer control.

    Complete the following steps:

    1. In the Solution Explorer, right-click the project name and select Add │ Existing Item.
    2. In the Add Existing Item dialog box, locate a PDF file (for example the C1XapOptimizer.pdf included with the samples) and click Add.
      You can select any PDF file but will have to replace "C1XapOptimizer.pdf" with the name of your PDF file in the code below.
    3. Select the PDF file in the Solution Explorer, and in the Properties window set the file's Build Action to Embedded Resource.
    4. Right-click the page and select View | Code to switch to Code view.
    5. In Code view, add the following import statements to the top of the page:
    Visual Basic
    Copy Code
    Imports C1.Xaml.PdfViewer
    

    C#
    Copy Code
    using C1.Xaml.PdfViewer;
    
    1. Add code to the page's constructor so that it appears like the following:
    Visual Basic
    Copy Code
    Public Sub New()
        Me.InitializeComponent()
        Dim asm As Assembly = GetType(MainPage).GetTypeInfo().Assembly
        Dim stream As Stream
        stream = asm.GetManifestResourceStream("PdfViewerSamples.C1XapOptimizer.pdf")
        pdfViewer.LoadDocument(stream)
    End Sub
    

    C#
    Copy Code
    public MainPage()
    {
        this.InitializeComponent();
        Assembly asm = typeof(MainPage).GetTypeInfo().Assembly;
        Stream stream = asm.GetManifestResourceStream("PdfViewerSamples.C1XapOptimizer.pdf");
        pdfViewer.LoadDocument(stream);
    }
    
    You will need to replace "PdfViewerSamples" with the name of your project's namespace.
    1. Add the following btnLoad_Click event handler to the project:
    Visual Basic
    Copy Code
    Private Async Sub btnLoad_Click(sender As Object, e As Windows.UI.Xaml.RoutedEventArgs)
        Dim openPicker As New FileOpenPicker()
        openPicker.FileTypeFilter.Add(".pdf")
        Dim file As StorageFile = Await openPicker.PickSingleFileAsync()
        If file IsNot Nothing Then
            Dim stream As System.IO.Stream = Await file.OpenStreamForReadAsync()
            pdfViewer.LoadDocument(stream)
        End If
    End Sub
    

    C#
    Copy Code
    private async void btnLoad_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.FileTypeFilter.Add(".pdf");
        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            Stream stream = await file.OpenStreamForReadAsync();
            pdfViewer.LoadDocument(stream);
        }
    }
    

    In this step you completed adding code to your application. In the next step you'll run the application and observe run-time interactions.

    See Also