Getting Started with ExpertPdf RTF to PDF Converter Library for .NET |
ExpertPdf RTF to PDF Converter converts RTF (Rich Text Format) documents to PDF in .NET Framework applications: Windows Forms, WPF and console applications, Windows services and ASP.NET web sites. The RTF document comes from a file or from a string - for example the text of a rich text box, a database field or a template filled in code - and the PDF document is saved to a file, written to a stream or returned in memory.
The library is one assembly, eprtftopdf.dll. The main class is PdfConverter.
Convert RTF files and RTF strings to PDF files, streams or byte arrays.
Page size (A0 to A10, B0 to B5, Letter, Legal and more), portrait or landscape orientation, margins.
Scale the content to the width of the page, or the whole document to the height of one page.
A header with a title, a subtitle and a line; a footer with a text, page numbers and a line; the colors of their texts.
Embedded fonts, compression levels, compressed cross-reference tables for smaller files.
An option to keep text lines from being cut at the bottom of a page.
Append existing PDF documents (files or streams) at the end of the result.
Document information: title, author, subject, keywords, creation date.
Passwords and permissions, with RC4 encryption and 40 or 128 bit keys.
PDF/A-1b documents for long term archiving.
.NET Framework 4.0 or later. The download also has a build for .NET Framework 2.0 - 3.5 applications.
Windows 10, Windows 11, Windows Server 2016 or later (32-bit or 64-bit).
The converter is for .NET Framework applications only: .NET Core and .NET 5 or later (including .NET 10 and .NET 11) are not supported.
The easiest way to install the library is the ExpertPdf.RtfToPdf package (https://www.nuget.org/packages/ExpertPdf.RtfToPdf/), for .NET Framework projects, AnyCPU and x86. In Visual Studio open the NuGet Package Manager and install the package, or run:
dotnet add package ExpertPdf.RtfToPdf
ExpertPdf.RtfToPdf.x64 (https://www.nuget.org/packages/ExpertPdf.RtfToPdf.x64/) is the x64 optimized build, for applications that target the x64 platform and run in a 64-bit process (for a web site: a 64-bit application pool).
The download contains:
Bin\.NET_4.0\eprtftopdf.dll - the library for .NET Framework 4.0 and later
Bin\eprtftopdf.dll - the library for .NET Framework 2.0 - 3.5
Doc - this documentation
Samples - samples for .NET Framework 4.6.2 in C# and VB.NET (see Samples)
Add a reference to the eprtftopdf.dll for your framework. In an ASP.NET web site copy it to the Bin folder of the site.
Create a PdfConverter, set the options and call one of the conversion methods. This code converts an RTF file to an A4 PDF document with a header and a footer:
using System.Drawing; using ExpertPdf.RtfToPdf; PdfConverter converter = new PdfConverter(); converter.LicenseKey = "your license key"; // the PDF page; the margins are in points (1/72 inch) converter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4; converter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait; converter.PdfDocumentOptions.LeftMargin = 36; converter.PdfDocumentOptions.RightMargin = 36; converter.PdfDocumentOptions.TopMargin = 20; converter.PdfDocumentOptions.BottomMargin = 20; // a header with a title and a subtitle converter.PdfDocumentOptions.ShowHeader = true; converter.PdfHeaderOptions.HeaderText = "Quarterly Sales Report"; converter.PdfHeaderOptions.HeaderSubtitleText = "Sales Analysis team"; converter.PdfHeaderOptions.HeaderTextColor = Color.Navy; // the footer is shown by default: a text and the page numbers ("Page 1 / 3") converter.PdfFooterOptions.FooterText = "Internal use"; converter.PdfFooterOptions.PageNumberText = "Page"; converter.SavePdfFromRtfFileToFile(@"C:\Documents\report.rtf", @"C:\Documents\report.pdf");
The conversion methods:
SavePdfFromRtfFileToFile(String, String), SavePdfFromRtfFileToStream(String, Stream), GetPdfBytesFromRtfFile(String) - from an RTF file
SavePdfFromRtfStringToFile(String, String), SavePdfFromRtfStringToStream(String, Stream), GetPdfBytesFromRtfString(String) - from an RTF string
A conversion problem - for example an RTF document that cannot be converted, or an output file that cannot be written - is reported with a RtfConvertException. The methods can also throw the usual file exceptions, for example when the RTF file does not exist.
An RTF document that is already in memory does not need a temporary file. This ASP.NET Web Forms code converts the RTF text of an uploaded file and sends the PDF document to the browser:
string rtf; using (StreamReader reader = new StreamReader(rtfUpload.PostedFile.InputStream, Encoding.Default)) rtf = reader.ReadToEnd(); PdfConverter converter = new PdfConverter(); byte[] pdfBytes = converter.GetPdfBytesFromRtfString(rtf); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "inline; filename=document.pdf"); Response.BinaryWrite(pdfBytes); Response.Flush(); Response.SuppressContent = true; Context.ApplicationInstance.CompleteRequest();
In a Windows Forms application the content of a RichTextBox control is converted the same way, from its Rtf property.
The converter lays out the RTF document on a virtual page 800 pixels wide, like a window of that width, and draws the result on the PDF pages; a virtual page wider than the PDF page is shrunk to the page width. Set another width with the PdfConverter(Int32) constructor or the PageWidth property: a wider virtual page gives longer lines and smaller text in the PDF document. The PdfConverter(Int32, Int32) constructor (or PageHeight) also fixes the height of the document: the content below that height is not converted.
FitWidth (on by default) shrinks the content when the virtual page is wider than the PDF page. When it is off the content keeps its real size.
FitHeight shrinks the whole document to the height of one PDF page.
AvoidTextBreak moves a line of text that would be cut by the bottom of a page to the next page.
EmbedFonts embeds the TrueType fonts: the file is larger, and the text looks the same on every computer.
PdfCompressionLevel and UseCrossRefStreams make the file smaller.
The converter can add existing PDF documents at the end of the converted document, for example terms and conditions after an offer: set AppendPDFFile or AppendPDFStream for one document, AppendPDFFileArray or AppendPDFStreamArray for several.
PdfDocumentInfo sets the title, author, subject, keywords and creation date that PDF viewers show in the document properties.
PdfSecurityOptions protects the document: a user password to open it, an owner password that gives all the rights, and the permissions (print, copy the content, copy for accessibility, edit the content, edit annotations, fill form fields, assemble the document), with RC4 encryption and a 128 bit (default) or 40 bit key. The document is encrypted only when one of these options is changed from its default.
PdfStandardSubset set to Pdf_A_1b produces a PDF/A-1b document for long term archiving: the fonts are embedded, transparency is disabled, the colors have an sRGB output intent and the document carries the PDF/A identification. A PDF/A document cannot be encrypted (setting PdfSecurityOptions too throws an RtfConvertException), UseCrossRefStreams is ignored, and documents appended to it should be PDF/A-1b documents themselves.
PdfConverter converter = new PdfConverter(); converter.PdfDocumentInfo.Title = "Quarterly report"; converter.PdfDocumentInfo.AuthorName = "Sales team"; converter.PdfDocumentInfo.Subject = "Sales by region"; converter.PdfDocumentInfo.Keywords = "sales, quarterly"; // a protected document: "open" opens it without printing or copying, "owner" gives all the rights converter.PdfSecurityOptions.UserPassword = "open"; converter.PdfSecurityOptions.OwnerPassword = "owner"; converter.PdfSecurityOptions.CanPrint = false; converter.PdfSecurityOptions.CanCopyContent = false; converter.SavePdfFromRtfFileToFile(@"C:\Documents\report.rtf", @"C:\Documents\protected.pdf"); // a PDF/A-1b document for archiving PdfConverter archive = new PdfConverter(); archive.PdfDocumentInfo.Title = "Quarterly report"; archive.PdfStandardSubset = PdfStandardSubset.Pdf_A_1b; archive.SavePdfFromRtfFileToFile(@"C:\Documents\report.rtf", @"C:\Documents\archive.pdf");
The Samples folder of the download has these samples for .NET Framework 4.6.2, each in C# and VB.NET. They convert the sample.rtf document that comes with them, a two page report with formatted text, lists, tables and pictures.
CommandLine_RtfToPdf - a command line tool: an RTF file converted to a PDF file with a header, a footer and the document information, an RTF string converted in memory, a conversion to a stream with another PDF document appended, a password protected document and a PDF/A-1b document. It runs without arguments; run it with /? for its options.
WinForms_RtfToPdfDemo - a Windows Forms demo with all the options of the converter and a preview of the RTF document.
AspNet\RtfToPdfWebDemo - an ASP.NET Web Forms web site that converts the sample document or an uploaded RTF file and sends the PDF document to the browser. Open it in Visual Studio with File > Open > Web Site, or run it with IIS Express.
PdfDocumentInfo: the document information of the PDF document.
PdfSecurityOptions: passwords and permissions.
PdfStandardSubset: PDF/A-1b output.
Version 10 is part of the ExpertPdf 22.0 release and needs a version 10 license key.
New samples for .NET Framework 4.6.2, in C# and VB.NET: a command line tool, a Windows Forms demo and an ASP.NET Web Forms web site.
Set the LicenseKey property to the license key you have received after the purchase, before you call a conversion method. Without a license key the PDF documents have an evaluation notice.
For support and more documentation visit https://www.html-to-pdf.info or write to office@html-to-pdf.net.