You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Foley Ma <fo...@gmail.com> on 2012/06/17 23:44:01 UTC
Extracting a page and recreating a document
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
Re: Extracting a page and recreating a document
Posted by Foley Ma <fo...@gmail.com>.
I decided to use the Splitter class. Thank you very much!
Foley
On Sun, Jun 17, 2012 at 5:56 PM, Gilad Denneboom
<gi...@gmail.com> wrote:
> You can use the
> PDFSplit<http://pdfbox.apache.org/apidocs/org/apache/pdfbox/PDFSplit.html>class,
> or for a more basic approach, the
> Splitter<http://pdfbox.apache.org/apidocs/org/apache/pdfbox/util/Splitter.html>util
> directly.
>
> On Sun, Jun 17, 2012 at 11:44 PM, Foley Ma <fo...@gmail.com> wrote:
>
>> 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
>>
Re: Extracting a page and recreating a document
Posted by Gilad Denneboom <gi...@gmail.com>.
You can use the
PDFSplit<http://pdfbox.apache.org/apidocs/org/apache/pdfbox/PDFSplit.html>class,
or for a more basic approach, the
Splitter<http://pdfbox.apache.org/apidocs/org/apache/pdfbox/util/Splitter.html>util
directly.
On Sun, Jun 17, 2012 at 11:44 PM, Foley Ma <fo...@gmail.com> wrote:
> 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
>