java printing - setting size of the border

I try to set the size to zero or remove the border of a printed document in java. It always has a standard white border.

Here is my function printing a JPanel and some components:

    public void printComponent(){

          PrinterJob pj = PrinterJob.getPrinterJob();
          pj.setJobName(" Print Component ");

          pj.setPrintable (new Printable() {

            public int print(Graphics pg, PageFormat pf, int pageNum) throws PrinterException {
                if (pageNum > 0){
                      return Printable.NO_SUCH_PAGE;

                      Graphics2D g2 = (Graphics2D) pg;
                      g2.translate(pf.getImageableX(), pf.getImageableY());
                      return Printable.PAGE_EXISTS;

          if (pj.printDialog() == false)

          try {
              PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
              PrinterResolution pr = new PrinterResolution(200, 200, PrinterResolution.DPI);
              pj.print( aset);
          } catch (PrinterException ex) {
                // handle exception

I am using Adobe PDF printer since I haven't any printer here. Any suggestions?


Use the version of PrinterJob.setPrintable() that takes a PageFormat argument.

In the PageFormat, set the paper's imageable area have no border (x=0, y=0, width=paper's width, height=paper's height).

You might want to feed that through PrinterJob.validatePage(), which:

Returns the clone of page with its settings adjusted to be compatible with the current printer of this PrinterJob. For example, the returned PageFormat could have its imageable area adjusted to fit within the physical area of the paper that is used by the current printer.

This is a good idea because the printer might not support borderless printing and it will this method will adjust your PageFormat so that settings are compatible with the printer.

Here is an example that prints some text on a page with removed borders:

PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat format = pj.getPageFormat(null);
Paper paper = format.getPaper();
//Remove borders from the paper
paper.setImageableArea(0.0, 0.0, format.getPaper().getWidth(), format.getPaper().getHeight());

pj.setPrintable(new Printable()
    public int print(Graphics pg, PageFormat pf, int pageNum)
            throws PrinterException
        if (pageNum > 0)
            return Printable.NO_SUCH_PAGE;

        Graphics2D g2 = (Graphics2D)pg;
        g2.translate(pf.getImageableX(), pf.getImageableY());
        int textHeight = g2.getFontMetrics().getHeight();
        g2.drawString("Good morning, what will be for eating?", 0, textHeight);
        return Printable.PAGE_EXISTS;
}, format);

if (!pj.printDialog())


Tested with a Postscript -> File printer on Windows. There was still a small border left but that is likely a limitation of the printer driver.

Need Your Help

Delphi thiscall calling convention

c++ delphi

I need to call non-static C++ member functions

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.