Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a rich Textbox (HTML) to a table cell?

I have a rich text box named:”DocumentContent” which I’m going to add its content to pdf using the below code:

iTextSharp.text.Font font = FontFactory.GetFont(@"C:\Windows\Fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12f, Font.NORMAL, BaseColor.BLACK);
            DocumentContent = System.Web.HttpUtility.HtmlDecode(DocumentContent);
            Chunk chunkContent = new Chunk(DocumentContent);
            chunkContent.Font = font;            

            Phrase PhraseContent = new Phrase(chunkContent);
            PhraseContent.Font = font;


            PdfPTable table = new PdfPTable(2);
            table.WidthPercentage = 100;
            PdfPCell cell;

            cell = new PdfPCell(new Phrase(PhraseContent));
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

The problem is when I open PDF file the content appears as HTML not a text as below:

<p>Overview&#160; line1 </p><p>Overview&#160; line2
</p><p>Overview&#160; line3 </p><p>Overview&#160;
line4</p><p>Overview&#160; line4</p><p>Overview&#160;
line5&#160;</p>

But it should look like below

Overview line1
Overview line2
Overview line3
Overview line4
Overview line4
Overview line5

What I'm going to do is to keep all the styling which user apply to the rich text and just change font family to Arial.

I can change Font Family but I need to Decode this content from HTML to Text.

Could you please advise? Thanks

like image 983
Kate Avatar asked Dec 25 '22 02:12

Kate


1 Answers

Please take a look at the HtmlContentForCell example.

In this example, we have the HTML you mention:

public static final String HTML = "<p>Overview&#160;line1</p>"
        + "<p>Overview&#160;line2</p><p>Overview&#160;line3</p>"
        + "<p>Overview&#160;line4</p><p>Overview&#160;line4</p>"
        + "<p>Overview&#160;line5&#160;</p>";

We also create a font for the <p> tag:

public static final String CSS = "p { font-family: Cardo; }";

In your case, you may want to replace Cardo with Arial.

Note that we registered the regular version of the Cardo font:

FontFactory.register("resources/fonts/Cardo-Regular.ttf");

If you need bold, italic and bold-italic, you also need to register those fonts of the same Cardo family. (In case of arial, you'd register arial.ttf, arialbd.ttf, ariali.ttf and arialbi.ttf).

Now we can parse this HTML and CSS into a list of Element objects with the parseToElementList() method. We can use these objects inside a cell:

PdfPTable table = new PdfPTable(2);
table.addCell("Some rich text:");
PdfPCell cell = new PdfPCell();
for (Element e : XMLWorkerHelper.parseToElementList(HTML, CSS)) {
    cell.addElement(e);
}
table.addCell(cell);
document.add(table);

See html_in_cell.pdf for the resulting PDF.

I do not have the time/skills to provide this example in iTextSharp, but it should be very easy to port this to C#.

like image 151
Bruno Lowagie Avatar answered Dec 30 '22 10:12

Bruno Lowagie