java printing - setting size of the border

本小妞迷上赌 提交于 2020-01-02 05:30:29

问题


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() {

            @Override
            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());
                      TournamentView.this.paint(g2);
                      return Printable.PAGE_EXISTS;
            }

          });
          if (pj.printDialog() == false)
          return;

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

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


回答1:


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());
format.setPaper(paper);

pj.setPrintable(new Printable()
{
    @Override
    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())
    return;

pj.print();

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.



来源:https://stackoverflow.com/questions/8335301/java-printing-setting-size-of-the-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!