Document Library for UWP | ComponentOne
PdfDocumentSource for UWP / Features / Export PDF / Export PDF using Format Specific Filter
In This Topic
    Export PDF using Format Specific Filter
    In This Topic

    PdfDocumentSource provides support for exporting a PDF file to an external format through Export method inherited from C1DocumentSource class.

    To export PDF to HTML format

    1. Add a button control to the design view for exporting PDF.
    2. Switch to the code view and add the following namespaces in the code view.
      Imports C1.Xaml.Document
      Imports C1.Xaml.Document.Export
      
      using C1.Xaml.Document;
      using C1.Xaml.Document.Export;
      
    3. Add a PDF file to the project. In our case, we have used PDF file named DefaultDocument.pdf.
    4. Initialize the instance of C1PDFDocumentSource class using the following code:
      Dim pds As New C1PdfDocumentSource()
      
      C1PdfDocumentSource pds = new C1PdfDocumentSource();
      
    5. Load the PDf file into the object of C1PdfDocumentSource using the LoadFromFileAsync method.
      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);
      
    6. Add the following code to the button's click event to export the PDF to HTML format using HtmlFilter class.
      Try
              'Create HTMLFilter object
              'HtmlFilter filter = new HtmlFilter();
              Dim filter As New RtfFilter()
              filter.ShowOptions = False
      
              Dim storageFolder As StorageFolder = ApplicationData.Current.LocalFolder
      
              'Create file
              Dim FileForWrite As StorageFile = Await storageFolder.CreateFileAsync("TestFile.rtf", _
                                                CreationCollisionOption.ReplaceExisting)
      
              'Set the output file name
              filter.StorageFile = FileForWrite
      
              'Export PDF
      
      
              Await pds.ExportAsync(filter)
      Catch ex As Exception
              Dim md As New MessageDialog(String.Format("Failed to export", ex.Message), "Error")
              Await md.ShowAsync()
      End Try
      
      try
      {
          //Create HTMLFilter object
          RtfFilter filter = new RtfFilter();
          filter.ShowOptions = false;
      
          StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
      
          //Create file
          StorageFile FileForWrite = await storageFolder.CreateFileAsync("TestFile.rtf",
                                     CreationCollisionOption.ReplaceExisting);
      
          //Set the output file name
          filter.StorageFile = FileForWrite;
      
          //Export PDF
          await pds.ExportAsync(filter);    
      }
      catch (Exception ex)
      {
          MessageDialog md = new MessageDialog(string.Format("Failed to export", 
                             ex.Message), "Error");
          await md.ShowAsync();
      }
      

    To export PDF to an image file format

    Similar code as above can be used for exporting a PDF document to a series of page image files in one of the supported image formats (JPEG, PNG, TIFF, etc.). It is also possible to create a single ZIP file containing the page images. The following code uses one of the image format filter class, JpegFilter, to export the multi-paged file to JPEG format and creates a single ZIP file of the exported images.

    Try
            'Create JpegFilter object
            Dim jpgfilter As New JpegFilter()
            jpgfilter.UseZipForMultipleFiles = True
            jpgfilter.ShowOptions = False
    
    
            Dim storageFolder As StorageFolder = ApplicationData.Current.LocalFolder
    
            'Create file
            Dim file As StorageFile = Await storageFolder.CreateFileAsync("TestFile.zip", CreationCollisionOption.ReplaceExisting)
    
            'Set the output file name
            jpgfilter.StorageFile = file
    
    
            'Export PDF
            Await pds.ExportAsync(jpgfilter)
    Catch ex As Exception
            Dim md As New MessageDialog(String.Format("Failed to export", ex.Message), "Error")
            Await md.ShowAsync()
    End Try
    
    try
    {
        //Create JpegFilter object
        JpegFilter jpgfilter = new JpegFilter();
        jpgfilter.UseZipForMultipleFiles = true;
        jpgfilter.ShowOptions = false;
    
    
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    
        //Create file
        StorageFile file = await storageFolder.CreateFileAsync("TestFile.zip",
                           CreationCollisionOption.ReplaceExisting);
    
        //Set the output file name
        jpgfilter.StorageFile = file;
                
    
    //Export PDF
    await pds.ExportAsync(jpgfilter);
    }
    catch (Exception ex)
    {
        MessageDialog md = new MessageDialog(string.Format("Failed to export", ex.Message), "Error");
        await md.ShowAsync();
    }
    
    See Also