How can I add a rich text file (HTML) to a table cell?

I have a rich text box named: "DocumentContent" which I'm going to add to the PDF using the code below:

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 the PDF, the content is displayed as HTML and not text as shown 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 keep all the styles the user applies to the rich text and just change the font family to Arial.

I can change the font family, but I need to decode this content from HTML to text.

Could you advise? Thanks to

+3


source to share


2 answers


Please see 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 tag <p>

:

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

      

In your case, you can replace Cardo

with Arial

.

Please note that we have registered the regular version of the font Cardo

:



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

      

If you want bold, italic, and bold, you also need to register these fonts from the same family Cardo

. (In the case of arial, you must register arial.ttf, arialbd.ttf, ariali.ttf and arialbi.ttf).

We can now parse this HTML and CSS into a list of objects Element

using the method parseToElementList()

. 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 don't have the time / skill to provide this example in iTextSharp, but it is very easy to port it to C #.

+3


source


Finally, I am writing this code in C # which works great thanks to Bruno who helped me understand XMLWorker.

Here is an example using XMLWorker in C #.

I used sample HTML as shown below:

public static 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>";

      

I created a Test.css file and saved it in a SharePoint style library. (for this test, I saved it to D disk to keep it simple) Here is the content of my test css file: p {font-family: arial; }

Then, using the below C # code, I saved the PDF file to D disk. (In SharePoint I used Memorystream. I find this example very easy to understand)



string fileName = @"D:\Test.pdf";                     
var css = @"D:\Test.css";
using (var ActionStream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(HTML)))
{
    using (FileStream cssFile = new FileStream(css, FileMode.Open))
    {
        var document = new Document(PageSize.A4, 30, 30, 10, 10);                    
        var worker = XMLWorkerHelper.GetInstance();
        var writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
        document.Open();
        worker.ParseXHtml(writer, document, ActionStream, cssFile);
        writer.CloseStream = false;
        document.Close();

    }
}

      

It creates a Test.pdf file adding my HTML with font family: Arial. In this way, all Welsh characters can be saved in a PDF file.

Note. I added iTextSharp.dll v: 5.5.3 and XMLworker.dll v: 5.5.3 to my project.

using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.css;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline;

      

Hope this can be helpful.

Kate

+2


source







All Articles