2013-01-16 71 views
4

我刚拿到打印机在java中工作我也需要它,但还有最后一个问题需要解决。打印时,字体的宽度相当拉伸,并且不像应该的那样清晰和清晰。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

任何建议或帮助是极大的赞赏。

回答

2

默认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。你也可以在打印对话框中添加它看看它在做什么,但这是我现在可以不需要的复杂程度;)

+0

你可能指示我到一个网站或一个来源,我可以学习如何使用打印对话框来检查这些东西吗?感谢您的帮助:) – Brayden

+0

在帖子末尾的链接有他打印对话框的条目 – MadProgrammer

+0

谢谢!我忽略了它,我很抱歉! – Brayden

0

上面的工作!要打开打印对话框有了它,使用此:

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,因此出现在纸上!它们也可以在对话框中更改。