tags:

views:

71

answers:

2

Hi, i want to split one wide PDF page into two PDF pages. My original page is wide as two A4 page size but height is normal(for A4). I trying to use IText but with no effects. Thanks for attention.

A: 

I would look to be creating a copy of the original PDF with the changed page rather than updating the existing one, it'll be easier to work in iText that way and you can always rename the file aftwarads.

Have a look at the HelloWorldCopy example here.

Only thing you will need to change is the call to split the wide page into two pages. So do the same as the HelloWorldCopy example for all pages except the one you want to split - for this page have a look at the alternative PDfCopy.addPage() method that allows you to specify a rectangle that defines the newly created page's size.

So that should allow you to split the wide page into two properly-sized new pages. Now you need to make sure the left-hand part of the wide page goes into the first new page and the right-hand part goes into the second new page. For this you should look at the PdfImportedPage.setMatrix method (PdfImportedPage is the object returned from copy.getImportedPage() in the example.

Steve Claridge
I can't get content of original page to fill the new page created by PDfCopy.addPage(). And PdfImportedPage.setMatrix I don't understand.
gohohgle
What you want to do isn't trivial. Have a look at the PDF specification to understand the matrix better. The HelloWorld example shows how to copy from one page to another.
Steve Claridge
A: 

I don't know the iText API, but you can follow these steps to get there:

Create two new copies of the existing page. This means that you have the same Resources, the same ContentStream, etc.

Get the MediaBox for the first page which is an array laid out as [llx lly urx ury].

if MediaBox[2] - MediaBox[0] == long edge of A4 page then
    HalfPageWidth = MediaBox[2] - MediaBox[0];
    PageCopy1.CropBox = [MediaBox[0] MediaBox[1] (MediaBox[0] + HalfPageWidth) MediaBox[3]]
    PageCopy2.CropBox = [(MediaBox[0] + HalfPageWidth) MediaBox[1] MediaBox[2] MediaBox[3]]
else
    HalfPageHeight = MediaBox[3] - MediaBox[1];
    PageCopy1.CropBox = [MediaBox[0] MediaBox[1] MediaBox[2] (MediaBox[1] + HalfPageHeight)]
    PageCopy2.CropBox = [MediaBox[0] (MediaBox[1] + HalfPageHeight)] MediaBox[2] MediaBox[3]]

Remove the original page and save these two pages. Basically, you're making two identical copies of the page and cropped each to half the page. You may also need to set the page rotation.

plinth
I don't understand your answer.
gohohgle