An RTF document can be rendered in the pages of a PDF document created with the PdfDocument class, between the text, images and other elements added from code. The content starts at a position in the current page and flows to as many new pages as it needs, laid out inside the margins of the PDF document. The document keeps its formatting, headings, links and images.
The document is given to a PdfRtfElement object and rendered with the RtfPdfDocumentExtensionsAddRtf(PdfDocument, PdfRtfElement) extension method of the PDF document. The method returns a PdfContentRenderInfo object describing the pages and the areas where the content was rendered. The RtfPdfDocumentExtensionsAddRtfAsync(PdfDocument, PdfRtfElement, CancellationToken) method is the asynchronous variant.
The element is created with PdfRtfElementPdfRtfElement(Byte) from the content of an RTF file read into a byte array or with PdfRtfElementPdfRtfElement(String) from a string: a value that contains the RTF header is the document itself and any other value is the path of the RTF file.
// A RTF string is given to the element as it is
PdfRtfElement rtfElement = new PdfRtfElement(rtfString);
// The content of a RTF file
PdfRtfElement rtfFileElement = new PdfRtfElement(inputRtfBytes);The X and Y properties give the position where the content starts, in points and relative to the top left corner of the content area of the current page, inside the margins. The content keeps the left offset on the next pages, where it starts at the top margin. Width is the width of the content in points, or 0 to extend it to the right margin of the page. Zoom scales the rendered content, in percents from 10 to 200, with 100 as the default.
The headings of the RTF document join the outline of the PDF document when GenerateDocumentOutline is true. The FontFiles and FallbackFontFiles lists give more fonts to the content, in addition to the fonts installed on the machine.
rtfElement.X = 0;
rtfElement.Y = crtYPos;
// 0 extends the content to the right margin of the page
rtfElement.Width = 0;
rtfElement.Zoom = 100;
// The headings of the RTF document join the outline of the PDF document
rtfElement.GenerateDocumentOutline = true;
PdfContentRenderInfo rtfRenderInfo = pdfDocument.AddRtf(rtfElement);The returned PdfContentRenderInfo object exposes the FirstPageNumber, LastPageNumber and PagesCount of the content and the area it covers on each page in RenderedRectangles. The elements added after the content go to its last page, below the LastPageRectangle.
// Use EVO PDF Namespace
using EvoPdf.Next;
public byte[] AddRtfToPdfDocument(byte[] inputRtfBytes, string fontFilePath)
{
// Set the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.LicenseKey = "your-license-key";
// The RTF content from a file read into memory; a RTF string can also be given to the element
PdfRtfElement rtfElement = new PdfRtfElement(inputRtfBytes);
PdfDocumentCreateSettings pdfCreateSettings = new PdfDocumentCreateSettings()
{
PageSize = PdfPageSize.A4,
PageOrientation = PdfPageOrientation.Portrait,
Margins = new PdfMargins(36, 36, 36, 36),
Language = "en-US"
};
using PdfDocument pdfDocument = new PdfDocument(pdfCreateSettings);
pdfDocument.PdfDocumentInfo.Title = "RTF Content in a PDF Document";
PdfBaseFont baseFont = PdfFontManager.CreateBaseFont(fontFilePath);
PdfFont titleFont = PdfFontManager.CreateFont(baseFont, 16f, PdfFontStyle.Bold, PdfColor.DarkBlue);
PdfFont textFont = PdfFontManager.CreateFont(baseFont, 10f, PdfFontStyle.Normal, PdfColor.Black);
// Elements created with the PDF creator before the document content
PdfTextElement titleElement = new PdfTextElement("RTF Content in a PDF Document", titleFont)
{
X = 0,
Y = 0,
Width = pdfDocument.ContentWidth,
Alignment = PdfTextAlignment.Center
};
titleElement.Accessibility.StructureType = PdfStructureType.Heading1;
float crtYPos = pdfDocument.AddText(titleElement).LastPageRectangle.Bounds.Bottom + 10;
PdfTextElement introElement = new PdfTextElement(
"The text above and this paragraph are text elements of the PDF document. The RTF content " +
"starts below them and continues on new pages when it does not fit in this one. " +
"The elements added after the RTF content go to its last page, below it.", textFont)
{
X = 0,
Y = crtYPos,
Width = pdfDocument.ContentWidth
};
crtYPos = pdfDocument.AddText(introElement).LastPageRectangle.Bounds.Bottom + 15;
// The RTF content is rendered in the PDF document from the given position of the current
// page and inside the page margins. The X position is kept on the next pages
rtfElement.X = 0;
rtfElement.Y = crtYPos;
// 0 extends the content to the right margin of the page
rtfElement.Width = 0;
rtfElement.Zoom = 100;
// The headings of the RTF document join the outline of the PDF document
rtfElement.GenerateDocumentOutline = true;
PdfContentRenderInfo rtfRenderInfo = pdfDocument.AddRtf(rtfElement);
// The rendered area reported for the last page places the elements after the content
crtYPos = rtfRenderInfo.LastPageRectangle.Bounds.Bottom + 15;
PdfRectangleElement separator = new PdfRectangleElement(0, crtYPos, pdfDocument.ContentWidth, 1)
{
FillColor = PdfColor.DarkBlue,
BorderColor = null
};
pdfDocument.AddRectangle(separator);
PdfTextElement closingElement = new PdfTextElement(
string.Format("The RTF content was rendered on {0} page(s), from page {1} to page {2}. This text was added after it.",
rtfRenderInfo.PagesCount, rtfRenderInfo.FirstPageNumber, rtfRenderInfo.LastPageNumber), textFont)
{
X = 0,
Y = crtYPos + 8,
Width = pdfDocument.ContentWidth
};
pdfDocument.AddText(closingElement);
// The buffer to receive the generated PDF document
byte[] outPdfBuffer = pdfDocument.Save();
return outPdfBuffer;
}