0

I have a basic .vm file which I populate with XML, and then use iText to convert it to a PDF doc:

<?xml version="1.0" encoding="UTF-8"?>
<!--<!DOCTYPE itext SYSTEM "http://itext.sourceforge.net/itext.dtd">-->
<itext creationdate='${System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")}' producer="X">
  <paragraph align="Default" size="18.0" style="bold" indentationleft="0.0">
    Test
  </paragraph>
</itext>

The above is parsed absolutely fine. However something slightly more complicated:

<?xml version="1.0" encoding="UTF-8"?>
<!--<!DOCTYPE itext SYSTEM "http://itext.sourceforge.net/itext.dtd">-->
<itext creationdate='${System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")}' producer="X">
  <paragraph align="Default" size="18.0" style="bold" indentationleft="0.0">
    Test
  </paragraph>
  <table>
    <row>
      <cell>
        <paragraph>
          Table test
        </paragraph>
      </cell>
    </row>
  </table>
</itext>

Fails with exception "Unable to cast object of type 'iTextSharp.text.Paragraph' to type 'iTextSharp.text.Table'."

Any ideas? Certainly I can't see any formatting issues.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
FBryant87
  • 4,273
  • 2
  • 44
  • 72

1 Answers1

1

First, if you are doing XML to PDF using the iText DTD then you are using the very old and obsolete 4.x series of iTextSharp from 2008/2009. That version had many bugs that the 5.x series fixed. If you are using that version because of the license I would encourage you (and your legal representatives) to read the fourth block on the iText Sales FAQ page title Why Shouldn't I Use iText 2.x (or iTextSharp 4.x)?.

Second, but related to the first, there's a modern way to perform XML to PDF. Read through this answer which goes into much more detail and has links from the author of iText himself explaining why you shouldn't use the DTD anymore.

Finally, if you're still going to use the DTD method, some people say that you can place an <ignore/> tag between your closing </paragraph> and opening <table> tag which will iText's parser to skip the whitespace which is a known problem. This post also talks about a way to use an XmlTextReader to parse the XML. And lastly, you could just try removing all whitespace between between XML tags to see if that fixes things.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274