PrinterAbortException при использовании принтера ob
Я занимаюсь разработкой системы управления классами. У меня есть модуль, где в
при получении оплаты за обучение студент должен распечатать квитанцию. Для этого я использую класс Printer Job, предоставляющий псевдопринтер, который создает документ.
код:
public PageFormat getPageFormat(PrinterJob pj)
{
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
double middleHeight =8.0;
double headerHeight = 2.0;
double footerHeight = 2.0;
double width = convert_CM_To_PPI(8); //printer know only point per inch.default value is 72ppi
double height = convert_CM_To_PPI(headerHeight+middleHeight+footerHeight);
paper.setSize(width, height);
paper.setImageableArea(
0,
10,
width,
height - convert_CM_To_PPI(1)
); //define boarder size after that print area width is about 180 points
pf.setOrientation(PageFormat.PORTRAIT); //select orientation portrait or landscape but for this time portrait
pf.setPaper(paper);
return pf;
}
protected static double convert_CM_To_PPI(double cm) {
return toPPI(cm * 0.393600787);
}
protected static double toPPI(double inch) {
return inch * 72d;
}
public class BillPrintable extends JComponent implements Printable {
public int print(Graphics graphics, PageFormat pageFormat,int pageIndex)
throws PrinterException
{
int result = NO_SUCH_PAGE;
if (pageIndex == 0) {
Graphics2D g2d = (Graphics2D) graphics;
double width = pageFormat.getImageableWidth();
g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY());
////////// code by alqama//////////////
FontMetrics metrics=g2d.getFontMetrics(new Font("Arial",Font.BOLD,7));
// int idLength=metrics.stringWidth("000000");
//int idLength=metrics.stringWidth("00");
int idLength=metrics.stringWidth("000");
int amtLength=metrics.stringWidth("000000");
int qtyLength=metrics.stringWidth("00000");
int priceLength=metrics.stringWidth("000000");
int prodLength=(int)width - idLength - amtLength - qtyLength - priceLength-17;
// int idPosition=0;
// int productPosition=idPosition + idLength + 2;
// int pricePosition=productPosition + prodLength +10;
// int qtyPosition=pricePosition + priceLength + 2;
// int amtPosition=qtyPosition + qtyLength + 2;
int productPosition = 0;
int discountPosition= prodLength+5;
int pricePosition = discountPosition +idLength+10;
int qtyPosition=pricePosition + priceLength + 4;
int amtPosition=qtyPosition + qtyLength;
try{
/*Draw Header*/
int y=20;
int yShift = 10;
int headerRectHeight=15;
int headerRectHeighta=40;
///////////////// Product names Get ///////////
///////////////// Product names Get ///////////
///////////////// Product price Get ///////////
///////////////// Product price Get ///////////
String sql1="select classid from student where fname=?";
PreparedStatement ps1 = con.prepareStatement(sql1);
ps1.setString(1,name);
ResultSet rs1 = ps1.executeQuery();
String id="";
while(rs1.next()){
id=rs1.getString(1);
}
String month=jComboBox1.getSelectedItem().toString();
String date=String.valueOf(new Date(System.currentTimeMillis()));
String total=jLabel3.getText();
String remaining = jLabel9.getText();
g2d.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
// create BufferedImage object of same width and height as of original image
g2d.setFont(new Font("Trebuchet MS", Font.BOLD, 9));
g2d.drawString("-------------------------------------",12,y);y+=yShift;
g2d.drawString(" TOP 30 Coaching Classes ",12,y);y+=yShift;
g2d.drawString("-------------------------------------",12,y);y+=headerRectHeight;
g2d.setFont(new Font("Trebuchet MS", Font.PLAIN, 8));
g2d.drawString(" Monthly Fees Receipt ",12,y);y+=yShift;
g2d.drawString("-------------------------------------",12,y);y+=headerRectHeight;
g2d.drawString("------------------------------------",10,y);y+=headerRectHeight;
g2d.drawString("Student Name "+name,10,y);y+=yShift;
g2d.drawString("Standar "+id,10,y);y+=yShift;
g2d.drawString("Month "+month,10,y);y+=yShift;
g2d.drawString("Date "+date,10,y);y+=yShift;
g2d.drawString("-------------------------------------",10,y);y+=yShift;
g2d.drawString("Partculars Amount ",10,y);y+=yShift;
g2d.drawString("------------------------------------",10,y);y+=headerRectHeight;
g2d.drawString("Total fees "+total,10,y);y+=yShift;
g2d.drawString("Amount Recevived "+temp_amount,10,y);y+=yShift;
g2d.drawString("Amount Remaining "+remaining,10,y);y+=yShift;
g2d.drawString("-------------------------------------",10,y);y+=yShift;
g2d.drawString(" For details contact ",10,y);y+=yShift;
g2d.drawString(" Vipul Bari-7506770692 ",10,y);y+=yShift;
g2d.drawString("*************************************",10,y);y+=yShift;
g2d.drawString(" THANK YOU ",10,y);y+=yShift;
g2d.drawString("*************************************",10,y);y+=yShift;
// g2d.setFont(new Font("Monospaced",Font.BOLD,10));
// g2d.drawString("Customer Shopping Invoice", 30,y);y+=yShift;
}
catch(Exception r){
r.printStackTrace();
}
result = PAGE_EXISTS;
}
return result;
}
}
когда нажата кнопка печати (моего графического интерфейса):
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new BillPrintable(),getPageFormat(pj));
try {
pj.print();
}
теперь, когда я нажимаю кнопку отмены (изображение, предоставленное заданием на принтер), это дает следующее исключение; это диалоговое окно всплывает
28 января 2018 2:46:16 top30.StudentFeesUpdate jButton1ActionPerformed
SEVERE: null
java.awt.print.PrinterAbortException
at sun.print.RasterPrinterJob.cancelDoc(RasterPrinterJob.java:1886)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1430)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1331)
at top30.StudentFeesUpdate.jButton1ActionPerformed(StudentFeesUpdate.java:563)
at top30.StudentFeesUpdate.access$400(StudentFeesUpdate.java:42)
at top30.StudentFeesUpdate$2.actionPerformed(StudentFeesUpdate.java:306)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)