Description: I am writing a small program that needs to generate a report (1 pager). This is nothing big and it's the first time I need to generate some sort of report as a printable output. Should be simple enough, so I created a JPanel, added all the fields/panels and styles required to make it look like a report.
Problem: When I try to print the report it does not fit on an A4 and is cuts off at the right and bottom. I then tried to change the print method to scale the JPanel but now I am getting some strange compression (right word?) issues on the font and some other items.
What I've tried: I tried to print the report to a JPEG/PNG (This looks perfect when viewed) and then send to the printer but this causes horrible image quality issues. I then changed the JPanel contents to be smaller to fit but this made the report look to small when viewed on the screen before printing.
Help with: It would be nice if someone could point me in the right direction or tell me where my scaling is causing the issue if possible... I really do not look forward to creating different panels for each report just to view and print correctly.
public int print(Graphics gc, PageFormat pageFormat, int pageIndex) {
if(pageIndex>0) {
return NO_SUCH_PAGE;
}
// Create a instance of the RepaintManager from the component being printed
RepaintManager mgr = RepaintManager.currentManager(component);
Graphics2D g2d = (Graphics2D)gc;
// Get the bounds of the component
Dimension dim = component.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// Get the bounds of the printable area
double pHeight = pageFormat.getImageableHeight();
double pWidth = pageFormat.getImageableWidth();
double pXStart = pageFormat.getImageableX();
double pYStart = pageFormat.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
g2d.scale(xRatio, yRatio);
mgr.setDoubleBufferingEnabled(false); // Only for swing components
component.paint(g2d);
mgr.setDoubleBufferingEnabled(true); // Only for swing components
return PAGE_EXISTS;}