5 Replies Latest reply: Mar 2, 2012 9:31 PM by 800449 RSS

    Help with print margins

      I'm using the Java Printing API, but have problems setting print margins.

      What I'm seeing is, if I try to place my image at the top left of the document, a white margin overlays/crops my image.

      Here's a screenshot of my regular output:

      Here's a screenshot of the adjusted margins (using the Paper() object):

      Note, I don't have a physical printer, so I'm using the "Microsoft XPS Document Writer" to test the output of my application.

      I've also tried using the PDF-RENDERER application as a plug-in and I experience the same white border when trying to print PDFs.

      Any insight is appreciated. Link to bug report: http://code.google.com/p/jzebra/issues/detail?id=42

        • 1. Re: Help with print margins
          COuld you provide code? What's imageable area?

          Can you try to set it to 0 in the
          public int print(Graphics g, PageFormat pageFormat, int pageIndex)
          method. Like this
          pageFormat.getPaper().setImageableArea(0,0,paper.getWidth(), paper.getHeight());
          • 2. Re: Help with print margins
            h2. Edit: I apologize as I'm not in front of my code at the moment. I will reply again after double-checking the below statement.
            StanislavL wrote:
            COuld you provide code? What's imageable area?

            Can you try to set it to 0 in the

            Thank you for the quick response. The snippet that you have provided is the same code I used which resulted in the second image (http://i.imgur.com/9lrLj.png)

            I'm using a JLabel on a JPanel that implements Printable for a very basic HTML print renderer. i.e.
            new JLabel("<html><h1>test</h1></html>");
            public int print(Graphics g, PageFormat pf, int pageIndex)
            Paper paper = pf.getPaper();
            // Paper paper = new Paper();
            paper.setImageableArea(0,0,paper.getWidth(), paper.getHeight());
            Edited by: 797446 on Mar 1, 2012 12:12 PM
            • 3. Re: Help with print margins
              Yeah, I definitely tried that. Is there any known issues with how Java handles margins?

              I'll try this with another PostScript processor (such as PrimoPDF) to rule out the Microsoft XPS Document writer as the cause.
                      boolean doubleBuffered = label.isDoubleBuffered();
                      //Paper paper = new Paper();
                      //paper.setSize(8.5 * 72, 11 * 72);
                      //paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
                      Graphics2D g2d = (Graphics2D) g;
                      g2d.translate(pf.getImageableX(), pf.getImageableY());
                      return (PAGE_EXISTS);

              Edited by: 797446 on Mar 1, 2012 5:46 PM
              • 4. Re: Help with print margins
                I tried different implementations of Printable/Pageable and had no problems with margins.

                You can use the example's source http://java-sl.com/JEditorPanePrinter.html where margins work fine and check the difference. Or you can use the printer to print any html.
                • 5. Re: Help with print margins
                  It took a while, but I was able to fix this printing issue.

                  The tutorials provided in the print samples (which are now horribly outdated) don't seem to cover this, but in Windows 7 x64, I had to provide additional print attributes to override the default print margins.

                  Most printers cannot print on the entire surface of the media, due to printer hardware limitations. This class can be used to query the acceptable values for a supposed print job, and to request an area within the constraints of the printable area to be used in a print job.
                          HashPrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
                          attr.add(new MediaPrintableArea(0f, 0f, w/72f, h/72f, MediaPrintableArea.INCH));              
                          PrinterJob job = PrinterJob.getPrinterJob();    
                  The key was to provide the attributes along with the print() command. I kept finding this code snippet when finding PDF printing APIs which would suffer from the same border problems.