使用最新版本将iText XML转换为PDF [英] iText XML to PDF using latest version

查看:253
本文介绍了使用最新版本将iText XML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些示例,展示了如何使用iText XML文档将XML用于PDF。但它们都是旧版本4.x.是否有任何示例或者有人发布了必需/更新代码的示例以在5.x版本中执行相同操作?

I found a few examples showing how to use the XML to PDF using a iText XML document. But they are all for the older version 4.x. Is there any examples or can someone post an example of the required/updated code to do the same in version 5.x?

所有示例都参考这样的代码,但我找不到用新版本替换ITextHandler类的内容。

http://www.ridgway.co.za/archive/2005/07/31/itextsharpxmltopdfexample.aspx

All the examples refere to code like this, but I can not find what to use to replace the ITextHandler class with in the new version.
http://www.ridgway.co.za/archive/2005/07/31/itextsharpxmltopdfexample.aspx

Document document = new Document();
PdfWriter.GetInstance(document, new FileStream("ExampleDoc.pdf", FileMode.Create));
ITextHandler xmlHandler = new ITextHandler(document);
xmlHandler.Parse("ExampleDoc.xml");

另外,我并不是想从HTML转到PDF。 CSS样式永远不会像预期的那样出现。

Also, I am not trying to go from HTML to PDF. The CSS styling never comes out as expected.

编辑以提升它,真的需要一些帮助。任何人都可以?

Editing to bump it up, really need some help here. Anyone at all?

推荐答案

iText使用专有语法处理XML文件很久以前就被删除了。请参见来自作者的直接答案。相反,我们鼓励您使用全球公认的 XHTML的XML标准。

iText's processing of XML files using a proprietary syntax was removed a very long time ago. See this and this for direct answers from the author. Instead you are encouraged to use the globally recognized XML standard which is XHTML.

我知道你说你不想使用HTML因为它永远不会正确,但也许你可以发布一些你正在尝试的样本,我们可以提供帮助。另外,请确保您使用的是XMLWorker而不是HTMLWorker。使用时请参阅这些链接以获取更多帮助/信息。

I know you said that you don't want to use HTML because it never comes out correctly but maybe you could post some samples of what you're trying and we could help. Also, please make sure that you are using the XMLWorker and not the HTMLWorker. See these links for additional help/info when using it.

  • List of supported CSS properties
  • Controlling fonts in HTML processing
  • Adding base64 encoded images
  • Changing the default image root path for relative images

编辑

此编辑是对@ JohnC评论的回答

我不能代表iText团队和他们的原因,但我可以猜测事情。 PDF没有段落,单词,表格等。相反,PDF有文字,图纸(线条,图案)和图像。如果您想手动执行这些操作,可以使用原始 PdfContentByte 对象。但是,我们鼓励您使用iText的抽象,例如段落 PdfPTable ,它们使用 PdfContentByte代表您

I can't speak for the iText team and their reasons but I can guess at things. PDF doesn't have "paragraphs", "words", "tables", etc. Instead, PDF has text, drawings (lines, patterns) and images. If you want to do these things manually you can use the raw PdfContentByte objects. You are encouraged, however, to use iText's abstractions like Paragraph and PdfPTable which use the PdfContentByte on your behalf.

要使iText支持XML格式,首先需要创建自己的专有DTD和/或XML Schema。如果添加了任何功能,则需要正确地对模式进行版本化,这可能会给消费者带来问题和困惑。然后,它需要构建/维护一个解析器,将XML抽象转换为iText抽象或原始PDF命令。对于前者,你有一个抽象的谈论抽象,只是乞求破解。对于后者,您现在有两个抽象实现,最终将遇到功能奇偶校验问题。

For iText to support an XML format it would need first create its own propriety DTD and/or XML Schema. If any features get added it would need to then version the schema properly which can cause problems and confusions for consumers. Then it would need to build/maintain a parser that turned the XML abstractions into either iText abstractions or raw PDF commands. For the former, you have an abstraction talking to an abstraction which is just begging to break. For the latter, you now have two abstraction implementations that will run into feature parity issues eventually.

此外,XML代表什么?段落,文本块,图像和表格?听起来像HTML已经没有必要重复那种架构了。或者它是将内容Z放在坐标X,Y使用字体ABC?这就是 PdfContentByte 的用武之地。真的,可能有一个原生解析器,但我猜测没有太多人要求一个。或者XML是基于您自己的数据的格式,例如< book> < inventory> ?如果是这样,那么iText真的不知道如何设计风格。但是,您可以使用杠杆.Net / Java和XSLT将XML转换为它知道的XHTML命令。

Further, what would the XML represent? Paragraphs, chunks of text, images and tables? Sounds like HTML already so there's no need to repeat that kind of schema. Or would it be "put content Z at coordinate X,Y using Font ABC"? That's where the PdfContentByte comes in. True, there could be a native parser but I'm guessing there just isn't too many people asking for one. Or would the XML be your own format based on your own data with things like <book> and <inventory>? If that's the case, then iText would really have no idea of how to style that either. You could, however, use leverage .Net/Java and XSLT to transform your XML into XHTML commands that it does know.

这篇关于使用最新版本将iText XML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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