import block of rtf into table cell in table in radfixedpage

1 Answer 32 Views
PdfProcessing
Andrew
Top achievements
Rank 1
Andrew asked on 23 Feb 2024, 09:05 AM

I am trying to create a pdf page using the pdfprocessing library.

The page has a number of tables with mostly text and a few images. I think I am ok with handling these.

I am building up the page in a RadFixedPage added to a RadFixedDocument.

However, in one of the table cells I need to be able to insert a block of rtf formatted text.

This is user entered using a RadRichTextBox and saved to a string field in code-behind using RtfDataProvider

A typical block of rtf may be:

{\rtf1\ansi\ansicpg1252\uc1\deff0\deflang1033{\fonttbl{\f0 Segoe UI;}{\f1 Times New Roman;}{\f2 Verdana;}}{\colortbl\red0\green0\blue0 ;\red0\green176\blue80 ;\red255\green192\blue0 ;\red250\green250\blue250 ;\red0\green0\blue255 ;}{\*\defchp\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone}{\*\defpap\sl276\slmult1\ql\sa180\ltrpar}{\stylesheet{\s0\sqformat\spriority0\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone\sl276\slmult1\ql\ltrpar Normal;}{\*\ts2\tsrowd\spriority59\trbrdrt\brdrnone\trbrdrb\brdrnone\trbrdrl\brdrnone\trbrdrr\brdrnone\trbrdrh\brdrnone\trbrdrv\brdrnone\trgaph0\trpaddl75\trpaddr75\trpaddt0\trpaddb0\clpadft3\clpadt0\clpadfr3\clpadr0\clpadfl3\clpadl0\clpadfb3\clpadb0\tsvertalt\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone\sl276\slmult1\ql\sa180\ltrpar Table Normal;}{\s3\spriority99\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone\sl276\slmult1\ql\sb100\sa100\sbauto1\saauto1\ltrpar Normal (Web);}}\nouicompat\viewkind4\sectd\headery720\footery720\pard\sl276\slmult1\qc\ltrpar{\ltrch\f0\fs18\i\b\strike0\cf1\cb2\highlight2\ulc0\ulnone line }{\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone 1}{\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone\par}\pard\sl276\slmult1\ql\ltrpar{\ltrch\f1\fs18\i\b0\strike0\cf0\cb3\highlight3\ulc0\ulnone sDefaultStyleSettings}{\ltrch\f1\fs18\i0\b0\strike0\cf0\cb3\highlight3\ulc0\ulnone =}{\ltrch\f1\fs18\i0\b0\strike0\cf4\cb3\highlight3\ulc0\ulnone "True"}{\ltrch\f0\fs18\i0\b\strike0\cf0\ulc0\ulnone\par}\pard\sl276\slmult1\ql\ltrpar{\ltrch\f0\fs18\i0\b0\strike0\cf0\ulc0\ulnone linr 3s}{\ltrch\f0\fs18\i0\b\strike0\cf0\ulc0\ulnone lin 2}{\ltrch\f1\fs18\i0\b0\strike0\cf0\cb3\highlight3\ulc0\ulnone DocumentInherit}{\ltrch\f0\fs18\i0\b\strike0\cf0\ulc0\ulnone\par}}

 

For inserting normal text I have been using

            row = table.Rows.AddTableRow();
            cell = row.Cells.AddTableCell();
            cell.Blocks.AddBlock().InsertText(Language.Language.ProductName);

any suggestions how I can insert rtf rather than plain text?

Thanks

Andrew

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 26 Feb 2024, 05:20 PM

Hello Andrew,

The most convenient and out-of-the-box approach to achieve the desired PDF file is to initially create the document and all its contents as a RadFlowDocument with the WordsProcessing library and then export it to PDF:

RtfFormatProvider rtfProvider = new RtfFormatProvider();

RadFlowDocument rtfDocument = rtfProvider.Import("{\\rtf1\\ansi\\ansicpg1252\\uc1\\deff0\\deflang1033{\\fonttbl{\\f0 Segoe UI;}{\\f1 Times New Roman;}{\\f2 Verdana;}}{\\colortbl\\red0\\green0\\blue0 ;\\red0\\green176\\blue80 ;\\red255\\green192\\blue0 ;\\red250\\green250\\blue250 ;\\red0\\green0\\blue255 ;}{\\*\\defchp\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone}{\\*\\defpap\\sl276\\slmult1\\ql\\sa180\\ltrpar}{\\stylesheet{\\s0\\sqformat\\spriority0\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone\\sl276\\slmult1\\ql\\ltrpar Normal;}{\\*\\ts2\\tsrowd\\spriority59\\trbrdrt\\brdrnone\\trbrdrb\\brdrnone\\trbrdrl\\brdrnone\\trbrdrr\\brdrnone\\trbrdrh\\brdrnone\\trbrdrv\\brdrnone\\trgaph0\\trpaddl75\\trpaddr75\\trpaddt0\\trpaddb0\\clpadft3\\clpadt0\\clpadfr3\\clpadr0\\clpadfl3\\clpadl0\\clpadfb3\\clpadb0\\tsvertalt\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone\\sl276\\slmult1\\ql\\sa180\\ltrpar Table Normal;}{\\s3\\spriority99\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone\\sl276\\slmult1\\ql\\sb100\\sa100\\sbauto1\\saauto1\\ltrpar Normal (Web);}}\\nouicompat\\viewkind4\\sectd\\headery720\\footery720\\pard\\sl276\\slmult1\\qc\\ltrpar{\\ltrch\\f0\\fs18\\i\\b\\strike0\\cf1\\cb2\\highlight2\\ulc0\\ulnone line }{\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone 1}{\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone\\par}\\pard\\sl276\\slmult1\\ql\\ltrpar{\\ltrch\\f1\\fs18\\i\\b0\\strike0\\cf0\\cb3\\highlight3\\ulc0\\ulnone sDefaultStyleSettings}{\\ltrch\\f1\\fs18\\i0\\b0\\strike0\\cf0\\cb3\\highlight3\\ulc0\\ulnone =}{\\ltrch\\f1\\fs18\\i0\\b0\\strike0\\cf4\\cb3\\highlight3\\ulc0\\ulnone \"True\"}{\\ltrch\\f0\\fs18\\i0\\b\\strike0\\cf0\\ulc0\\ulnone\\par}\\pard\\sl276\\slmult1\\ql\\ltrpar{\\ltrch\\f0\\fs18\\i0\\b0\\strike0\\cf0\\ulc0\\ulnone linr 3s}{\\ltrch\\f0\\fs18\\i0\\b\\strike0\\cf0\\ulc0\\ulnone lin 2}{\\ltrch\\f1\\fs18\\i0\\b0\\strike0\\cf0\\cb3\\highlight3\\ulc0\\ulnone DocumentInherit}{\\ltrch\\f0\\fs18\\i0\\b\\strike0\\cf0\\ulc0\\ulnone\\par}}");

RadFlowDocument mainDocument = new RadFlowDocument();
var section = mainDocument.Sections.AddSection();
var editor = new RadFlowDocumentEditor(mainDocument);

//Create table with formatting
Table table = section.Blocks.AddTable();
mainDocument.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.TableGridStyleId);
table.StyleId = BuiltInStyleNames.TableGridStyleId;

table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
TableRow row = table.Rows.AddTableRow();
TableCell cell = row.Cells.AddTableCell();

//Crete paragraph for content
var paragraph = cell.Blocks.AddParagraph();
//Move editor to the start of the paragraph
editor.MoveToParagraphStart(paragraph);
//Draw RTF in paragraph
editor.InsertDocument(rtfDocument);

PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
using (Stream output = File.OpenWrite("output.pdf"))
{
    pdfFormatProvider.Export(mainDocument, output);
}

Process.Start("output.pdf");

I understand that you are currently starting with a RadFixedDocument, but if you are satisfied with the RadFlowDocument approach and would like to go with it here are some related articles from our documentation that can help you with the implementation:

I hope this helps.

Regards,
Yoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing
Asked by
Andrew
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or