Click or drag to resize

RTF to PDF Converter Element

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:

PdfPage page = document.Pages.AddNewPage(PageSize.A4, new Margins(10, 10, 0, 0), PageOrientation.Portrait);

// the code below can be used to create a page with default settings A4, document margins inherited, portrait orientation
//PdfPage page = document.Pages.AddNewPage();

// add a font to the document that can be used for the texts elements 
PdfFont font = document.Fonts.Add(new Font(new FontFamily("Times New Roman"), 10, GraphicsUnit.Point));

// add header and footer before rendering the content
if (cbAddHeader.Checked)
    AddHtmlHeader(document);
if (cbAddFooter.Checked)
    AddHtmlFooter(document, font);

// the result of adding an element to a PDF page
AddElementResult addResult;

// Get the specified location and size of the rendered content
// A negative value for width and height means to auto determine
// The auto determined width is the available width in the PDF page
// and the auto determined height is the height necessary to render all the content
float xLocation = float.Parse(textBoxXLocation.Text.Trim());
float yLocation = float.Parse(textBoxYLocation.Text.Trim());
float width = float.Parse(textBoxWidth.Text.Trim());
float height = float.Parse(textBoxHeight.Text.Trim());

// Create the RTF to PDF converter element
string rtfString = System.IO.File.ReadAllText(textBoxRtfFile.Text.Trim());
RtfToPdfElement rtfToPdfElement = new RtfToPdfElement(xLocation, yLocation, width, height, rtfString, RtfToPdfElement.DEFAULT_RTF_VIEWER_WIDTH_PX, -1);

//optional settings for the RTF to PDF converter
rtfToPdfElement.FitWidth = cbFitWidth.Checked;
rtfToPdfElement.EmbedFonts = cbEmbedFonts.Checked;

// add theHTML to PDF converter element to page
addResult = page.AddElement(rtfToPdfElement);