Converting Text and RTF to PDFs

The DynamicPDF Converter for .NET converts text and rich text (RTF) using the TextConverter and RtfConverter classes.

Text

Convert text to PDF using the TextConverter class combined with the optional TextConversionOptions class.

TextConverter

The TextConverter class has two constructors that take a string and optionally a TextConversionOptions instance.

The TextConverter class only takes text strings, there are no constructors that accept byte arrays or files.

Add a TextConversionOptions instance through the TextConverter class's constructor or through its ConversionOptions property.

Convert

The TextConverter class has Convert method overloads that output the PDF as a file or byte array. The Convert method is overloaded to allow appending to an existing PDF by passing an input PDF as a byte array (example below). You can also use the TextConversionOptions.AppendToPdf property to append to an existing PDF.

TextConversionOptions

The TextConversionOptions class can also take conversion options to specify how to format the created PDF. Conversion option properties include formatting the created PDF's dimensions, margins, and metadata.

The TextConversionOptions class also has an AppendToPdf property that allows specifying if the created PDF should be appended to a pre-existing PDF.

Examples

The following example illustrates converting a text document to a PDF.

TextConversionOptions options = new(PageSize.Legal, PageOrientation.Portrait, 50);
TextConverter converter = new TextConverter(File.ReadAllText(@"DocumentA.txt")), options);
converter.Convert(outputPdf);
Dim options As New TextConversionOptions(PageSize.Legal, PageOrientation.Portrait, 50)
Dim converter As New TextConverter(File.ReadAllText("DocumentA.txt"), options)
converter.Convert(outputPdf)

The following example illustrates converting a text document to a PDF and appending it to an existing PDF.

byte[] bytes = File.ReadAllBytes(@"DocumentA.pdf");
TextConverter converter = new TextConverter(File.ReadAllText(@"DocumentA.txt"));
byte[] output = converter.Convert(bytes);
File.WriteAllBytes(outputPdf, output);
Dim bytes As Byte() = File.ReadAllBytes("DocumentA.pdf")
Dim converter As New TextConverter(File.ReadAllText("DocumentA.txt"))
Dim output As Byte() = converter.Convert(bytes)
File.WriteAllBytes(outputPdf, output)

Figure 1. Appending to a PDF document

Rich Text Format

Convert text to PDF using the RtfConverter class combined with the optional RtfConversionOptions class.

RtfConverter

The RtfConverter class has constructor overloads allowing passing a byte array or a file path. It also has overloads allowing passing an RtfConversionOptions instance as a parameter.

The Convert method is overloaded to allow appending to an existing PDF by passing an input PDF as a byte array (see text example above). You can also use the RtfConversionOptions.AppendToPdf property to append to an existing PDF.

RtfConversionOptions

The RtfConversionOptions class can also take conversion options to specify how to format the created PDF. Conversion option properties include formatting the created PDF's dimensions, margins, and metadata.

The RtfConversionOptions class also has an AppendToPdf property that allows specifying if the created PDF should be appended to a pre-existing PDF.

Example

RtfConversionOptions options = new(PageSize.Legal, PageOrientation.Portrait, 50);
RtfConverter converter = new(@"DocumentA.rtf"), options);
converter.Convert(outputPdf);
Dim options As New RtfConversionOptions(PageSize.Legal, PageOrientation.Portrait, 50)
Dim converter As New RtfConverter("DocumentA.rtf", options)
converter.Convert(outputPdf)

In this topic