How can i rotate page at a specified angle(for example 25 degrees)?
PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
for (int i =1 ; i <= srcDoc.getNumberOfPages(); i++) {
PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
content.add(page...);
}
Can i set RotationAngle to work with PdfFormXObject ? Or is there another way?
The easiest way is by using an AffineTransform
(from com.itextpdf.kernel.geom
):
PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
PageSize pageSize = pdfDoc.getDefaultPageSize();
PdfFormXObject page = srcDoc.getPage(1).copyAsFormXObject(pdfDoc);
AffineTransform transform = AffineTransform.getRotateInstance(25 * Math.PI / 180, (pageSize.getLeft() + pageSize.getRight())/2, (pageSize.getBottom() + pageSize.getTop())/2);
content.concatMatrix(transform);
content.addXObject(page, 0, 0);
(RotatePageXObject test testAddPage25Degree
)
AffineTransform.getRotateInstance
is documented to
* Get an affine transformation representing a counter-clockwise rotation over the passed angle,
* using the passed point as the center of rotation
so we feed it the angle (transformed to radians) and the coordinates of the page center.
Applied to this source PDF it creates this result: