问题描述
这里我有html文本数据
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>
现在当我尝试使用以下代码打印itex-sharp pdf时:
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
- 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
- 例如dsg w r4ewty 43 dfgbreg
- t43 t43tgfdfsgre
- dslmg lmdsg
- dsg
- grews
- dgds g
- gsrd h
- eg dsg w r4ewty 43 dfgbreg
- t43 t43tgfdfsgre
那么,如何使用c#和itextsharp PdfPTable和PdfPCell实现嵌套< ul>
和< li>
标签?
So,how can I achieve this using c# and itextsharp PdfPTable and PdfPCell for nested <ul>
and <li>
tags ?
请帮帮我。
谢谢。
推荐答案
ITextSharp库不支持。
It is not supported in ITextSharp library.
说实话,我不喜欢ITextSharp。从HTML中保存PDF需要做很多工作。我一直在使用的是Nreco.PdfGenerator,在这里找到:
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:
现在,它支持嵌入式CSS,内联CSS,CSS分页,CSS表格行为设置,以及所有内容。您可以免费获得的最佳工具 - > PDF转换。
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.
从HTML生成pdf的示例代码:
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
使用您的示例我得到与HTML结果相同的PDF结果。
Using your example I got same PDF result as HTML result.
这篇关于itext shatp nested< ul> <李>使用c#在PdfPcell中无法正确打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!