0

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;}

Report Good Example (PNG)

Report Bad Example (Print/PDF)

Griffin
  • 79
  • 1
  • 11
  • Try using an AffineTransform as in the answer to this question: http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed – samgak Jan 18 '17 at 08:07
  • @samgak Will have a look, think I found that post while searching but must have overlooked it due to JComponent being in the header. Late night/little sleep – Griffin Jan 18 '17 at 08:18
  • @samgak I have tried it, same result (this is the 4th printing method I have tried). I played with my preferred size and even removed the scaling and it still prints the same. I am starting to suspect the font to be the issue. – Griffin Jan 18 '17 at 08:42
  • You might just have to set the font size on each JTextField. Although that sounds like the opposite of the approach you want to take – samgak Jan 18 '17 at 08:48
  • @samgak currently I have a method that sets the JTextField font to Trebuchet MS. I have tried with different fonts, like Arial and this increases the spaces between words but then it looks like double spaces where added everywhere. – Griffin Jan 18 '17 at 08:53
  • I think I have a answer to the font scaling. It's not a solution more a workaround but it seems that the printing does not like fonts smaller than 10 in this configuration. Change to 11 and up, no problem. – Griffin Jan 18 '17 at 09:09
  • First thing: pageFormat.getImageableHeight() returns the height in inches not pixels; Second Thing: any scaling will result into distortion you cant eat your cake and have it all; Third thing: the scaling should preserve the aspect ratio. – gpasch Jan 19 '17 at 03:41

0 Answers0