0
votes

Using 1.8.9

I want to cut a PDF page to a multi-page PDF using crop tools. But when I add more than one page to my PDDocument it doesn't add it at all.

Code example (the original PDPage is a parameter of my function) :

private static void splitPage(int nbOfCrops, PDPage myPage) throws IOException{

PDDocument pdfSplit = new PDDocument();
ArrayList<PDPage> pages = new ArrayList<PDPage>();


    float croppingHeight = (myPage.findCropBox().getUpperRightY()/nbOfCrops);

    for(int page = 0; page<nbOfCrops; page++){
        pages.add(myPage); //Creates multiple copies of myPage
    }

    int splits = 0;
    for(PDPage page: pages){
        PDRectangle cropBox = page.findCropBox();
        PDRectangle rectangle = new PDRectangle();

        rectangle.setUpperRightY((float) (cropBox.getUpperRightY() - (croppingHeight* (splits))));
        rectangle.setLowerLeftY((float) (cropBox.getUpperRightY() - (croppingHeight*(splits+ 1))));
        rectangle.setUpperRightX(cropBox.getUpperRightX());
        rectangle.setLowerLeftX(cropBox.getLowerLeftX());
        page.setCropBox(rectangle);

        pdfSplit.addPage(page);

        splits++;
    }
    try {
        pdfSplit.save("test.pdf");   
        System.out.println(pdfSplit.getNumberOfPages()); //Always returns 1
        pdfSplit.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

So, what should I do/modify to add correctly each cropped pages ?

My document (if you want to see what I want to do) :

http://www.cinemas-utopia.org/admin/grilles/toulouse/2015-06-02.pdf

1
It is difficult to test because your code is incomplete and you're not telling the version, but I don't see you doing a "new PDPage", you're always adding the same PDPage object. Was that intended? - Tilman Hausherr
Just edited a little. If there is an ArrayList of PDPage objects I don't need to instantiate them. (Maybe i'm wrong). I copy the page X times to crop it each time at a different position and return a list of cropped pages. - pleguen
Please try creating a software that does a "new PDPage", at worst you lose 10 minutes. The reason I mention it is because in PDF itself, different pages are different objects. I can't tell if it is the cause of your probem, but in the final PDF, they must be different, or they would all have the same cropbox. - Tilman Hausherr
@TilmanHausherr Thanks for your answer, I'm about to do great stuff just a matter of time. - pleguen

1 Answers

0
votes

I did this, with some improvements to upscale the PDF (particular case).

    private static void cutAPDPage(int nbOfCrops, PDPage myPage, int resize) throws IOException{
    PDDocument pdfSplit = new PDDocument();
    ArrayList<PDPage> pages = new ArrayList<PDPage>();

    PDRectangle cropBox = myPage.findCropBox();
    PDRectangle newCropBox = new PDRectangle();
    newCropBox.setLowerLeftX(cropBox.getLowerLeftX());
    newCropBox.setLowerLeftY(cropBox.getLowerLeftY() - resize);
    newCropBox.setUpperRightX(cropBox.getUpperRightX());
    newCropBox.setUpperRightY(cropBox.getUpperRightY() + resize);
    myPage.setCropBox(newCropBox);


    PDRectangle mediaBox = myPage.findMediaBox();
    PDRectangle newMediaBox = new PDRectangle();
    newMediaBox.setLowerLeftX(mediaBox.getLowerLeftX());
    newMediaBox.setLowerLeftY(mediaBox.getLowerLeftY() - resize);
    newMediaBox.setUpperRightX(mediaBox.getUpperRightX());
    newMediaBox.setUpperRightY(mediaBox.getUpperRightY() + resize);
    myPage.setMediaBox(newMediaBox);

    float croppingHeight = (myPage.findCropBox().getUpperRightY()/nbOfCrops);

    for(int page = 0; page<nbOfCrops; page++){
        pages.add(new PDPage());
    }
    int splits = 0;
    for(PDPage page: pages){
        page = (PDPage) pdf.importPage(myPage);
        PDRectangle cropBox1 = page.findCropBox();
        PDRectangle rectangle = new PDRectangle();

        rectangle.setUpperRightY((float) (cropBox1.getUpperRightY() - (croppingHeight * (splits))));
        rectangle.setLowerLeftY((float) (cropBox1.getUpperRightY() - (croppingHeight*(splits + 1))));
        rectangle.setUpperRightX(cropBox1.getUpperRightX());
        rectangle.setLowerLeftX(cropBox1.getLowerLeftX());
        page.setCropBox(rectangle);

        pdfSplit.addPage(page);

        splits++;
    }
    try {
        pdfSplit.save("split.pdf");   
        pdfSplit.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Works like a charm !