最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Java Print landscape pages with no rotation - Stack Overflow

programmeradmin1浏览0评论

I'm trying to print a document from Java 23 in LANDSCAPE mode.

I print to PDF file when asked for the destination to print.

When I want it to print my document on landscape, I have a PDF rotated 90° to the left.

These are the files in my project:

  • src/main/java/com/example/littleVHPrinting/App.java
  • src/main/java/com/example/lvhp/print/AppPageable.java
  • src/main/java/com/example/lvhp/print/AppPrintable.java
  • src/main/resources/textContent.txt

This is the most relevant code:

public class AppPageable implements java.awt.print.Pageable {
//stuff...
    @Override
    public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
        PageFormat pf = new PageFormat();
        final Paper paper = new Paper();
        switch(orientation){
            //This has no effect, and pf.setOrientation(PageFormat.LANDSCAPE); prints rotated 90°
            case HORIZONTAL->{paper.setSize(paper.getHeight(), paper.getWidth());}
        }
        paper.setImageableArea(0d, 0d, paper.getWidth(), paper.getHeight());
        pf.setPaper(paper);
        return pf;
    }

//more stuff...
}

If I do pf.setOrientation(PageFormat.LANDSCAPE), I have a rotated PDF like this one. What I really want is a PDF with a LANDSCAPE printing, without rotation, just like this.

Maybe this is not an error from the API, they surely wanted it to work like this, but is not what I need. What can I do?

Thanks in advance.

Full running project on

发布评论

评论列表(0)

  1. 暂无评论