Error: Header doesn't contain versioninfo

Error: Header doesn't contain versioninfo

我正在尝试动态创建一些 pdf 页面并使用 PDFBox PDFMergerUtility 合并它们。基本上我有一组要合并的文档,现在我想在顶部添加一个包含一些动态文本和图像的封面。

PDFMergerUtility ut = new PDFMergerUtility();

        // Creating PDF document object
        PDDocument document = new PDDocument();

        PDPage blankPage = new PDPage();
        // Adding the blank page to the document
        document.addPage(blankPage);

        // Start a new content stream which will "hold" the to be created content
        PDPageContentStream contentStream = new PDPageContentStream(document, blankPage);

        // Define a text content stream using the selected font, moving the cursor and
        // drawing the text "Hello World"
        contentStream.beginText();
        // Setting the font to the Content stream
        contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

        // Setting the position for the line
        contentStream.newLineAtOffset(25, 500);

        String text = "This is the sample document and we are adding content to it.";

        // Adding text in the form of string
        contentStream.showText(text);
        contentStream.endText();

        // Make sure that the content stream is closed:
        contentStream.close();

        ut.addSource(document.getPage(0).getContents());

    
        // Save the merged pdf to outputstream
        ByteArrayOutputStream destStream = new ByteArrayOutputStream();
        ut.setDestinationStream(destStream);
        ut.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

但我收到以下错误:

java.io.IOException: Error: Header doesn't contain versioninfo
        at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:175)
        at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:154)
        at org.apache.pdfbox.Loader.loadPDF(Loader.java:456)
        at org.apache.pdfbox.Loader.loadPDF(Loader.java:377)
        at org.apache.pdfbox.multipdf.PDFMergerUtility.legacyMergeDocuments(PDFMergerUtility.java:457)
        at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:355)
        at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:339)
        at com.iof.services.AuditDocumentService.mergeAttachments(AuditDocumentService.java:210)
        at com.iof.services.AuditDocumentService.downloadAuditDocument(AuditDocumentService.java:118)
        at com.iof.services.AuditDocumentService.lambda[=11=](AuditDocumentService.java:95)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
        at com.iof.services.AuditDocumentService.downloadAuditDocument(AuditDocumentService.java:85)
        at com.iof.schedulers.AuditDocumentScheduler.downloadAuditDocument(AuditDocumentScheduler.java:17)
        at jdk.internal.reflect.GeneratedMethodAccessor73.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)

有人可以帮我解决我做错的事吗?

你会

ut.addSource(document.getPage(0).getContents())

这是错误的,PDFMergerUtility 期望 PDF 文档作为来源,但 PDPage.getContents() 只有 returns 某些页面的内容流。

而是将 document 保存到某些 ByteArrayOutputStream 并将该流的内容作为源添加到 ut