本文介绍了itext shatp nested< ul> <李>使用c#在PdfPcell中无法正确打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我有html文本数据

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>

现在当我尝试使用以下代码打印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

  1. dslmg lmdsg

  2. dsg 

  3.   grews

  4. dgds g 

  5. gsrd h

  6.  例如dsg w r4ewty 43 dfgbreg

  7.   t43 t43tgfdfsgre

  1. dslmg lmdsg
  2. dsg 
  3.  grews
  4. dgds g 
  5. gsrd h
  6.  eg dsg w r4ewty 43 dfgbreg
  7.  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&lt; ul&gt; &LT;李&GT;使用c#在PdfPcell中无法正确打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 04:27