java打印 - 设置边框的大小 [英] java printing - setting size of the border

查看:390
本文介绍了java打印 - 设置边框的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将大小设置为零,或者在java中删除打印文档的边框。它总是有一个标准的白色边框。



这里是我打印JPanel和一些组件的函数:

  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;

尝试{
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){
//处理异常
}
}

我使用的是Adobe PDF打印机,因为我在这里没有任何打印机。有什么建议么? 使用 api / java / awt / print / PrinterJob.html#setPrintable%28java.awt.print.Printable,%20java.awt.print.PageFormat%29rel =nofollow> PrinterJob.setPrintable()一个 PageFormat 参数。

在PageFormat中,设置论文的可成像区域没有边框(x = 0,y = 0,width =纸张的宽度,高度=纸张的高度)。

您可能希望通过 PrinterJob.validatePage(),其中:


返回页面的克隆,并将其设置调整为与此PrinterJob的当前打印机兼容。例如,返回的PageFormat可以将其可成像区域调整为适合当前打印机使用的纸张的物理区域。


这是个好主意,因为打印机可能不支持无边距打印,并且这种方法会调整您的 PageFormat ,以便设置与打印机兼容。



这是一个在页面上打印一些带有删除边框的文本的示例:

  PrinterJob pj = PrinterJob.getPrinterJob(); 
PageFormat format = pj.getPageFormat(null);
Paper paper = format.getPaper();
//从纸上删除边框
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(早上好,吃什么?,0 ,textHeight);
return Printable.PAGE_EXISTS;
}
},format);

if(!pj.printDialog())
return;

pj.print();

使用Windows上的Postscript - >文件打印机进行测试。还有一个小的边框,但这可能是打印机驱动程序的限制。


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?

解决方案

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.

这篇关于java打印 - 设置边框的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆