0

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.

Herin
  • 704
  • 3
  • 18
  • 34
  • 2
    I wanted to mark this as a duplicate question, because what is asked is not supported as explained in this answer: [How can I convert XHTML nested list to pdf with iText?](http://stackoverflow.com/questions/26755315/how-can-i-convert-xhtml-nested-list-to-pdf-with-itext) Unfortunately, that answer wasn't accepted, nor upvoted, so I can't use it as a reason to close this question. I guess people just don't like no for an answer... – Bruno Lowagie Aug 27 '15 at 07:37
  • Hello Bruno, but here in this http://demo.itextsupport.com/xmlworker/ demo site, it will working fine when I create nested
      and
    • and it will print in Pdf, same as web view looking. Thanks.
    – Herin Aug 27 '15 at 08:47
  • But you're using `HTMLWorker`, not XML Worker. `HTMLWorker` has been abandoned many years ago in favor of XML Worker. Also, if you take a look at the [ListInCell](http://itextpdf.com/sandbox/tables/ListInCell) example, you'll see that you need a recent version of iText if you want lists in cells. Maybe you're using an obsolete version of iText. – Bruno Lowagie Aug 27 '15 at 09:18
  • Hello, I am using Version 5.5.4.0 of itextsharp. but when I have included in my solution and also give reference in my page. But I did not able to access Xml Worker. Can you please give some demo/sample for the same to use Xml worker for nested
    • in PdfPCell ? Thanks
    – Herin Aug 27 '15 at 10:48
  • XML Worker requires an extra DLL: http://itextpdf.com/product/xml_worker You can download it here: http://sourceforge.net/projects/itextsharp/files/xmlworker/ – Bruno Lowagie Aug 27 '15 at 11:03
  • I have already itextsharp.xmlworker DLL with version 5.5.4.0 is available and included in the solution and page. but I am not able to access the XML Worker class in iTextSharp.text.xml namespace. And also how can I travers the loop for all dynamic nested list in PdfPCell in PdfpTable. Can you please provide sample the same. Thanks. – Herin Aug 27 '15 at 11:18
  • I am a Java developer; I don't write C# code. You can find Java examples here: http://itextpdf.com/sandbox/xmlworker There are many C# examples to be found in other answers on StackOverflow. – Bruno Lowagie Aug 27 '15 at 11:37
  • @BrunoLowagie, upvote added – Chris Haas Aug 27 '15 at 13:09

1 Answers1

0

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.

DonBaton
  • 43
  • 7