0

I am trying to set the HTML output from the XMLWorkerHelper into a PdfPCell instead of the Document. What am I doing wrong?

string html = "<h1>Test h1 Heading</h1><ul><li>html 1</li><li>html 2</li><li>html 3</li></ul>";

XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(html));

PdfPCell P4Type1 = new PdfPCell();
P4Tabel.AddCell(P4Type1); 
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Karl Wilson
  • 72
  • 1
  • 5
  • See this link about getting a list of `IElement`. Once you have those you should be able to walk the list and add the objects to a cell. http://stackoverflow.com/a/15362705/231316 – Chris Haas Apr 17 '13 at 13:52

2 Answers2

1

Instead of working with an individual Chunk work with the parent IElement which is where most of the formatting is applied. Not all IElements can be added so you'll need to check the IsContent() boolean property:

PdfPCell P4Type1 = new PdfPCell();
foreach (var element in mh.elements) {
    if (element.IsContent()) {
        P4Type1.AddElement(element);
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Thanks a lot that did it, thanks you again for your time. Ill post a full working version with some notes soon, os it may help others. – Karl Wilson Apr 18 '13 at 13:32
  • ok. i am following up this post here. [link]http://stackoverflow.com/questions/16101481/use-external-css-to-parse-xml.[/link] this is a working example of how to parse html from a string into a PDFCEll. now how to use external CSS file evades me. – Karl Wilson Apr 19 '13 at 09:30
1

This code is tested ok, that giving html string into PdfPCell to write in pdf document

string htmlFolioNotice = " <P align=center ><B> Thank you for your stay with us.Please visit us again.</B></P> " + "<P align=justify>NOTICE TO GUESTS: This property is privately owned and the management reserves the right to refuse service to anyone. Management will not be responsible for accidents or injury to guests or for loss of money, jewelry or valuables of any kind. Management will not be responsible for any item left in the room.<BR>&nbsp;<BR>CHECKOUT TIME: 11:00 AM SELF REGISTRATION ONLY I AGREE that my liability for this bill is not waived and agree to be held personally liable in the event that the indicated person or company failed to pay for any part or full amount of these charges including any missing/damaged items, etc.. I agreee that if an attorney is retained to collect these charges, I will pay all reasonable attorney's fees and costs incurred. If payment is by credit card you are authorized to charge my account for all charges incurred, including any and all damages/missing items, etc.. I agree that the sole purpose of renting this room is for my own residency only. Damge to property,Disturbance to guest or employees or Law Enforcement comes to your room. Immediate check out with no refund</P>";`enter code here`
PdfPTable tblNotise = new PdfPTable(1) { WidthPercentage = 100, HorizontalAlignment = 1, SpacingBefore = 20f };
    int[] tblNotisewidth = { 100 };
    tblNotise.SetWidths(tblNotisewidth);

    PdfPCell contentCell = new PdfPCell();
    contentCell.Border = 0;
    var htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlFolioNotice), null);
    for (int k = 0; k < htmlarraylist.Count; k++)
    {
        var ele = (IElement)htmlarraylist[k];
        contentCell.AddElement(ele);
    }
    tblNotise.AddCell(contentCell);
    docPDF.Add(tblNotise);
Pradip Talaviya
  • 399
  • 2
  • 8
  • 22