问题描述
主题:将文件添加到mainPDF后,尝试打开COsstream。换句话说,在将PDF文件添加到主PDF文件后,尝试将文本写入主PDF文档。我不想保存或关闭mainPDF,因为我计划一次又一次地添加更多的PDFFiles和更多的文本。就我而言,我尝试使用TreeMerge后打开PDPageContentStream contentStream。显然,主文件过早关闭了吗? 向下滚动以在下面查看我的代码
自2014年左右以来,这个主题肯定存在很多麻烦: 我真的希望这些问题的答案能发布完整的代码。因此,我列出了类似问题的帖子列表,并尝试解决此问题(下面的滚动)。我相信会有更多的初学者会遇到这个问题。
-
PDPage实例化的地方。 COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
-
来自帖子:PDFBox IO Exception: COSStream has been closed and cannot be read。评论https://stackoverflow.com/a/55591429/14092356-对我不起作用:我在此处发布代码
'''
public void tableOfContents() throws IOException { String path1 = new File("Index1.pdf").getAbsoluteFile().toString(); File file1 = new File(path1); String path2 = new File("Index2.pdf").getAbsoluteFile().toString(); File file2 = new File(path2); PDFMergerUtility merger = new PDFMergerUtility(); PDDocument combine = PDDocument.load(file1); PDDocument combine2 = PDDocument.load(file2); merger.appendDocument(mainDocument,combine); //combine.close(); merger.appendDocument(mainDocument,combine2); merger.mergeDocuments(); merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly()); }
更多资源
- PDFBox COSStream closed before use
- java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
- "IOException: COSStream has been closed and cannot be read" when trying to save PDF after adding page with PdfBox
- 合并大量pdf文件非常困难,因为您需要跟踪https://issues.apache.org/jira/browse/PDFBOX-3901的打开和关闭情况
- Reading a particular page from a PDF document using PDFBox
这是我遇到问题的代码
import org.apache.pdfBox.pdmodel.PDDocument;
import org.apache.pdfBox.pdmodel.PDPage;
import org.apache.pdfBox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import java.io.IOException;
public class main {
public static void main(String[] args) throws IOException {
PDDocument document = new PDDocument();
PDOutlineItem pagesOutline = new PDOutlineItem();
for (int numberOfPages = 0; numberOfPages < 5; numberOfPages++) {
//Creating a blank page
PDPage blankPage = new PDPage();
//Adding the blank page to the document
document.addPage(blankPage);
}
ExampleImportingClass example = new ExampleImportingClass(document);
document.save("exampleError.pdf");
System.out.println("PDF created");
document.close();
}
}
import org.apache.pdfBox.pdmodel.PDDocument;
import org.apache.pdfBox.pdmodel.PDPage;
import org.apache.pdfBox.pdmodel.PDPageContentStream;
import org.apache.pdfBox.pdmodel.PDPageTree;
import org.apache.pdfBox.pdmodel.font.PDType1Font;
import org.apache.pdfBox.pdmodel.graphics.image.PDImageXObject;
import java.io.File;
import java.io.IOException;
public class ExampleImportingClass {
private PDDocument document;
public ExampleImportingClass(PDDocument document) throws IOException {
this.document = document;
page1();
page2_3();
page4();
}
public void page1() throws IOException {
PDPage page1 = document.getPage(0);
String path = new File("catPicture3.jpg").getAbsoluteFile().toString();
PDImageXObject pdImage = PDImageXObject.createFromFile(path,document);
PDPageContentStream content1 = new PDPageContentStream(document,page1);
content1.beginText();
content1.endText();
content1.drawImage(pdImage,50,400,300,300);
content1.close();
}
public void page2_3() throws IOException {
String path1 = new File("Example1.pdf").getAbsoluteFile().toString();
File file1 = new File(path1);
String path2 = new File("Example2.pdf").getAbsoluteFile().toString();
File file2 = new File(path2);
PDPage page1 = document.getPage(0);
PDPage page2 = document.getPage(2);
PDPageTree mergePD = document.getPages();
PDDocument doc1 = PDDocument.load(file1);
PDDocument doc2 = PDDocument.load(file2);
mergePD.insertAfter(doc1.getPage(0),page1);
mergePD.insertAfter(doc2.getPage(0),page2);
}
public void page4() throws IOException {
PDPage page = document.getPage(3);
PDPageContentStream content1 = new PDPageContentStream(document,page);
content1.beginText();
content1.setFont(PDType1Font.TIMES_ROMAN,14);
content1.newLineatoffset(50,350);
content1.showtext("If we remove page4() method AND its not working,then its not ");
content1.endText();
content1.close();
}
这是臭名昭著的错误堆栈跟踪:
2020年8月27日晚上11:10:48 org.apache.pdfBox.cos.COSDocument最终定稿 警告:警告:您没有关闭PDF文档 2020年8月27日11:10:48 PM org.apache.pdfBox.cos.COSDocument最终定稿 警告:警告:您没有关闭PDF文档
Exception in thread "main" java.io.IOException: COsstream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
at org.apache.pdfBox.cos.COsstream.checkClosed(COsstream.java:154)
at org.apache.pdfBox.cos.COsstream.createrawInputStream(COsstream.java:204)
at org.apache.pdfBox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1219)
at org.apache.pdfBox.cos.COsstream.accept(COsstream.java:475)
at org.apache.pdfBox.cos.COSObject.accept(COSObject.java:158)
at org.apache.pdfBox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:526)
at org.apache.pdfBox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:464)
at org.apache.pdfBox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:448)
at org.apache.pdfBox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1113)
at org.apache.pdfBox.cos.COSDocument.accept(COSDocument.java:452)
at org.apache.pdfBox.pdfwriter.COSWriter.write(COSWriter.java:1386)
at org.apache.pdfBox.pdfwriter.COSWriter.write(COSWriter.java:1273)
at org.apache.pdfBox.pdmodel.PDDocument.save(PDDocument.java:1357)
at org.apache.pdfBox.pdmodel.PDDocument.save(PDDocument.java:1328)
at org.apache.pdfBox.pdmodel.PDDocument.save(PDDocument.java:1316)
at main.main(main.java:22)
解决方法
几件事可能导致此错误。最终对我有用的是在流的开头添加偏移值和空白文本。所以我认为这是合并错误,因为当我停止合并新的PDF文件时问题消失了。但是,这是我的封面的问题。封面的COSStream干扰了合并的pdf文件。
我的封面上只有图像,没有文字。由于我仍然不知道的原因,这是导致错误的原因。一旦我为文本和空白文本设置了偏移量,程序就会进行编译。 请注意,PDImage之前已经具有偏移值。显然,这对于编译器来说还不够。
content1.newLineAtOffset(50,350);
content1.setFont(PDType1Font.TIMES_ROMAN,14);
content1.showText(“如果添加此代码,它将立即生效。奇怪”);
public void page1() throws IOException {
PDPage page1 = document.getPage(0);
String path = new File("catPicture3.jpg").getAbsoluteFile().toString();
PDImageXObject pdImage = PDImageXObject.createFromFile(path,document);
PDPageContentStream content1 = new PDPageContentStream(document,page1);
content1.beginText();
content1.newLineAtOffset(50,350);
content1.setFont(PDType1Font.TIMES_ROMAN,14);
content1.showText("If we add this code it works now. Strange");
content1.endText();
content1.drawImage(pdImage,50,400,300,300);
content1.close();
}