PdfDocumentSource for WPF / Features / Export PDF / Export PDF using ExportProvider
Export PDF using ExportProvider

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.WPF.Document
    Imports C1.WPF.Document.Export
    Imports Microsoft.Win32
    Imports C1.WPF
    
  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 SaveFileDialog class using the following code:
    Dim pds As New C1PdfDocumentSource()
    Dim dialog As New SaveFileDialog()
    
  5. Load the PDf file into the object of C1PdfDocumentSource using the LoadFromFile method.
    pds.LoadFromFile("..\..\DefaultDocument.pdf")
    
  6. Add the following code below InitializeComponent() method to get the list of supported exporters using SupportedExportProviders property.
    Dim supportedProviders = pds.SupportedExportProviders
    For Each sep As var In supportedProviders
         cbExporter.Items.Add(New C1ComboBoxItem() With { _
             Key .Content = [String].Format("Export to {0}...", sep.FormatName), _
             Key .Tag = sep _
         })
    Next
    
  7. Add the following code to create a method, DoExport, for exporting the PDF to another format using Export method.
    Private Sub DoExport(pds As C1PdfDocumentSource, ep As ExportProvider)
            dialog.DefaultExt = "." + ep.DefaultExtension
            dialog.FileName = System.IO.Path.GetFileName("Document")
            dialog.Filter = [String].Format("{0} (*.{1})|*.{1}|All files (*.*)|*.*", _
                                           ep.FormatName, ep.DefaultExtension)
            Dim dr As System.Nullable(Of Boolean) = dialog.ShowDialog(Me)
            If Not dr.HasValue OrElse Not dr.Value Then
                    Return
            End If
    
            Try
                    Dim exporter = ep.NewExporter()
                    exporter.ShowOptions = False
    
                    'Open document after export
                    exporter.Preview = True
    
                    'Set the output file name
                    exporter.FileName = dialog.FileName
    
                    'Export PDF
                    pds.Export(exporter)
                    MessageBox.Show(Me, "Document was successfully exported.", _
                                   "Information", MessageBoxButton.OK, _
                                   MessageBoxImage.Information)
            Catch ex As Exception
                    MessageBox.Show(Me, ex.Message, "Error", MessageBoxButton.OK, _
                                    MessageBoxImage.[Error])
            End Try
    End Sub
    
  8. Add the following code to the button's click event to call the DoExport method that accepts the object of C1PdfDocumentSource and ExportProvider as parameters.
    DoExport(pds, DirectCast(DirectCast(cbExporter.SelectedItem, C1ComboBoxItem).Tag, ExportProvider))
    
See Also