73365

PDDocument can't add list of PDPage with addPage()

Question:

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) :

<a href="http://www.cinemas-utopia.org/admin/grilles/toulouse/2015-06-02.pdf" rel="nofollow">http://www.cinemas-utopia.org/admin/grilles/toulouse/2015-06-02.pdf</a>

Answer1:

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 !

Recommend

  • CSS custom shape button with two colors
  • Runtime error in UVA Online Judge [closed]
  • I don't get what's the difference between format() and … (python)
  • 3.0.0.M1: SSL - Invalid keystore format
  • How to define an array of floats in Shader properties?
  • Can I update/select from a table in one query?
  • Scala: Function returning an unknown type
  • WordPress > setting permalink option via script buggy?
  • insert radio value multiple data codeigniter in database to one row?
  • Can't get plist URL in Swift
  • What Makes These Two Array Adds Different?
  • JSON encode and decode on PHP
  • How to copy styled text in JTextPane
  • converter json to two dimensional array
  • Yii2: Finding file and getting path in a directory tree
  • KnockoutObservableArray with typed elements in TypeScript
  • Python ImageIO Gif Set Delay Between Frames
  • Clear activity stack before launching another activity
  • Add dynamic data to line chart from mysql database with highcharts
  • Symfony 2. CSRF token is invalid
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • JqueryMobile Popup menu is not working
  • How to assign byte[] as a pointer in C#
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Appending Character to Character Array In C
  • PHP CURL timing out but CLI CURL works
  • Declaring variable dynamically in VB.net
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Jackson Parser: ignore deserializing for type mismatch
  • How to test if a URL from an Eclipse bundle is a directory?
  • Initializer list vs. initialization method
  • Repeat a vertical line on every page in Report Builder / SSRS
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Date difference with leap year
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check