2017-09-16 83 views
2

嘿有即时制作发票软件一切都很好只是问题是当我的报告打印时这是根据标准打印机打印,但我想通过帐单收据打印它打印机如何在java中打印适合帐单打印机的报告

@Override 
    public int print(Graphics g, PageFormat format, int page_index) 
    throws PrinterException { 
if (page_index > 0) { 
    return Printable.NO_SUCH_PAGE; 
} 

// get the bounds of the component 
Dimension dim = comp.getSize(); 
double cHeight = dim.getHeight(); 
double cWidth = dim.getWidth(); 

// get the bounds of the printable area 
double pHeight = format.getImageableHeight(); 
double pWidth = format.getImageableWidth(); 

double pXStart = format.getImageableX(); 
double pYStart = format.getImageableY(); 

double xRatio = pWidth/cWidth; 
double yRatio = pHeight/cHeight; 


Graphics2D g2 = (Graphics2D) g; 
g2.translate(pXStart, pYStart); 
g2.scale(xRatio, yRatio); 
comp.paint(g2); 

return Printable.PAGE_EXISTS; 
} 




public static void printing(){ 









    PrinterJob pjob = PrinterJob.getPrinterJob(); 
    PageFormat preformat = pjob.defaultPage(); 
    preformat.setOrientation(PageFormat.PORTRAIT); 
    PageFormat postformat = pjob.defaultPage(); 
//If user does not hit cancel then print. 
if (preformat != postformat) { 
    //Set print component 
    pjob.setPrintable(new reporting(frame), postformat); 
    // if (pjob.printDialog()) { 
    try { 
    pjob.print(); 
    frame.dispose(); 
    } catch (PrinterException ex) { 
    Logger.getLogger(reporting.class.getName()).log(Level.SEVERE, null, ex); 
    } 
//} 
//} 
} 

} 

此代码打印我的收据,但这是印刷根据标准的打印机,当我想通过结算打印机 进行打印并使用加斯帕报告打印出来IM仅仅指刚林不直接打印我的JFrame 帮帮我? thankx提前

+0

有很多方法可以做到这一点。如果你知道收据打印纸的大小,你可以使用[this example](https://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237)来改变纸张尺寸 – MadProgrammer

+0

[这是另一个例子](https://stackoverflow.com/questions/28427566/save-jpanel-as-image-hd-quality/28492187#28492187)在同一个想法和[这个例子也是如此](https://stackoverflow.com/questions/27029166/java-printerjob-not-printing-to-fit-paper/27029220#27029220) - 我“认为”你可能有的问题是,知道“高度”的页面,如果它是一个热敏式打印机,只是有一个滚动 – MadProgrammer

+0

如果您使用的是热敏打印机,您可能需要[看看这些其他问题](https://stackoverflow.com/search?q= %5Bjava%5D +热+打印机)作为一些(我知道),有自己的打印机API – MadProgrammer

回答

0

如果您正在寻找用户指定通过输入打印机,我会尝试使用系统打印对话框,更多的细节在这里:

https://docs.oracle.com/javase/tutorial/2d/printing/dialog.html

但基本上围绕增加您try块:

if (pj.printDialog()) 

如果你正在寻找的是将作业发送到指定的打印机的更有计划的方式,你事先知道打印机的名称,你想看看使用打印SER副。也许following SO question可能是有用的。

+0

我阅读所有答案,但最有用的,我发现是我认为pj.printdialog()。但有一点仍然没有解决,如何说我的打印机打印调整在纸卷bcz im打印在纸卷和纸张尺寸是字母没有任何纸卷或这种类型的下拉如纸卷,所以我需要根据纸卷来了解如何打印调整 –