Skip to content

RTF document to PDF

Converts an RTF document stream to PDF bytes by rendering the RTF text as a metafile and drawing it across as many pages as the content requires.

Field Value
Operation ID OP-SFC-005
Trigger Called by Execute a single conversion (converter template) via RtfConverter.ConvertItem, dispatched from Convert a source document to PDF for rtf
Inputs Source Stream, ConvertRequest
Outputs PDF byte[]
Primary components RtfConverter.ConvertItem
Entry point PdfApi.SyncFusionCore\Convert\RtfConverter.cs

Steps

  1. Choose conformance: Pdf_A1B when IsPdfa, otherwise None; create a PdfDocument with it (BR-SFC-011).
  2. Add one page and read its client bounds.
  3. Read the whole stream as ASCII text (StreamReader(stream, Encoding.ASCII)) (BR-SFC-012).
  4. Convert the RTF text to a metafile with PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile).
  5. Set PdfMetafileLayoutFormat.SplitTextLines = true so text flows across pages without a break, and draw the metafile at origin (BR-SFC-013).
  6. Save to a MemoryStream, close the document, and return the bytes.

Process flow

The following diagram visualizes this operation's control flow.

flowchart TD
    Start(["ConvertItem called<br>with stream, request"]) --> Conf["Choose conformance<br>Pdf_A1B or None"]
    Conf --> Create["Create PdfDocument,<br>add one page"]
    Create --> Read[Read stream as ASCII text]
    Read --> Meta["Convert text to metafile<br>via PdfImage.FromRtf"]
    Meta --> SplitLines["Set SplitTextLines = true,<br>draw metafile at origin"]
    SplitLines --> Save["Save to MemoryStream,<br>close document"]
    Save --> Return([Return PDF bytes])

Failure and edge-case behavior

  • Any Syncfusion conversion failure surfaces as an exception, which is caught by Execute a single conversion (converter template) and turned into a failed PdfResult rather than propagated here.
  • Non-ASCII characters in the source RTF are not preserved because the stream is read with an ASCII StreamReader.

Sources

  • PdfApi.SyncFusionCore\Convert\RtfConverter.cs