RectangleUtils { public static PDRectangle rotate(int degrees, PDRectangle oldRectangle, PDRectangle mediaBox) { while(degrees < 0) { degrees = 360 + degrees; } switch (degrees) { case 0: return oldRectangle; case 90: return new PDRectangle(oldRectangle.getLowerLeftY(), mediaBox.getWidth() - oldRectangle.getLowerLeftX() - oldRectangle.getWidth(), oldRectangle.getHeight(), oldRectangle.getWidth() ); case 180: return new PDRectangle(mediaBox.getWidth() - oldRectangle.getLowerLeftX() - oldRectangle.getWidth(), mediaBox.getHeight() - oldRectangle.getLowerLeftY() - oldRectangle.getHeight(), oldRectangle.getWidth(), oldRectangle.getHeight() ); case 270: return new PDRectangle(mediaBox.getHeight() - oldRectangle.getLowerLeftY() - oldRectangle.getHeight(), oldRectangle.getLowerLeftX(), oldRectangle.getHeight(), oldRectangle.getWidth() ); default: throw new RuntimeException("Cannot rotate rectangle by degrees: " + degrees); } }  static boolean intersect(PDRectangle r1, PDRectangle r2); static PDRectangle translate(float xOffset, float yOffset, PDRectangle oldRectangle); static PDRectangle rotate(int degrees, PDRectangle oldRectangle, PDRectangle mediaBox);  }
@Test public void rotateRectangle() { PDRectangle in = new PDRectangle(0, 0, 396, 612); PDRectangle mediaBox = new PDRectangle(0, 0, 792, 612); assertThat(RectangleUtils.rotate(-90, in, mediaBox), is(new PDRectangle(0, 0, 612, 396))); assertThat(RectangleUtils.rotate(270, in, mediaBox), is(RectangleUtils.rotate(-90, in, mediaBox))); assertThat(RectangleUtils.rotate(90, in, mediaBox), is(new PDRectangle(0, 396, 612, 396))); assertThat(RectangleUtils.rotate(180, in, mediaBox), is(new PDRectangle(396, 0, 396, 612))); }