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

醉酒当歌 提交于 2019-11-27 16:15:36

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#.

Finally I write this code in c# which is working perfectly, Thanks to Bruno who helped me to understand XMLWorker.

Here is an example using XMLWorker in C#.

I used a sample HTML as 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 have created Test.css file and saved it in SharePoint Style Library. (for this test I saved it in D drive 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 in D drive. ( In SharePoint I used Memorystream. I keep this example very simple 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 Test.pdf file adding my HTML with Font Family:Arial. So all of the Welsh Characters can be saved in PDF file.

Note: I have 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 useful.

Kate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!