XML的iText解析器异常 - > PDF转换 [英] iText parser exception for XML -> PDF conversion

查看:94
本文介绍了XML的iText解析器异常 - > PDF转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的.vm文件,我用XML填充,然后使用iText将其转换为PDF文档:

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>

失败,异常无法将'iTextSharp.text.Paragraph'类型的对象强制转换为'iTextSharp' .text.Table'。

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.

推荐答案

首先,如果您使用iText DTD进行XML到PDF,那么你从2008/2009开始使用非常陈旧和过时的4.x系列iTextSharp。该版本有5.x系列修复的许多错误。如果您因许可使用该版本,我会鼓励您(以及您的法律代表)阅读 iText上的第四个块。销售常见问题页面标题为什么我不应该使用iText 2.x(或iTextSharp 4.x)?

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)?.

秒,但与第一种相关,有一种现代的方式来执行XML到PDF。阅读通过此答案,该答案更加详细,并且有来自iText的作者本人的链接,解释了为什么不应该使用DTD了。

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.

最后,如果您仍然要使用DTD方法,有人会说您可以放置​​< ignore /> 在您的结算< / paragraph> 和开启< table> 标签,iText的解析器将跳过这个已知问题的空白。 这篇文章还讨论了使用 XmlTextReader 来解析XML的方法。最后,您可以尝试删除XML标记之间的所有空格,以查看是否可以修复这些内容。

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.

这篇关于XML的iText解析器异常 - &gt; PDF转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆