PDDocument无法使用addPage()添加PDPage列表

时间:2015-06-01 12:49:16

标签: java pdfbox

使用1.8.9

我想使用裁剪工具将PDF页面剪切为多页PDF。但是,当我向PDDocument添加多个页面时,它根本不会添加它。

代码示例(原始PDPage是我函数的参数):

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();
    }
}

那么,我应该做什么/修改以正确添加每个裁剪的页面?

我的文件(如果你想看看我想做什么):

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

1 个答案:

答案 0 :(得分:0)

我做了这个,并对PDF进行了一些改进(特殊情况)。

    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();
    }
}

像魅力一样!