【问题标题】:Java Printing Font StretchJava 打印字体拉伸
【发布时间】:2012-12-30 09:47:27
【问题描述】:

我刚刚让打印机在 java 中工作,我也需要它,但是我需要解决最后一个问题。打印时,字体的宽度被拉长了,不像应有的那样清晰。

这是我的代码,我在纸上的实际绘图:

    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

非常感谢任何建议或帮助。

【问题讨论】:

    标签: java printing fonts graphics2d stretch


    【解决方案1】:

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

    基本上你需要使用打印服务 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;
        }
      }
    }
    

    您可能会找到Working with Print Services and Attributes 的一些帮助...

    我应该警告你,这将打印到它可以找到符合PrintRequestAttributeSet 的第一个打印件。您还可以在打印对话框中添加以查看它在做什么,但这是我现在可以不用的另一个复杂程度;)

    【讨论】:

    • 您能指导我到一个网站或来源,在那里我可以学习如何使用打印对话框检查这些内容吗?感谢您的帮助:)
    • 文章末尾的链接有他打印对话框的条目
    • 谢谢!我忽略了,对不起!
    【解决方案2】:

    以上方法有效!要使用它打开打印对话框,请使用:

        PrinterJob job = PrinterJob.getPrinterJob();
        TextDocumentPrinter document = new TextDocumentPrinter();
    
        PrinterResolution pr = new PrinterResolution(300, 300, PrinterResolution.DPI);
    
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(pr);
        aset.add(OrientationRequested.PORTRAIT);
    
        job.setPrintable(document);
    
        boolean doPrint = false;
    
        if (showDialog){
            doPrint = job.printDialog(aset);
        }else doPrint = true;
    
        if (doPrint){
            try{
                job.print();
            }catch(PrinterException e){
                e.printStackTrace();
            }
        }
    

    aset 变量包含您所有的新默认值,通过将其插入 printDialog,这些变量将输入到 printJob 中并因此显示在纸上!它们也可以在对话框中更改。

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 2018-12-23
      • 2013-12-14
      相关资源
      最近更新 更多