Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a page with PDFBox doesn't work

I'm trying to add a page to an existing PDF-Document that I'm performing multiple different actions on before and after the page is supposed to be added.

Currently I open the page at the beginning of the document and write stuff on the first and second page of it. On the second page I add some images aswell. The Stuff that's written on the PDFs is different per PDF and sometimes it's so much stuff that two pages (or sometimes even 3) aren't enough. Now I'm trying to add a third or even fourth page once a certain amount of written text/printed images is on the second page.

Somehow no matter what I do, the third page I want to add doesn't show up in the final document. Here's my code to add the page:

if(doc.getNumberOfPages() < p+1){
    PDDocument emptyDoc = PDDocument.load("./data/EmptyPage.pdf");
    PDPage emptyPage = (PDPage)emptyDoc.getDocumentCatalog().getAllPages().get(0);
    doc.addPage(emptyPage);;
    emptyDoc.close();
}

When I check doc.getNumberOfPages() before, it says 2. Afterwards it says 3. The final document still just has 2 pages. The code after the if-clause contains multiple contentStreams that are supposed to write text on the new page (and on the first and second page).

 contentStream = new PDPageContentStream(doc, (PDPage) allPages.get((int)p), true, true);

In the end, I save the document via

doc.save(tarFolder+nr.get(i)+".pdf");
doc.close();

I've created a whole new project with a class that's supposed to do the exact same thing - add a page to another PDF. This code works perfectly fine and the third page shows up - so it seems like I'm just missing something. My code works perfectly fine for page 1 + 2, we just had the new case that we need a third/fourth page sometimes lately, so I want to integrate that into my main project.

Here's the new project that works:

PDDocument doc = PDDocument.load("D:\\test.pdf");
PDDocument doc2 = PDDocument.load("D:\\EmptyPage.pdf");

List<PDPage> allPages = doc2.getDocumentCatalog().getAllPages();
PDPage page = (PDPage) allPages.get(pageNumber);

doc.addPage(page); 
doc.save("D:\\testoutput.pdf");

What's weird in my main project is that the third page I add gets counted by

"getNumberOfPages()"

but doesn't show up in the final product. The program throws an error if I don't add the page because it tries to write content on the third page.

Any idea what I'm doing wrong?

Thanks in advance!

Edit:

If I add the page at the beginning, when my document is loaded the first time, the page gets added and exists in my final document - like this:

doc = PDDocument.load(config.getFolder("template"));
PDDocument emptyDoc = PDDocument.load("./data/EmptyPage.pdf");
PDPage emptyPage = (PDPage)emptyDoc.getDocumentCatalog().getAllPages().get(0);
doc.addPage(emptyPage);

However, since some documents don't need that extra page, it gets unnecessarily complicated - and I feel like removing the page if it isn't needed isn't really pretty, since I'd like to avoid adding it in the first place. Maybe somebody has an idea now?

like image 932
dacre Avatar asked Jan 20 '26 12:01

dacre


1 Answers

I found an answer thanks to Tilman Hausherr.

If I move the

emptyDoc.close()

to the end of my code, right after:

doc.save(tarFolder+nr.get(i)+".pdf");
doc.close();

the page shows up in the final document without any issues.

like image 172
dacre Avatar answered Jan 23 '26 02:01

dacre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!