java - 为什么我收到 IOException : PDF header signature not found when creating a PDF?

标签 java pdf itext

我尝试创建一个包含空页的 PDF 文件,但代码抛出 java.io.IOException: P 当我尝试读取临时文件时,未找到 PDF header 签名。为什么?

相关代码:

Document testDoc = new Document();
File testFile = File.createTempFile("pdfTemp", ".tmp");
String test = testFile.getName();
PdfWriter testWriter = PdfWriter.getInstance(document, new FileOutputStream(test));
testDoc.open();
for (int x=1; x<=pdfReader.getNumberOfPages(); x++){
    testWriter.setPageEmpty(false);
    testDoc.newPage();
}
testDoc.close();
PdfReader testReader = new PdfReader(test);

最佳答案

这可能是一个编码问题。您的代码使用

PdfWriter testWriter = PdfWriter.getInstance(document, new FileOutputStream(test));

但是 document 变量没有在任何地方声明。我怀疑您打算使用以下内容:

PdfWriter testWriter = PdfWriter.getInstance(testDoc, new FileOutputStream(test));

关于java - 为什么我收到 IOException : PDF header signature not found when creating a PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841747/

相关文章:

Java 7 看似奇怪的行为,在调试消息中放置空字符串

r - 使用 knit kable 功能对齐列

java - 我的代码执行如何依赖于登台服务器

c# - 在 iTextSharp 4.1.6.0 中使用半透明填充绘制形状

java - 如何在没有传输编码 : chunked? 的情况下发送 HTTP 响应

java.lang.RuntimeException : Unable to instantiate service 错误

java - 通过 RMI 保持相同的线程名称

Java PDFBox 为 PDF 表单中的几个字段设置自定义字体

java - Android iText 阅读 PDF : Difference between iText's Parser and Extraction

c# - 在没有 API(例如 iTextSharp)的情况下在 .NET 中填充 PDF 字段