Reputation: 2472
I am using the iText API for Java and am running into an issue trying to combine multiple TIFFs into a PDF. Some are rotated, some are not. I can't quite figure out how rotate and preserve the width/height of the page.
This SO is helpful but only for rotating the whole document
iText Document : Rotate the page
Here is some code that I am using to test artificially rotating the image. This works but cuts off the image. For example if the original image is 1000(width)x2000(height), it will rotate it but half the image is lost, since the page size remains 1000x2000. Hopefully this makes sense.
Image img = Image.getInstance(part); //part is a string pointer to a file.
Rectangle imgPageSize;
if (i == 0) {// testing - rotate first page
img.setRotationDegrees((float) 90.0); //testing
imgPageSize = new Rectangle(img.getHeight(), img.getWidth());
}
TiffToPDF.setPageSize(imgPageSize); // this does not work
if (!TiffToPDF.isOpen())
TiffToPDF.open();
TiffToPDF.add(img);
Upvotes: 2
Views: 5227
Reputation: 77528
Please take a look at the rotate_pages.pdf document. In this example, we start with a page in portrait, then we have a page in landscape, followed by a page in inverted portrait, a page in seascape and finally again a page in portrait.
The page orientation was changed using a page event:
public class Rotate extends PdfPageEventHelper {
protected PdfNumber rotation = PdfPage.PORTRAIT;
public void setRotation(PdfNumber rotation) {
this.rotation = rotation;
}
public void onEndPage(PdfWriter writer, Document document) {
writer.addPageDictEntry(PdfName.ROTATE, rotation);
}
}
As you can see, we add a /Rotate
entry to the page dictionary before we end the page. Possible values for the rotation are:
PdfPage.PORTRAIT
PdfPage.LANDSCAPE
PdfPage.INVERTEDPORTRAIT
PdfPage.SEASCAPE
We use the page event like this:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
Rotate rotation = new Rotate();
writer.setPageEvent(rotation);
When we want to change the rotation, we simple use thesetRotation()
method in the event class. For instance:
rotation.setRotation(PdfPage.LANDSCAPE);
document.add(new Paragraph("Hello World!"));
document.newPage()
There's no need to rotate the image. If you want to return to portrait on the next page, just use setRotation(PdfPage.PORTRAIT);
after the document.newPage()
line as is done in the PageRotation example on the iText web site.
Upvotes: 5