itext shatp nested <ul> <li> in PdfPcell not printing correctly using c#

人走茶凉 提交于 2020-01-07 06:57:53

问题


Here I have html text data

<ul>
    <li><strong>sf f</strong></li>
    <li><strong>sd gmdslkg &nbsp;</strong>
    <ul>
        <li><strong><span style="color:#FF0000">dsg&nbsp;</span></strong></li>
        <li><span style="color:#FF0000"><strong>ffg&nbsp;</strong></span></li>
        <li><span style="color:#800080"><strong>dfg g fdghdf</strong></span>
        <ul>
            <li><span style="color:#EE82EE"><strong>dsg &nbsp;g</strong></span></li>
            <li><span style="color:#EE82EE"><strong>fdgh d</strong></span></li>
            <li><span style="color:#EE82EE"><strong>ghdf rfh&nbsp;</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&nbsp;</strong></span></li>
            <li><span style="color:#A52A2A"><strong>dfg&nbsp;</strong></span></li>
        </ul>
        </li>
        <li><strong>dfg dfghd r re 5ygtr &nbsp;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&nbsp;</strong></li>
    <li><strong>&nbsp;<span style="color:#FFA500">grews</span></strong></li>
    <li><span style="color:#EE82EE"><strong>dgds g&nbsp;</strong></span></li>
    <li><span style="color:#EE82EE"><strong>gsrd h</strong></span></li>
    <li><span style="color:#800080"><strong>&nbsp;eg dsg w r4ewty 43 dfgbreg</strong></span></li>
    <li><span style="color:#800080"><strong>&nbsp;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
  1. dslmg lmdsg
  2. dsg 
  3.  grews
  4. dgds g 
  5. gsrd h
  6.  eg dsg w r4ewty 43 dfgbreg
  7.  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

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