ComponentOne Document Library for WinForms
Export PDF using ExportProvider
PdfDocumentSource for WinForms > Features > Export PDF > 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 C1PdfDocumentSource, SaveFileDialog, Button, and ComboBox controls from the Toolbox on the form.
  2. In the Properties window, navigate to DropDownStyle property and select DropDownList from the drop-down.
  3. Switch to code view and add the following namespaces in the code view.
    Imports System.IO
    Imports C1.Win.C1Document
    Imports C1.Win.C1Document.Export
    
    using System.IO;
    using C1.Win.C1Document;
    using C1.Win.C1Document.Export;
    
  4. Add a PDF file to the project. In our case, we have used PDF file named DefaultDocument.pdf from the product sample.
  5. Load the PDF file into the object of C1PdfDocumentSource using the LoadFromFile method.
    C1PdfDocumentSource1.LoadFromFile("..\..\DefaultDocument.pdf")
    
    c1PdfDocumentSource1.LoadFromFile(@"..\..\DefaultDocument.pdf");
    
  6. Add the following code below InitializeComponent method to get the list of supported exporters using SupportedExportProviders property.
    Dim supportedProviders = C1PdfDocumentSource1.SupportedExportProviders
    For Each sep In supportedProviders
        cbExporter.Items.Add(New FileAction() With {
        .Text = String.Format("Export to {0}...", sep.FormatName),
        .ExportProvider = sep
    })
    
    var supportedProviders = c1PdfDocumentSource1.SupportedExportProviders;
    foreach (var sep in supportedProviders)
        cbExporter.Items.Add(new FileAction()
        {
            Text = string.Format("Export to {0}...",
            sep.FormatName), ExportProvider = sep
        });
    
  7. Add the following code to add a class, FileAction, containing variables.
    Private Class FileAction
        Public Property Text() As String
            Get
                Return m_Text
            End Get
            Set
                m_Text = Value
            End Set
        End Property
        Private m_Text As String
        Public Property ExportProvider() As ExportProvider
            Get
                Return m_ExportProvider
            End Get
            Set
                m_ExportProvider = Value
            End Set
        End Property
        Private m_ExportProvider As ExportProvider
        Public Overrides Function ToString() As String
            Return Text
        End Function
    End Class
    
    private class FileAction
    {
        public string Text { get; set; }
        public ExportProvider ExportProvider { get; set; }
        public override string ToString()
        {
            return Text;
        }
    }
    
  8. 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)
        SaveFileDialog1.DefaultExt = "." + ep.DefaultExtension
        SaveFileDialog1.FileName = Path.GetFileName("Document") + "." +
                                   ep.DefaultExtension
        SaveFileDialog1.Filter = String.Format("{0} (*.{1})|*.{1}|All files (*.*)|*.*",
                                 ep.FormatName, ep.DefaultExtension)
    
        If SaveFileDialog1.ShowDialog(Me) <> DialogResult.OK Then
            Return
        End If
    
        Try
            Dim exporter = ep.NewExporter()
            exporter.ShowOptions = False
            exporter.FileName = SaveFileDialog1.FileName
            If exporter.ShowOptionsDialog() Then
                pds.Export(exporter)
                MessageBox.Show(Me, "Document was successfully exported.",
                                "Information", MessageBoxButtons.OK,
                                MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(Me, ex.Message, "Error", MessageBoxButtons.OK,
                            MessageBoxIcon.[Error])
        End Try
    End Sub
    
    private void DoExport(C1PdfDocumentSource pds, ExportProvider ep)
    {
        saveFileDialog1.DefaultExt = "." + ep.DefaultExtension;
        saveFileDialog1.FileName = Path.GetFileName("Document")+ "." + 
                                   ep.DefaultExtension;
        saveFileDialog1.Filter = string.Format("{0} (*.{1})|*.{1}|All files (*.*)|*.*",
                                 ep.FormatName, ep.DefaultExtension);
    
        if (saveFileDialog1.ShowDialog(this) != DialogResult.OK)
            return;
    
        try
        {
            var exporter = ep.NewExporter();
            exporter.ShowOptions = false;
            exporter.FileName = saveFileDialog1.FileName;
            if (exporter.ShowOptionsDialog())
            {
                pds.Export(exporter);
                MessageBox.Show(this, "Document was successfully exported.",
                                "Information", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
        }
    }
    
  9. 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(C1PdfDocumentSource1, DirectCast(cbExporter.SelectedItem,
             FileAction).ExportProvider)
    
    DoExport(c1PdfDocumentSource1, ((FileAction)
             cbExporter.SelectedItem).ExportProvider);
    
See Also