2
votes

I am trying to print out the contents of my JTable to a printer, using table.print() method:

boolean complete = table.print(JTable.PrintMode.FIT_WIDTH,header,null);

My question is, is it possible to set the size of the left and right margin on the printable area of the paper without having to manually set them on a PrintDialog?

Thank you very much for your help!

1

1 Answers

2
votes

assuming that with "not manually" set them you mean programatically set them without showing the PrintDialog: you can set them in the Paper returned by the PrinterJob and then start the job directly

PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
Paper paper = pf.getPaper();
double margin = 20.;
paper.setImageableArea(margin, 
        paper.getImageableY(), 
        paper.getWidth() - 2* margin, paper.getImageableHeight());
pf.setPaper(paper);
job.setPrintable(table.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null), 
        job.validatePage(pf));
job.print();