Document Library for UWP | ComponentOne
PdfDocumentSource for UWP / Features / Export PDF / Export PDF using ExportProvider
In This Topic
    Export PDF using ExportProvider
    In This Topic

    PdfDocumentSource allows you to enumerate the supported export formats for a document using the SupportedExportProviders property. The property returns a collection of ExportProvider classes that contain information about the supported formats, and can be used to create the corresponding export filter by using the NewExporter method of ExportProvider class.

    Different document types support different sets of export formats, therefore enumerating and creating the export filters via SupportedExportProviders yields the correct results.

    To export PDF using supported exporters

    1. Drag and drop Button and ComboBox controls from the Toolbox on the design view.
    2. Switch to code view and add the following namespaces in the code view.
      Imports C1.Xaml.Document
      Imports Windows.UI.Popups
      Imports C1.Xaml.Document.Export
      Imports Windows.Storage.Pickers
      Imports Windows.Storage
      
      using C1.Xaml.Document;
      using Windows.UI.Popups;
      using C1.Xaml.Document.Export;
      using Windows.Storage.Pickers;
      using Windows.Storage;
      
    3. Add a PDF file to the project. In our case, we have used PDF file named DefaultDocument.pdf from the product sample.
    4. Initialize the instances of C1PDFDocumentSource and create an instance of StorageFile class using the following code:
      Dim pds As New C1PdfDocumentSource()
      Dim sf As StorageFile
      
      C1PdfDocumentSource pds = new C1PdfDocumentSource();
      StorageFile sf;
      
    5. Load the PDF file into the object of C1PdfDocumentSource using the LoadFromFileAsync method.
      string fileName = null;
                  
      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);
      
    6. Add the following code below InitializeComponent() method to get the list of supported exporters using SupportedExportProviders property.
      cbExporter.Items.Clear()
      Dim supportedProviders = pds.SupportedExportProviders
      For Each sep As var In supportedProviders
              cbExporter.Items.Add(sep.FormatName)
      Next
      cbExporter.SelectedIndex = 0
      
      cbExporter.Items.Clear();
      var supportedProviders = pds.SupportedExportProviders;
      foreach (var sep in supportedProviders)
          cbExporter.Items.Add(sep.FormatName);
      cbExporter.SelectedIndex = 0;
      
    7. Add the following code to the button's click event to export the PDF file using ExportAsync method.
      ' prepare ExportFilter object
      Dim ep As ExportProvider = pds.SupportedExportProviders(cbExporter.SelectedIndex)
      Dim ef As ExportFilter = TryCast(ep.NewExporter(), ExportFilter)
      
      If (TypeOf ef Is BmpFilter OrElse TypeOf ef Is JpegFilter OrElse TypeOf ef Is PngFilter OrElse TypeOf ef Is GifFilter) Then
        ' these export filters produce more than one file during export
        ' ask for directory in this case
        If ef.UseZipForMultipleFiles = True Then
           ' ask for zip file
           Dim fsp As New FileSavePicker()
           fsp.DefaultFileExtension = ".zip"
      
           fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + ".zip"
           ef.StorageFile = Await fsp.PickSaveFileAsync()
           If ef.StorageFile Is Nothing Then
              Return
           End If
      Else
      
           Dim fp As New FolderPicker()
           fp.FileTypeFilter.Add("." + ep.DefaultExtension)
           fp.FileTypeFilter.Add(".zip")
           ef.StorageFolder = Await fp.PickSingleFolderAsync()
           If ef.StorageFolder Is Nothing Then
              ' user cancels an export
              Return
           End If
         End If
      Else
      
         ' ask for file
         Dim fsp As New FileSavePicker()
         fsp.DefaultFileExtension = "." + ep.DefaultExtension
         fsp.FileTypeChoices.Add(ep.FormatName + " (." + ep.DefaultExtension + ")", New String() {"." + ep.DefaultExtension})
      
         fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + "." + ep.DefaultExtension
         ef.StorageFile = Await fsp.PickSaveFileAsync()
         If ef.StorageFile Is Nothing Then
            Return
         End If
      End If
      Try
         Await pds.ExportAsync(ef)
      Catch ex As Exception
         Dim md As New MessageDialog(String.Format("Failed to Export", ex.Message), "Error")
         Await md.ShowAsync()
      End Try
      
      // prepare ExportFilter object
      ExportProvider ep = pds.SupportedExportProviders[cbExporter.SelectedIndex];
      ExportFilter ef = ep.NewExporter() as ExportFilter;
      
      if ((ef is BmpFilter || ef is JpegFilter || ef is PngFilter || ef is GifFilter))
      {
          // these export filters produce more than one file during export
          // ask for directory in this case
          if (ef.UseZipForMultipleFiles == true)
          {
            // ask for zip file
            FileSavePicker fsp = new FileSavePicker();
            fsp.DefaultFileExtension = ".zip";
      
            fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + ".zip";
            ef.StorageFile = await fsp.PickSaveFileAsync();
            if (ef.StorageFile == null)
               return;
          }
      
          else
          {
              FolderPicker fp = new FolderPicker();
              fp.FileTypeFilter.Add("." + ep.DefaultExtension);
              fp.FileTypeFilter.Add(".zip");
              ef.StorageFolder = await fp.PickSingleFolderAsync();
              if (ef.StorageFolder == null)
                  // user cancels an export
                  return;
          }
      }
      else
      {
      
          // ask for file
          FileSavePicker fsp = new FileSavePicker();
          fsp.DefaultFileExtension = "." + ep.DefaultExtension;
          fsp.FileTypeChoices.Add(ep.FormatName + " (." + ep.DefaultExtension + ")",
              new string[] { "." + ep.DefaultExtension });
      
          fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + "." +
                                  ep.DefaultExtension;
          ef.StorageFile = await fsp.PickSaveFileAsync();
          if (ef.StorageFile == null)
              return;
      }
      try
      {
          await pds.ExportAsync(ef);
      }
      catch (Exception ex)
      {
          MessageDialog md = new MessageDialog(string.Format("Failed to Export", 
                                               ex.Message), "Error");
          await md.ShowAsync();
      }
      
    See Also