Articles in this section
Category / Section

How to insert an RTF text into a PDF document?

3 mins read

 

Essential PDF supports drawing an RTF text into a PDF document by converting it as bitmap or metafile image. Converting an RTF text into Bitmap option will have good performance while converting RTF text to metafile image will have great resolution and searchable text.

We can render the RTF text into a PDF document by using the method FromRtf() in PdfImage class.

C#

//Read the RTF document

StreamReader reader = new StreamReader("Essential PDF.rtf", Encoding.ASCII);

text = reader.ReadToEnd();

reader.Close();

//Convert it as Bitmap image.

PdfImage bitmap = PdfImage.FromRtf(text, bounds.Width, PdfImageType.Bitmap);

PdfLayoutFormat format = new PdfLayoutFormat();

format.Break = PdfLayoutBreakType.FitPage;

format.Layout = PdfLayoutType.Paginate;

bitmap.Draw(page, 0, 0, format);

//Convert it as metafile.

PdfMetafile metafile = (PdfMetafile)PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile);

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();

format.SplitTextLines = true;

//Draw the image.

metafile.Draw(page, 0, 0, format);

VB

'Read the RTF document

Dim reader As StreamReader = New StreamReader("Essential PDF.rtf", Encoding.ASCII)

text = reader.ReadToEnd()

reader.Close()

'Convert it as Bitmap image.

Dim bitmap As PdfImage = PdfImage.FromRtf(text, bounds.Width, PdfImageType.Bitmap)

Dim format As PdfLayoutFormat = New PdfLayoutFormat()

format.Break = PdfLayoutBreakType.FitPage

format.Layout = PdfLayoutType.Paginate

bitmap.Draw(page, 0, 0, format)

'Convert it as metafile.

Dim metafile As PdfMetafile = CType(PdfImage.FromRtf(text, bounds.Width, PdfImageType.Metafile), PdfMetafile)

Dim format As PdfMetafileLayoutFormat = New PdfMetafileLayoutFormat()

'Allow the text to flow multiple pages without any breaks.

format.SplitTextLines = True

'Draw the image.

metafile.Draw(page, 0, 0, format)

Please do find the sample from the below specified location,

http://websamples.syncfusion.com/samples/PDF.W indows/PDF_RTFSupport.zip


Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.

 

Conclusion:

I hope you enjoyed learning about how to insert an RTF text into a PDF document.

 

You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied