Convert Document to PDF
Environment
| Product Version | Product | Author |
|---|---|---|
| Not applicable | Telerik Document Processing | 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
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.
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 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.
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 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.
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 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.
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);