Java打印字体拉伸 [英] Java Printing Font Stretch

查看:126
本文介绍了Java打印字体拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是让打印机也可以在Java中工作,但是还有最后一个问题需要解决.打印时,字体的宽度相当宽,并不像应有的那样清晰明了.

I just got the printer to work in java how I need it too, but there's one last problem I need to solve. When it prints, the font's width is rather stretched, and not crisp and clear like it should be.

这是我写在纸上的实际代码的代码:

Here is my code my the actual drawing to the paper:

    FontMetrics metrics = graphics.getFontMetrics(font);
    int lineHeight = metrics.getHeight();

    arrangePage(graphics, pageFormat, lineHeight);

    if (page > pageBreaks.length){
        return NO_SUCH_PAGE;
    }

    Graphics2D g = (Graphics2D) graphics;

    g.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    g.setFont(font);

    int y = 0;
    int begin = 0;

    if (page == 0){
        begin = 0;
    }else begin = pageBreaks[page-1];

    int end = 0;

    if (page == pageBreaks.length){
        end = lines.length;
    }else end = pageBreaks[page];

    for (int line = begin; line < end; line++){
        y += lineHeight;
        g.drawString(lines[line], 0, y);
    }

    string = deepCopy;

    return PAGE_EXISTS;

我如何摆脱伸展运动?可以注意到,这是基于本教程的: http://docs.oracle.com/javase/tutorial/2d/printing /set.html

How do I get rid of the stretching? It can be noted that this is based off this tutorial: http://docs.oracle.com/javase/tutorial/2d/printing/set.html

任何建议或帮助,我们将不胜感激.

Any advice or help is greatly appreciated.

推荐答案

默认DPI是正常的72 DPI(我相信),在打印纸上,这是非常糟糕的.您需要提示打印API尝试寻找具有更好DPI的打印机.

The default DPI is normal 72 DPI (I believe), which, on printed paper, is pretty terrible. You need to prompt the print API to try and find a printer with a better DPI.

基本上,您需要使用打印服务API.

Basically you need to use the print services API.

尝试类似...

public class PrintTest01 {

  public static void main(String[] args) {

    PrinterResolution pr = new PrinterResolution(300, 300, PrinterResolution.DPI);

    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    aset.add(pr);
    aset.add(OrientationRequested.PORTRAIT);

    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setPrintable(new Page());
    try {
      pj.print(aset);
    } catch (PrinterException ex) {
      ex.printStackTrace();
    }

  }

  public static class Page implements Printable {

    @Override
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
      if (pageIndex > 0) {
        return NO_SUCH_PAGE;
      }

      Graphics2D g2d = (Graphics2D) g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

      g.setFont(new Font("Arial", Font.PLAIN, 128));
      FontMetrics fm = g.getFontMetrics();
      int x = (int)(pageFormat.getWidth() - fm.stringWidth("A")) / 2;
      int y = (int)((pageFormat.getHeight() - fm.getHeight()) / 2) + fm.getAscent();

      g2d.drawString("A", x, y);

      return PAGE_EXISTS;
    }
  }
}

您可能会发现使用打印服务和属性一些帮助...

You might find Working with Print Services and Attributes of some help...

我应该警告您,这将打印到可以找到符合PrintRequestAttributeSet的第一张照片.您还可以在打印"对话框中添加以查看其功能,但这是我现在无需再经历的另一种复杂程度;)

I should warn you, this is going to print to the first print that it can find that meets the PrintRequestAttributeSet. You could also add in the print dialog to see what's it doing, but that's another level of complexity I can live without right now ;)

这篇关于Java打印字体拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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