问题
Here I have html text data
<ul>
<li><strong>sf f</strong></li>
<li><strong>sd gmdslkg </strong>
<ul>
<li><strong><span style="color:#FF0000">dsg </span></strong></li>
<li><span style="color:#FF0000"><strong>ffg </strong></span></li>
<li><span style="color:#800080"><strong>dfg g fdghdf</strong></span>
<ul>
<li><span style="color:#EE82EE"><strong>dsg g</strong></span></li>
<li><span style="color:#EE82EE"><strong>fdgh d</strong></span></li>
<li><span style="color:#EE82EE"><strong>ghdf rfh </strong></span>
<ul>
<li><span style="color:#FFD700"><strong>hdf</strong></span></li>
<li><span style="color:#FFD700"><strong>fdg dfghh</strong></span></li>
<li><span style="color:#FFD700"><strong>bh dfh</strong></span></li>
</ul>
</li>
<li><span style="color:#A52A2A"><strong>dfgh dfh </strong></span></li>
<li><span style="color:#A52A2A"><strong>dfg </strong></span></li>
</ul>
</li>
<li><strong>dfg dfghd r re 5ygtr ger</strong></li>
</ul>
</li>
<li><span style="color:#FF8C00"><strong>gds mgds</strong></span></li>
<li><span style="color:#B22222"><strong>dsfg sdg</strong></span></li>
</ul>
<ol>
<li><span style="color:#40E0D0"><strong>dslmg lmdsg</strong></span></li>
<li><strong>dsg </strong></li>
<li><strong> <span style="color:#FFA500">grews</span></strong></li>
<li><span style="color:#EE82EE"><strong>dgds g </strong></span></li>
<li><span style="color:#EE82EE"><strong>gsrd h</strong></span></li>
<li><span style="color:#800080"><strong> eg dsg w r4ewty 43 dfgbreg</strong></span></li>
<li><span style="color:#800080"><strong> t43 t43tgfdfsgre</strong></span></li>
</ol>
and now when I trying to print in itex-sharp pdf using below code :
var doc = new iTextSharp.text.Document();
doc.Open();
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
var tmpCellNote = new PdfPCell(new Phrase("Subcontractor Notes: ", noteFont)) { Border = 0, HorizontalAlignment = Element.ALIGN_LEFT, Colspan = 5 };
tmpCellNote.PaddingLeft = (10 * schedule.Level);
var objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(Convert.ToString(schedule.Notes)), ST);
for (int k = 0; k < objects.Count; ++k)
{
tmpCellNote.AddElement((IElement)objects[k]);
}
table.AddCell(tmpCellNote);
doc.Add(table);
using above code, it will printing like this:
- sf f
- sd gmdslkg
- dsg
- ffg
- dfg g fdghdf
- dsg g
- fdgh d
- ghdf rfh
- hdf
- fdg dfghh
- bh dfh
- dfgh dfh
- dfg
- dfg dfghd r re 5ygtr ger
- gds mgds
- dsfg sdg
- dslmg lmdsg
- dsg
- grews
- dgds g
- gsrd h
- eg dsg w r4ewty 43 dfgbreg
- t43 t43tgfdfsgre
So,how can I achieve this using c# and itextsharp PdfPTable and PdfPCell for nested <ul>
and <li>
tags ?
Please help me.
Thanks.
回答1:
It is not supported in ITextSharp library.
To be honest, I don't like ITextSharp. Takes so much work to save PDF from HTML. What I'm always using is Nreco.PdfGenerator, found here:
PDF GENERATOR
Now, it supports embedded CSS,inline CSS, page breaking by CSS, table behaviour setting by CSS, well, everything. Best tool you could ever find for free to get HTML -> PDF conversion.
Example code for generating pdf from HTML:
var generator = new NReco.PdfGenerator.HtmlToPdfConverter();
var fileLines = System.IO.File.ReadAllText(@"C:\TEMP\test.html"); //you can always use ANY html string, not exactly file
var pdfBytes = generator.GeneratePdf(fileLines); //returns PDF byte[]
System.IO.File.WriteAllBytes(@"C:\TEMP\test.pdf", pdfBytes); //save to PDF
Using your example I got same PDF result as HTML result.
来源:https://stackoverflow.com/questions/32243210/itext-shatp-nested-ul-li-in-pdfpcell-not-printing-correctly-using-c-sharp