'Rotating each page in the PDF using java

Hi folks i have a few use cases i need to cover while rotating the page in the PDF .

  1. I need to check each page rotation value and rotate it to 0 degree.

  2. when i check few docs in Adobe it shows me 90 degree but it will be in 0 degree.

I need to cover both the use cases, I have written a code using java PDFBox which will get the degree of rotation is showing wrong, If any one have idea How to find And what are the Aspects that decide the degree please help me through it . with code or Wiki to refer , I am working on a spring boot project.

 PDDocument document;


 public void getPdfFile(String pdfPath) throws IOException {

    File file = new File(pdfPath);

    document = PDDocument.load(file);

   int pageCount = document.getNumberOfPages();

   for(int i = 0; i < pageCount; i++) {
       PDPage page =  document.getPage(i);

      System.out.println( page.getRotation());
      if(page.getRotation() != 0) {
          page.setRotation(0);
      }

   }
     document.save("/Users/tejasreddy/Desktop/CE/StorePDF/rotated1_rotated.pdf");

     document.close();
}

enter image description here

CropBox And Media Box Details

Thanks Tejas.

3: PDF Details



Solution 1:[1]

There appears to be a misunderstanding in respect to the meaning of the PDF page rotation property, so I'll explain that here.

The visible page area has the dimensions and coordinate ranges given by the CropBox entry of the page. The x coordinates in this area increase going right, the y coordinate increase going up. In this area the instructions of the page content streams can draw text and other contents in any direction or orientation.

The Rotate entry of the page (i.e. the "page rotation") instructs a viewer program to display the page area defined by the crop box rotated clockwise by the value of that entry in degree (must be a multiple of 90°).

That's it.

Thus, the page rotation value does not necessarily coincide with the orientation of any content on the page. (Of, course, when creating a PDF one usually chooses the crop box and the rotation value to make both adding content and reading it in a viewer as easy as possible. But this is not technically enforced.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkl