New to Telerik Document Processing? Download free 30-day trial

Convert Document to PDF

Product Version Product Author
N/A Telerik Document Processing Tanya Dimitrova

Description

The libraries included in Telerik Document Processing allow you to create, modify and export PDF documents, as well as convert a document from different file format to PDF.

Depending on the scenario, you could take advantage of the different functionalities each library provides. This article contains examples of the most common approaches for converting a document to PDF using Telerik Document Processing.

Solution

This article shows examples of the most common scenarios for converting documents of different types to PDF. The Table of Contents section below contains the full list of covered examples for easy and quick navigation.

Table of Contents

Convert a Document to PDF

In scenarios where you need to convert a document from another file format to PDF, you could take advantage of the capabilities of WordsProcessing. This library allows you to import documents from the most common rich text formats (Docx, Doc, HTML, RTF) as well as plain text and export them to PDF. All the supported document formats and the corresponding format providers are listed in the Formats and Conversion section.

In order to use the PdfFormatProvider of WordsProcessing, you should add a reference to the Telerik.Windows.Documents.Flow.FormatProviders.Pdf assembly. For the full list of dependencies required by WordsProcessing, check the Getting Started topic.

The PdfFormatProvider class of WordsProcessing resides in the Telerik.Windows.Documents.Flow.FormatProviders.Pdf namespace. For more information on how to work with this provider, please read this topic.

Convert DOCX to PDF

    Telerik.Windows.Documents.Flow.Model.RadFlowDocument flowDocument;
    using (Stream input = File.OpenRead("sample.docx"))
    {
        Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
        // The import method enables you to also pass a byte[] with the DOCX document data
        flowDocument = provider.Import(input);
    }

    Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(flowDocument);

Convert DOC to PDF

    Telerik.Windows.Documents.Flow.Model.RadFlowDocument flowDocument;
    using (Stream input = File.OpenRead("sample.doc"))
    {
        Telerik.Windows.Documents.Flow.FormatProviders.Doc.DocFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Doc.DocFormatProvider();
        // The import method enables you to also pass a byte[] with the DOC document data
        flowDocument = provider.Import(input);
    }

    Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(flowDocument);

Convert HTML to PDF

    string htmlContent = "<!DOCTYPE html><html><body><p>Hello, world!</p></body></html>";

    Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
    // Create a document instance from the content.
    RadFlowDocument flowDocument = htmlProvider.Import(htmlContent);

    Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(flowDocument);

Convert RTF to PDF

    Telerik.Windows.Documents.Flow.Model.RadFlowDocument flowDocument;
    using (Stream input = File.OpenRead("sample.rtf"))
    {
        Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
        // The import method enables you to also pass a string or a stream with the content.
        flowDocument = provider.Import(input);
    }

    Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(flowDocument);

Convert Plain text to PDF

    Telerik.Windows.Documents.Flow.Model.RadFlowDocument flowDocument;
    string text = "Hello, WordsProcessing!";
    Telerik.Windows.Documents.Flow.FormatProviders.Txt.TxtFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Txt.TxtFormatProvider();
    // The import method enables you to also pass a string or a stream with the content.
    flowDocument = provider.Import(text);

    Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(flowDocument);

Convert a Spreadsheet Document to PDF

While the so far discussed libraries allow working with text documents, with SpreadProcessing you can create, import and export tabular data. This library supports the most common file formats for storing spreadsheet documents - XLSX, XLS, XLSM, CSV. All format providers are listed and described in the corresponding Formats and Conversion section.

In order to enable the export to PDF in SpreadProcessing, you will need to add a reference to the Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf assembly. For the full list of dependencies required by SpreadProcessing, check the Getting Started topic.

The PdfFormatProvider class of SpreadProcessing resides in the Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf namespace. For more information on how to work with this provider, please read this topic.

Convert XLSX to PDF

    Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
    using (Stream input = File.OpenRead("sample.xlsx"))
    {
        Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider provider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider();
        // The import method enables you to also pass a byte[] with the XLSX document data
        workbook = provider.Import(input);
    }

    Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(workbook);

Convert XLS to PDF

    Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
    using (Stream input = File.OpenRead("sample.xls"))
    {
        Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider provider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Xls.XlsFormatProvider();
        // The import method enables you to also pass a byte[] with the XLS document data
        workbook = provider.Import(input);
    }

    Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(workbook);

Convert CSV to PDF

    Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
    using (Stream input = File.OpenRead("sample.csv"))
    {
        Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv.CsvFormatProvider provider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.TextBased.Csv.CsvFormatProvider();
        // The import method enables you to also pass a string with the CSV document data
        workbook = provider.Import(input);
    }

    Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(workbook);

Convert DataTable to PDF

    Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook;
    using (Stream input = File.OpenRead("sample.csv"))
    {
        Telerik.Windows.Documents.Spreadsheet.FormatProviders.DataTableFormatProvider provider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.DataTableFormatProvider();
        // Load the DataTable from yor database
        DataTable dataTable = LoadDataTable();
        workbook = provider.Import(dataTable);
    }

    Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();

    // Export the document. The different overloads enables you to export to a byte[] or to a Stream.
    byte[] pdfBytes = pdfProvider.Export(workbook);

See Also

In this article