iText - 添加Image元素会生成损坏的PDF文件 [英] iText - adding Image element generates a corrupt PDF file

查看:1959
本文介绍了iText - 添加Image元素会生成损坏的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText®5.2.1©2000-2012 1T3XT BVBA和Integration Designer 8.0来创建以字节数组导出的PDF文件。

I'm using iText® 5.2.1 ©2000-2012 1T3XT BVBA and Integration Designer 8.0 to create a PDF file that is exported in an byte array.

我正在创建一个包含大量文本的文档,并希望在开头添加徽标。

I am creating a document with a fair amount of text and want to add a logo at the beginning.

添加图像的部分代码如下:

Part of the code that is adding the image is as follows:

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(Stringovi.SLIKA1);
Image image1 = Image.getInstance(decodedBytes);
image1.setAbsolutePosition(30f, 770f);
image1.scalePercent(60f);
document.add(image1);

由于系统要求,输入图像采用字节数组格式。
文档的其余部分由具有各种内容的不同表组成,并且全部是文本。
当我以前面提到的方式添加图像时程序结束,我得到一个字节输出,我通过Base64解码器运行。生成的PDF无法打开,显示的错误是:

The input image is in byte array format because of the system requirements. The rest of the document consists of different tables with various content and it's all text. When I add the image in the before mentioned way the program finishes and i get an byte output that i run trough a Base64 decoder. Resulting PDF can not be opend and the error shown is:

 "Error [PDF Structure 40]:Invalid reference table (xref)"

我无法看到我的错误在哪里如果有人可以如此善良并指出我的意思正确的方向我非常感谢。

I can't see where my mistake is so if anybody could be so kind and point me in the right direction I would very much appreciate it.

推荐答案

您作为损坏的PDF文件呈现的文件并不完整PDF文件。它不以 %% EOF 结尾,它没有交叉引用表,...这是一个不完整的PDF文档。

The document you presented as a "broken PDF file" is not a complete PDF file. It doesn't end with %%EOF, it doesn't have a cross-reference table,... It's a PDF document that isn't complete.

这意味着您的代码中没有以下行:

This means that you don't have the following line in your code:

document.close();

如果你有这条线,则不会到达。例如:抛出异常导致代码跳转到 catch 子句,跳过 close()操作。

If you do have this line, it isn't reached. For instance: an exception is thrown causing the code to jump to a catch clause, skipping the close() operation.

错误消息无效的参考表(外部参照)与该诊断一致。这不是iText引起的问题。这是由编码错误引起的问题:没有关闭文档和/或没有正确处理异常。

The error message saying Invalid reference table (xref) is consistent with that diagnosis. This isn't a problem caused by iText. It's a problem caused by bad coding: not closing the document and/or not dealing with exceptions correctly.

这篇关于iText - 添加Image元素会生成损坏的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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