Reputation: 1241
In my desktop application i need to print jPanel data on dot matrix printer where paper size should be 10x6. In java we have a restriction that width should not greater than height. But how can i accomplish my task by crossing this limit. If i set the page format with height is smaller than width it is treating it as A4 paper and feed the bottom of the paper.
If I didn't specify the page format it is printing fine but giving lot of margin(top,bottom,left,right). I am unable to change the margins. If set only the margins it is treating page as A4 and giving feed at bottom.
I need to print the data on pre printed page with alignment. Is there any other way to do this. Can I use Landscape, If so how to control the text flow (from bottom to top - X axis).
This is my code
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat = job.defaultPage();
pageFormat.setOrientation(PageFormat.LANDSCAPE);
job.setPrintable(this,pageFormat);
try {
job.print();
} catch (PrinterException ex) {
System.out.println(ex);
}
paint method
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
g.setFont(new java.awt.Font("Sans Serif", java.awt.Font.PLAIN, 10));
Graphics2D g2d = (Graphics2D)g;
AffineTransform old = g2d.getTransform();
if (pf.getOrientation() == PageFormat.LANDSCAPE) {
g2d.rotate( -Math.PI / 2, 0, 0);
g2d.translate( -pf.getImageableWidth(), 0);
}
else {
g2d.rotate(Math.PI / 2, 0, 0);
g2d.translate(0, -pf.getImageableHeight());
}
jPanel1.printAll(g2d);
g2d.setTransform(old);
//g2d.translate(70, 30);
return PAGE_EXISTS;
}
Thanks in advance.
Upvotes: 1
Views: 1509
Reputation: 57421
Set PageFormat's orientation to landscape and rotate the content. In the print() method rotate and translate the content.
Graphics2D g2d = (Graphics2D) g;
AffineTransform old = g2d.getTransform();
if (contentOrientation == ORIENTATION_DOWN_UP) {
g2d.rotate( -Math.PI / 2, 0, 0);
g2d.translate( -w, 0);
}
else {
g2d.rotate(Math.PI / 2, 0, 0);
g2d.translate(0, -h);
}
//paint all your content here
g2d.setTransform(old);
Upvotes: 1