Hello,
I'm trying to use PDFBox to extract a page from a multi page document
and creating a new document from it. I thought it would be as simple
as getting the PDPage, get contents..then create a new PDPage, set
content, and add it to a new PDDocument...but I guess it's not that
straight forward, or I'm missing something.

If I extract a PDPage, and put that same PDPAge it in a new document
and save it, it works.

i.e.

InputStream fileStream =
this.getClass().getResourceAsStream("sample.pdf");
PDDocument document = PDDocument.load(fileStream);      
PDPage pdfPage = (PDPage) document.getDocumentCatalog().getAllPages().get(0);

PDDocument document2 = new PDDocument();                                        
        


document2.addPage(pdfPage);
document2.save("c:\\generatedPDF.pdf");                 
document2.close();


But if I take the PDPage content and create a new PDPage, it doesn't work.

InputStream fileStream =
this.getClass().getResourceAsStream("sample.pdf");
PDDocument document = PDDocument.load(fileStream);      
PDPage pdfPage = (PDPage) document.getDocumentCatalog().getAllPages().get(0);

PDDocument document2 = new PDDocument();                                        
        
PDPage testPDPage = new PDPage();

testPDPage.setContents(pdfPage.getContents());

document2.addPage(testPDPage);
document2.save("c:\\generatedPDF.pdf");                 
document2.close();


At the end of the day, what I'm trying to do is extract all the pages
from the document, store it in a database, retrieve each page sometime
in the future and create a document with that one page. So, at some
point I figure I'll be converting the contents inty a bytearray and
store that, and recreate it.

Any help would be appreciated. Thanks for your time.

Foley

Reply via email to