2013-04-17 43 views
0

我試圖將XMLWorkerHelper的HTML輸出設置爲PdfPCell而不是Document。我究竟做錯了什麼?XMLWorkerHelper輸出爲PDFCell

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); 
+0

見本關於獲取「IElement」列表的鏈接。一旦你有這些,你應該能夠走過列表並添加對象到單元格。 http://stackoverflow.com/a/15362705/231316 –

+0

非常感謝您的快速回復。我放棄了。 –

回答

1

而是與父IElement這是大多數格式應用於個人Chunk工作的工作。並非所有IElements可以加入,所以你需要檢查IsContent()布爾屬性:

PdfPCell P4Type1 = new PdfPCell(); 
foreach (var element in mh.elements) { 
    if (element.IsContent()) { 
     P4Type1.AddElement(element); 
    } 
} 
+0

非常感謝你,再次感謝你的時間。我很快就發佈了一個完整的工作版本,它可能會幫助其他人。 –

+0

好的。我正在跟進這篇文章。 [link] http://stackoverflow.com/questions/16101481/use-external-css-to-parse-xml。[/ link]這是一個如何將字符串中的html解析爲PDFCEll的工作示例。現在如何使用外部CSS文件迴避我。 –

1

此代碼測試OK,,給予HTML串入PdfPCell到PDF文件中寫

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);