# Convert Document to PDF

## Environment

| Product Version | Product                     | Author                                                                  |
| --------------- | --------------------------- | ----------------------------------------------------------------------- |
| Not applicable  | Telerik Document Processing | [Tanya Dimitrova](https://www.telerik.com/blogs/author/tanya-dimitrova) |

## Description

The Telerik Document Processing libraries allow you to create, modify, and export PDF documents. You can also convert documents from different file formats to PDF.

Depending on the scenario, you can use the different features each library provides. This article contains examples of the most common approaches for converting a document to PDF.

## Solution

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

## Table of Contents

* [Convert a Document to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-a-document-to-pdf)
  * [DOCX to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-docx-to-pdf)
  * [DOC to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-doc-to-pdf)
  * [HTML to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-html-to-pdf)
  * [RTF to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-rtf-to-pdf)
  * [Plain text to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-txt-to-pdf)
* [Convert a Spreadsheet Document to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-a-spreadsheet-document-to-pdf)
  * [XLSX to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-xlsx-to-pdf)
  * [XLS to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-xls-to-pdf)
  * [CSV to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-csv-to-pdf)
  * [DataTable object to PDF](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/convert-to-pdf#convert-datatable-to-pdf)

## Convert a Document to PDF

When you need to convert a document from another file format to PDF, use the capabilities of **WordsProcessing**. This library allows you to import documents from the most common rich text formats (DOCX, DOC, HTML, RTF) and 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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/formats-and-conversion/formats-and-conversion).

> note To use the **PdfFormatProvider** of **WordsProcessing**, add a reference to the **Telerik.Windows.Documents.Flow\.FormatProviders.Pdf** assembly. For the full list of dependencies required by WordsProcessing, check the [WordsProcessing Getting Started](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/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, read [the PdfFormatProvider documentation](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/formats-and-conversion/pdf/pdfformatprovider).

### Convert DOCX to PDF

```csharp
	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

```csharp
	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

```csharp
	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

```csharp
	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

```csharp
	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 previously discussed libraries work with text documents, **SpreadProcessing** allows you to create, import, and export tabular data. This library supports the most common file formats for storing spreadsheet documents: XLSX, XLS, XLSM, and CSV. All format providers are listed and described in the corresponding [Formats and Conversion section](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/formats-and-conversion/pdf/pdfformatprovider).

> note To enable the export to PDF in SpreadProcessing, add a reference to the **Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf** assembly. For the full list of dependencies required by SpreadProcessing, check the [SpreadProcessing Getting Started](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/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, read [the SpreadProcessing PdfFormatProvider documentation](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/formats-and-conversion/pdf/pdfformatprovider).

### Convert XLSX to PDF

```csharp
	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

```csharp
	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

```csharp
	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

```csharp
	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

* [Getting Started with Telerik Document Processing](https://www.telerik.com/document-processing-libraries/documentation/getting-started/getting-started)
* [PdfProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radpdfprocessing/overview)
* [WordsProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/overview)
* [SpreadProcessing](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/overview)
