Click or drag to resize

RTF to PDF Converter Element

Note  Note

The RTF to PDF element is part of the .NET Framework 2.0 and 4.0 builds of PDF Creator (the ExpertPdf.PdfCreator package and the Bin folders of the download). The ExpertPdf.PdfCreator.NetCore packages do not include it.

The integrated RTF to PDF Converter is implemented by the RtfToPdfElement graphic element. It offers the possibility to specify the position and the size of the PDF content rendered from RTF and the possibility to add many RTF to PDF conversions to same document.

A very useful feature is the possibility to know the size of the rendered content in each page when the rendered content spans on many pages. The information about the last rendered page can be taken from the AddElementResult object returned after adding the element to a renderer like a page or template.

The RtfToPdfElement offer many constructors that basically call the following constructor with more or less default values for converting a RTF string to PDF:

public RtfToPdfElement(float x, float y, float width, float height, 
    string rtfStringToConvert, int rtfViewerWidth, int rtfViewerHeight);

The constructor creates a RTF to PDF converter element at the specified x and y coordinates with the specified width and height. The virtual RTF viewer width and height in pixels are specified by the rtfViewerWidth and rtfViewerHeight parameters.

  • x - The x position in points where the rendered content will be placed.

  • y - The y position in points where the rendered content will be placed.

  • width - The destination width in points for the rendered content. If the specified with is negative, the destination width will be given by the available width in page or template.

  • height - The destination height in points for the rendered content. If the specified height is negative, the destination height will be auto determined so all the content can be rendered. Please note that the specified height is the effective height that will be rendered in the PDF document and does not include for example the empty spaces introduced by custom or automatic page breaks.

  • rtfStringToConvert - The RTF string to convert to PDF.

  • rtfViewerWidth - The virtual RTF viewer width in pixels. The default value is 800 pixels. When this parameter is negative, the converter will try to auto-determine the RTF document width.

  • rtfViewerHeight - The virtual RTF viewer height in pixels. The default value is 0 which means the height will be auto-determined. The effect of this parameter is similar with viewing the RTF document in a viewer window with the specified width and height. When this parameter is negative, the converter will try to auto-determine the RTF document height.

Sample Code

Below there is a sample code showing how add a RTF to PDF element to a PDF document:

Document document = new Document();
PdfPage page = document.AddPage(PageSize.A4, new Margins(20, 20, 20, 20), PageOrientation.Portrait);

// RTF content at the top of the page, with the width of the page and the height it needs (-1 = automatic);
// the content continues on new pages when it is longer than a page
string rtf = File.ReadAllText("letter.rtf");
RtfToPdfElement rtfElement = new RtfToPdfElement(0, 0, -1, -1, rtf, RtfToPdfElement.DEFAULT_RTF_VIEWER_WIDTH_PX, -1);
rtfElement.FitWidth = true;
rtfElement.EmbedFonts = true;
page.AddElement(rtfElement);

document.Save("letter.pdf");
document.Close();