Excellent question. If I was able to upvote it twice, I would!
You can achieve what you want with a PdfPageEvent
:
public class RotateEvent extends PdfPageEventHelper {
public void onStartPage(PdfWriter writer, Document document) {
writer.addPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
}
}
You should use this RotateEvent
right after you've defined the writer:
PdfWriter writer = PdfWriter.getInstance(document, os);
writer.setPageEvent(new RotateEvent());
Note that I used SEASCAPE
to get the orientation shown in your image. You can also use LANDSCAPE
if you want the page to be oriented in the other direction.
I need to remember this question once I start writing a third edition of "iText in Action". It's a nice example of when to use the onStartPage()
event.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…