Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to produce a PDF that has pages in landscape. While it is possible to set the size of the page to landscape using:

document.setPageSize(PageSize.LETTER.rotate());

this does not achieve what I want because any content that I add is still oriented left->right while I would like it to be bottom->top.

I.E. this is what I am getting:

landscape with content left->right

When what I want is:

landscape with content bottom->top

I have been able to achieve the desired output by opening the PDF after it has been created and rotating it using iText, but I would like a solution that lets me rotate it immediately with iText after adding content to it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
537 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...