基于this SO question我尝试阅读pdf文件中的每一页。这样做的背景是,我试图替换不包含任何textcontent但包含完全空白页面的图像的页面。背景是pdf可以包含可能包含图像的空白页面。这些页面确实需要存在,因为它们即将打印成双面打印。
但是对于PDFBox 2.0,这似乎有点复杂,因为我每次尝试保存新生成的PDDocument
时都会遇到堆栈跟踪。是否应该使用PDFBox 2.0
的新版本做任何不同的事情?我是否应该避免关闭PDDocument buffer
,因为通过将其留下来,示例程序确实可以毫无例外地运行,这可能是潜在的副作用吗?
这里可以看到一个简单的运行示例。您可以使用任何pdf文件,因为结果将是一个pdf文件,其页面数量应该是空的:
public static void main(String[] args) throws IOException {
// Load a simple pdf file
PDDocument d = PDDocument.load(new File("D:\\test.pdf"));
// This should be our new output pdf
PDDocument c = new PDDocument();
for(int i = 0;i<d.getNumberOfPages();++i) {
// From the SO question, create a new PDDocument and just add the single page
PDDocument buffer = new PDDocument();
PDPage page = d.getPage(i);
buffer.addPage(page);
// Here i´d check if it has content but gonna leave it out now
// Reassign the page variable to generate a "blank" pdf
page = new PDPage();
// In order to let some printers not ignore the blank page I have to
// write white text on the white background.
PDPageContentStream contentStream = new PDPageContentStream(buffer, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.beginText();
contentStream.setNonStrokingColor(Color.white); // !!!!!!
contentStream.setFont( font, 6 );
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Empty page");
contentStream.endText();
contentStream.close();
// Close the buffer document, if i comment it out the exception is gone
buffer.close();
// Add the blank page
c.addPage(page);
}
d.close();
// The exception occurs here and seems to be connected with the closing of the buffer document
c.save("D:\\newtest.pdf");
c.close();
}
Stacktrace:
Exception in thread "main" java.io.IOException: Scratch file already closed
at org.apache.pdfbox.io.ScratchFile.checkClosed(ScratchFile.java:390)
at org.apache.pdfbox.io.ScratchFileBuffer.checkClosed(ScratchFileBuffer.java:99)
at org.apache.pdfbox.io.ScratchFileBuffer.seek(ScratchFileBuffer.java:295)
at org.apache.pdfbox.io.RandomAccessInputStream.restorePosition(RandomAccessInputStream.java:47)
at org.apache.pdfbox.io.RandomAccessInputStream.read(RandomAccessInputStream.java:78)
at java.io.InputStream.read(InputStream.java:101)
at org.apache.pdfbox.io.IOUtils.copy(IOUtils.java:66)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1134)
at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:372)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:533)
at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:450)
at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1034)
at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:409)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1284)
at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1185)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1110)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1082)
at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1070)
at pdftools.Test.main(Test.java:41)
答案 0 :(得分:1)
您的代码有些令人困惑,但问题的核心是在2.0中,如果您在其他文档中使用他们的页面,则不应关闭文档。
所以这里有一些解决方案:
答案 1 :(得分:0)
我一直在寻找 2021 年的解决方案,这就是现在可以完成的方法 (Java/Kotlin)。示例从 PDF 文件中提取页面并将其保存在额外文件中。
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import java.io.File
val inputPdf = File("C:\\myInput.pdf")
val outputPdf = File("C:\\myOutput.pdf")
fun main(args: Array<String>) =
PDDocument.load(inputPdf).use {
savePageToExtraFile(it.pages.first(), outputPdf)
}
fun savePageToExtraFile(page: PDPage, outputFile: File) =
PDDocument().use {
it.importPage(page)
it.save(outputFile)
}