Document Library for UWP | ComponentOne
PdfDocumentSource for UWP / Features / Load PDF
In This Topic
    Load PDF
    In This Topic

    PdfDocumentSource allows you to load a PDF in FlexViewer control using two methods, LoadFromFileAsync and LoadFromStreamAsync, of C1PdfDocumentSource class. The LoadFromFileAsync method loads PDF from the source file and the LoadFromStreamAsync method loads a PDf from source stream.

    To load PDF from file

    The following code uses the LoadFromFileAsync method to load a PDF from source file.

    Dim fileName As String = Nothing
    
    sf = Await StorageFile.GetFileFromApplicationUriAsync(New Uri _
         ("ms-appx:///DefaultDocument.pdf"))
    Await pds.LoadFromFileAsync(sf)
    fileName = Path.GetFileName(sf.Name)
    
    string fileName = null;
    
    sf = await StorageFile.GetFileFromApplicationUriAsync(
         new Uri("ms-appx:///DefaultDocument.pdf"));
    await pds.LoadFromFileAsync(sf);
    fileName = Path.GetFileName(sf.Name);
    

    To load PDF from stream

    1. Use the following code to load a PDF from source stream using LoadFromStreamAsync method.
      Private asm As Assembly = GetType(MainPage).GetTypeInfo().Assembly
      Private Function LoadPdf(pdfName As String) As Task
          Dim pdfSource As New C1PdfDocumentSource()
          pdfSource.UseSystemRendering = False
          If pdfSource Is Nothing Then
             pdfSource = New C1PdfDocumentSource()
          End If
          ' load pdf from resource stream
          Dim memStream = New MemoryStream()
          Using stream As Stream = asm.GetManifestResourceStream(Convert.ToString("Sample_PDFDocumentSource.Resources.") & pdfName)
             Await stream.CopyToAsync(memStream)
             memStream.Position = 0
          End Using
          Await pdfSource.LoadFromStreamAsync(memStream.AsRandomAccessStream())
          flexViewer.DocumentSource = pdfSource
      End Function
      
      Assembly asm = typeof(MainPage).GetTypeInfo().Assembly;
      async Task LoadPdf(string pdfName)
      {
          C1PdfDocumentSource pdfSource = new C1PdfDocumentSource();
          pdfSource.UseSystemRendering = false;
          if (pdfSource == null)
          {
              pdfSource = new C1PdfDocumentSource();
          }
          // load pdf from resource stream
          var memStream = new MemoryStream();
          using (Stream stream = asm.GetManifestResourceStream
                ("Sample_PDFDocumentSource.Resources." + pdfName))
          {
              await stream.CopyToAsync(memStream);
              memStream.Position = 0;
          }
          await pdfSource.LoadFromStreamAsync(memStream.AsRandomAccessStream());
          flexViewer.DocumentSource = pdfSource;
      }
      
    2. Add the following code beneath the InitializeComponent() method to call the LoadPdf method.
      LoadPdf("DefaultDocument.pdf")
      
      LoadPdf("DefaultDocument.pdf");
      
    See Also