我尝试创建一个包含空页的 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/