PDDocument 无法使用 addPage() 添加 PDPage 列表 [英] PDDocument can't add list of PDPage with addPage()
本文介绍了PDDocument 无法使用 addPage() 添加 PDPage 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 1.8.9
我想使用裁剪工具将 PDF 页面剪切为多页 PDF.但是当我向我的 PDDocument 添加多个页面时,它根本不会添加它.
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.
代码示例(原来的PDPage是我函数的一个参数):
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
推荐答案
我这样做了,并做了一些改进以升级 PDF(特定情况).
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();
}
}
就像一个魅力!
这篇关于PDDocument 无法使用 addPage() 添加 PDPage 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文