Java PDFBox 2.0读取单个页面并写入/保存到新文件

Java PDFBox 2.0读取单个页面并写入/保存到新文件,java,pdfbox,Java,Pdfbox,基于这一点,我试着阅读pdf文件中的每一页。背景是,我试图用完全空白的页面替换不包含任何文本内容但包含图像的页面。其背景是pdf可以包含可能包含图像的空白页面。这些页面确实需要在那里,因为它们即将用双面打印 但在PDFBox 2.0中,这似乎有点复杂,因为每次我试图保存新生成的PDDocument时都会遇到堆栈跟踪。新版本的PDFBox 2.0是否有任何不同?我是否应该避免关闭PDDocument缓冲区,因为如果不关闭它,示例程序会毫无例外地运行,这可能会产生什么潜在的副作用 这里可以看到一个简

基于这一点,我试着阅读pdf文件中的每一页。背景是,我试图用完全空白的页面替换不包含任何文本内容但包含图像的页面。其背景是pdf可以包含可能包含图像的空白页面。这些页面确实需要在那里,因为它们即将用双面打印

但在PDFBox 2.0中,这似乎有点复杂,因为每次我试图保存新生成的
PDDocument
时都会遇到堆栈跟踪。新版本的
PDFBox 2.0
是否有任何不同?我是否应该避免关闭
PDDocument缓冲区
,因为如果不关闭它,示例程序会毫无例外地运行,这可能会产生什么潜在的副作用

这里可以看到一个简单的运行示例。您可以使用任何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();
}

您的代码有些混乱,但问题的核心是,在2.0中,如果您在另一个文档中使用文档的页面,则不应关闭文档

以下是一些解决方案:

  • 不要关闭缓冲区文档,而是保留这些文档直到完成
  • 创建页面及其内容两次
  • 仅为目的地创建新页面(为什么要为“缓冲区”创建它,而您正在转储缓冲区?)
  • 使用importPage()代替使用addPage()复制页面。这将是一个深度复制

我在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)
    }
import org.apache.pdfbox.pdmodel.PDDocument
导入org.apache.pdfbox.pdmodel.PDPage
导入java.io.xml文件
val inputPdf=文件(“C:\\myInput.pdf”)
val outputPdf=文件(“C:\\myOutput.pdf”)
趣味主线(args:Array)=
加载(inputPdf)。使用{
savePageToExtraFile(it.pages.first(),outputPdf)
}
有趣的savePageToExtraFile(页面:PDPage,输出文件:File)=
PDDocument()。请使用{
it.importPage(第页)
保存(输出文件)
}
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)
    }