I tried printing for the fiest time and found these code snippets:
package main;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import objects.Dokument;
public class Drucker {
public Drucker() {
try {
PrinterJob pj = PrinterJob.getPrinterJob();
Book pb = new Book();
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
pb.append(new Dokument(), pf);
pb.append(new Dokument(), pf);
System.out.println(pb.getNumberOfPages());
pj.setPageable(pb);
if(pj.printDialog()) {
pj.print();
}
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
and the Dokument class:
package objects;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
public class Dokument implements Printable {
@Override
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
g.setColor(Color.red);
g.fillRect(0, 0, 50, 50);
g.setColor(Color.black);
g.drawString("Hello World", 50, 50);
return 0;
}
}
While it manages to print multiple pages, they are blank.
My guess was that this has something to do with the Graphics but i couldnt find any Graphics tags i could implement to make it work.
New contributor
user26511071 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Do not return 0, return:
Printable.NO_SUCH_PAGE
ifpi
is not a correct page number,Printable.PAGE_EXISTS
ifpi
is a correct page number and you rendered something into it.