Click or drag to resize

RTF to PDF Converter

The RTF to PDF converter of ExpertPdf Anywhere converts RTF (Rich Text Format) documents to PDF, on Windows, Linux and macOS. The RTF document comes from a file or from a string - for example the text of a rich text editor, 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 main class is PdfConverter, in the ExpertPdf.Anywhere.RtfToPdf namespace. The RTF document is laid out by the Word engine of the library, in managed code, directly on the PDF pages: no browser engine and no native tool is involved. For Word documents (.docx, .doc) use the converter in the ExpertPdf.Anywhere.WordToPdf namespace (PdfConverter), which has the same shape.

Features
  • 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.

  • 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 (always subset-embedded), compression levels and a maximum image resolution for smaller files.

  • 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 AES encryption (128 or 256 bit keys) or RC4 (40 bit key).

  • PDF/A-1a, 1b, 2a, 2b, 2u, 3a, 3b, 3u, 4, 4e and 4f documents for long term archiving, and PDF/X-1a.

  • Accessible, tagged PDF/UA-1 and PDF/UA-2 documents, built from the structure of the RTF document.

Installation

The converter is part of the ExpertPdf.Anywhere package; there is no separate package to install. See Installation for the packages and Requirements for the supported platforms.

dotnet add package ExpertPdf.Anywhere

The text is drawn with the fonts installed on the computer. When a font the document names is missing, a bundled Liberation font with the same metrics is used for Latin text. On a Linux or macOS host without fonts (a Docker image, for example), add the ExpertPdf.Anywhere.Fonts package for the other scripts.

Getting started

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 ExpertPdf.Anywhere.Drawing;
using ExpertPdf.Anywhere.RtfToPdf;

PdfConverter converter = new PdfConverter();

// 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("report.rtf", "report.pdf");

The conversion methods:

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.

Converting in memory and in a web application

An RTF document that is already in memory does not need a temporary file. This ASP.NET Core action converts an uploaded RTF file and returns the PDF document to the browser:

using Microsoft.AspNetCore.Http;
using ExpertPdf.Anywhere.RtfToPdf;

public IActionResult ConvertRtf(IFormFile rtfUpload)
{
    string rtf;
    using (StreamReader reader = new StreamReader(rtfUpload.OpenReadStream()))
        rtf = reader.ReadToEnd();

    PdfConverter converter = new PdfConverter();
    byte[] pdfBytes = converter.GetPdfBytesFromRtfString(rtf);

    return File(pdfBytes, "application/pdf", "document.pdf");
}

The content of a rich text editor control is converted the same way, from the RTF text the control gives.

Page layout

The RTF document is laid out on the PDF pages themselves: the page size and orientation of PdfDocumentOptions, inside the margins, with the space of the header and the footer taken off each page. The text wraps and breaks between pages as in a word processor, and the fonts the document uses are subset-embedded.

  • PdfCompressionLevel sets the compression of the content.

  • ImagesMaxResolution resamples images drawn at a higher resolution than this, in pixels per inch (0, the default, keeps every image as it is; 150 suits screen reading, 300 print).

Appending PDF documents

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.

PdfConverter converter = new PdfConverter();
converter.PdfDocumentOptions.AppendPDFFile = "terms.pdf";

using (FileStream output = new FileStream("offer.pdf", FileMode.Create))
    converter.SavePdfFromRtfFileToStream("offer.rtf", output);
Document information, security and PDF/A

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). A 128 bit key (the default) gives AES-128, a 256 bit key AES-256 and a 40 bit key RC4; EncryptMetadata set to false leaves the XMP metadata readable. The document is encrypted only when one of these options is changed from its default.

PdfStandardSubset produces a PDF/A document for long term archiving (PDF/A-1a to PDF/A-4f) or a PDF/X-1a document for print exchange. The archiving levels embed the fonts; PDF/A-1 and PDF/X-1a also remove transparency, and the level A subsets tag the document. A PDF/A or PDF/X document cannot be encrypted (setting PdfSecurityOptions too throws an RtfConvertException), and the documents appended to it must conform to the same level (see PDF Standards).

AccessibilityStandard declares PDF/UA-1 or PDF/UA-2: the document is tagged from the structure of the RTF document (headings, paragraphs, lists, tables, pictures). Set Language and the title too; both standards require them. PDF/UA-2 writes the document as PDF 2.0, so it combines only with PDF/A-4 or no archiving level (see Tagged PDF).

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("report.rtf", "protected.pdf");

// an accessible PDF/A-2a document for archiving
PdfConverter archive = new PdfConverter();
archive.PdfDocumentInfo.Title = "Quarterly report";
archive.Language = "en-US";
archive.PdfStandardSubset = PdfStandardSubset.Pdf_A_2a;
archive.AccessibilityStandard = ExpertPdf.Anywhere.HtmlToPdf.PdfUAStandard.Pdf_UA_1;
archive.SavePdfFromRtfFileToFile("report.rtf", "archive.pdf");
Samples

The ConvertRtfToPdf page of the web sample (for Windows, Linux and macOS) converts the sample.rtf document that comes with it, a report with formatted text, lists, tables and pictures, with the page size, orientation, margins, a footer text and page numbers you choose.

Licensing

Set LicenseKey once when the application starts, or the LicenseKey property before you call a conversion method. Without a license key the PDF documents have an evaluation notice. See Licensing.

See Also