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