2

I'm using itextsharp 5.3.2. I've created a custom action using VS2010 to generate a pdf from a SharePoint list item. The string "value" is from an Enhanced Rich Text multiline text field. For the most part, the method below works great to create a PdfPCell with the html content which I can then add to a table in my document. I used the answer from this post to originally write my method: How to get ordered list into pdf using itext?

I've run into a strange issue with an html list, ordered or unordered. It seems that if the list is 3+ levels deep and has multiple items at each level, the html list will fail to parse after the first level 1 item. However, any more content after the ordered list is correctly processed and added to the pdf.

I've wrapped the contents of my method in a try-catch but no exception is caught.

Here is the HTML source of my enhanced rich text field:

<div class="ExternalClassAF9636A4B56F497BB4B962AE682E5CA1"><ol><li>test item level 1</li>
<ol><li>test subitem level 1.1</li>
<ol><li>test subitem level 1.1.1</li>
<li>test subitem level 1.1.2</li>
<li>test subitem level 1.1.3</li></ol>
<li>test subitem level 1.2 <ol><li>test subitem level 1.2.1</li>
<li>test subitem level 1.2.2</li>
<li>test subitem level 1.2.3</li></ol></li>
<li>test subitem level 1.3 <ol><li>test subitem level 1.3.1</li>
<li>test subitem level 1.3.2</li>
<li>test subitem level 1.3.3</li></ol></li></ol>
<li>test item level 2 <ol><li>test subitem level 2.1<ol><li>test subitem level 2.1.1</li>
<li>test subitem level 2.1.2</li>
<li>test subitem level 2.1.3</li></ol></li>
<li>test subitem level 2.2<ol><li>test subitem level 2.2.1</li>
<li>test subitem level 2.2.2</li>
<li>test subitem level 2.2.3</li></ol></li>
<li>test subitem level 2.3<ol><li>test subitem level 2.3.1</li>
<li>test subitem level 2.3.2</li>
<li>test subitem level 2.3.3</li></ol></li></ol></li>
<li>test item level 3 <ol><li>test subitem level 3.1<ol><li>test subitem level 3.1.1</li>
<li>test subitem level 3.1.2</li>
<li>test subitem level 3.1.3</li></ol></li>
<li>test subitem level 3.2<ol><li>test subitem level 3.2.1</li>
<li>test subitem level 3.2.2</li>
<li>test subitem level 3.2.3</li></ol></li>
<li>test subitem level 3.3<ol><li>test subitem level 3.3.1</li>
<li>test subitem level 3.3.2</li>
<li>test subitem level 3.3.3</li></ol></li></ol></li></ol></div>
<p>Some more content after the ordered list.</p>

Here is the method that I'm passing the value into to return a PdfPCell:

const float INDENT = 25f;
...

private static PdfPCell createHtmlCell(string value)
{
    PdfPCell cell = new PdfPCell();  
    cell.BorderWidth = 0f;  
    cell.PaddingLeft = 10f;  

    StyleSheet styles = new StyleSheet();
    styles.LoadTagStyle(HtmlTags.UL, HtmlTags.INDENT, INDENT.ToString());  
    styles.LoadTagStyle(HtmlTags.OL, HtmlTags.INDENT, INDENT.ToString());  
    styles.LoadTagStyle(HtmlTags.LI, HtmlTags.INDENT, INDENT.ToString());  

    using (StringReader sr = new StringReader(value))
    {
        List<IElement> html = HTMLWorker.ParseToList(sr, styles);
        foreach (IElement e in html)
        {
            foreach (Chunk c in e.Chunks)
                c.Font.Size = 11f;

            if (e is Paragraph)
            {
                Paragraph p = (Paragraph)e;
                p.FirstLineIndent = INDENT / 2;
                p.SpacingAfter = 5f;
            }

            cell.AddElement(e);
        }
    }

    return cell;
}

Here is my pdf result: http://screencast.com/t/9OfqZ2c3Vv

Thanks for the help.

Community
  • 1
  • 1
thewire
  • 41
  • 5
  • I would recommend first cleaning up your HTML. You're 1.1 OL starts after of the parent LI is closed instead of before. This might be messing up iTextSharp's HTML parser. – Chris Haas May 31 '13 at 13:43
  • Thanks Chris. I had suspected that too. Unfortunately for me, that's the HTML that is generated by SharePoint. So my question is likely not "why is itextsharp failing?" but more "why is Microsoft SharePoint giving me malformed HTML?" – thewire May 31 '13 at 16:52
  • I don't have an IDE in front of me right now but can you try parsing valid HTML with iTextSharp first and see if that works as expected? If it still doesn't work maybe update your post here. If the valid code does work then I would close this thread and re-post as a specific SharePoint question. Unfortunately I don't know much about SharePoint to help you out – Chris Haas May 31 '13 at 19:09
  • Hello thewire, I have same problem with nested
      and
    • . Not printing properly in nested view. It only print all tags as parent level. Did you found any solution ? Please help me. Thanks
    – Herin Aug 31 '15 at 11:30

0 Answers0