This content originally appeared on Telerik Blogs and was authored by Anna Velcheva
Learn how to quickly turn your documents into PDF using the power of Telerik Document Processing Library.
The PDF file format is a fixed format, which allows its creator full control on how the document content will be arranged and displayed. It is widely used and required in a variety of scenarios, which makes it an indispensable staple of past and present digital communication. Any application which deals with documents in any way cannot do without it.
Creating PDF files programmatically, however, is not so straightforward. The standard is intricate, and its specification is daunting in its size of about a thousand pages. Putting together a PDF file from scratch is realistically not a feasible task for many development teams.
In comes the Progress Telerik Document Processing Libraries—DPL for short. DPL is a set of .NET libraries which provide APIs for the creation of the most commonly used file formats and conversion between them. DPL is a great fit for web, desktop and cross-platform apps, both for modern .NET and .NET Framework. This blog post will show you how to use it to turn almost any document into a PDF.
How DPL Conversion Works
Here is the short and sweet version of the way DPL operates. Each file format has a class which corresponds to it. For example, for PDF this is RadFixedDocument, for DOCX this is RadFlowDocument, and for XLSX it is Workbook. One object of these classes corresponds to one file and can be either created from scratch or imported from a file.
Each format has one or more format provider classes which are responsible for its import and export. Converting a file to a PDF boils down to importing a file into an object and then exporting it again to a PDF file. Let’s dive deeper into how this is done for each format.
DOCX to PDF
Starting with the classics, DOCX is one of the most natural file formats to export to PDF. The DOCX format is imported into a RadFlowDocument using the DocxFormatProvider:
RadFlowDocument flowDocument;
using (Stream inputStream = File.OpenRead(@"…\docxSample.docx"))
{
DocxFormatProvider docxProvider = new DocxFormatProvider();
flowDocument = docxProvider.Import(inputStream);
}
All the information, contents, styles, images are now in the flowDocument object. If you’d like to make changes to the file, now is a good time to do so using the RadFlowDocument API.
The next step is to export the object to PDF. This is done the same way as the import, but using the dedicated PdfFormatProvider for flow documents:
using (Stream outputStream = File.Create(@"…\docxToPdfSample.pdf"))
{
PdfFormatProvider pdfProvider = new PdfFormatProvider();
pdfProvider.Export(flowDocument, outputStream);
}
DOCX
If you would like to play around with what your document will look like as a PDF, check out our WordsProcessing export to PDF demo.
HTML to PDF
HTML is another natural format to export to PDF. Our approach will be the same as DOCX. Import an HTML file into a RadFlowDocument object and export the object to a PDF file.
RadFlowDocument flowDocument;
using (Stream inputStream = File.OpenRead(@"..\htmlSample.html"))
{
HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
flowDocument = htmlProvider.Import(inputStream);
}
using (Stream outputStream = File.Create(@"..\htmlToPdfSample.pdf"))
{
PdfFormatProvider pdfProvider = new PdfFormatProvider();
pdfProvider.Export(flowDocument, outputStream);
}
Once again, you can test this in the WordsProcessing export to PDF demo and upload your own HTML file.
XLSX to PDF
While DOCX and HTML fall into the domain of WordsProcessing, XLSX import and export are part of the SpreadProcessing library. The conversion mechanism, however, remains the same.
By now you probably already know the drill. Import the XLSX file into an object—in this case, the Workbook—and export it to PDF. SpreadProcessing has its own PdfFormatProvider, which we will use in this sample.
Workbook xlsxDocument;
using(Stream inputStream = File.OpenRead(@"..\xlsxSample.xlsx"))
{
XlsxFormatProvider xlsxFormatProvider = new XlsxFormatProvider();
xlsxDocument = xlsxFormatProvider.Import(inputStream);
}
using (Stream outStream = File.Create(@"..\xlsxToPdfSample.pdf"))
{
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
pdfFormatProvider.Export(xlsxDocument, outStream);
}
XLSX
While DOCX and to some extent HTML come with an inherent page layout, XLSX does not split into pages that intuitively. SpreadProcessing will do the job, just as Excel does when you want to print a document, but you might want to make some adjustments if the result is not split and laid out as you expect.
This is what the WorksheetPrintOptions are for. They allow you to set print areas, fit the content horizontally or vertically, set the orientation of the page and much more. Let’s make some changes to make our document fit better:
using(Stream inputStream = File.OpenRead(@"..\xlsxSample.xlsx"))
[…]
Worksheet worksheet = xlsxDocument.ActiveWorksheet;
worksheet.WorksheetPageSetup.FitToPages = true;
worksheet.WorksheetPageSetup.FitToPagesWide = 1;
worksheet.WorksheetPageSetup.PageOrientation = PageOrientation.Landscape;
using (Stream outStream = File.Create(@"..\xlsxToPdfSample.pdf"))
[…]
PDF
Conclusion
As you can see, with Telerik Document Processing Library, converting files to PDF is indeed quick and easy! Interested? Check out the DPL page, where you will find additional information, demos and documentation:
DPL is part of the following Telerik products:
- Telerik UI for Blazor
- Telerik UI for ASP.NET Core
- Telerik UI for ASP.NET MVC
- Telerik UI for ASP.NET AJAX
- Telerik UI for .NET MAUI
- Telerik UI for WinForms
- Telerik UI for WPF
- And the Telerik DevCraft bundle, which includes all of the above—try it yourself!
This content originally appeared on Telerik Blogs and was authored by Anna Velcheva
Anna Velcheva | Sciencx (2025-01-22T16:13:01+00:00) Quick and Easy Conversion to PDF with Telerik Document Processing Library. Retrieved from https://www.scien.cx/2025/01/22/quick-and-easy-conversion-to-pdf-with-telerik-document-processing-library/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.