我一直致力于实现一个读取pdf的简单应用程序,然后将一些信息放在JPanel中,然后生成一个bufferedimage并尝试将其发送到打印机。代码完美地生成图像并将其发送以进行打印。它与A4格式和许多其他格式完美配合,但我希望在pvc cr80卡上打印(打印机打印1016 * 642像素)
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
Container c = jPanel3;
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(im.getGraphics());
try {
String cn = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(cn); // Use the native L&F
} catch (Exception cnf) {
}
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(new Printable() {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate((int) pageFormat.getImageableWidth(),pageFormat.getImageableHight());
g2d.drawImage(im, 0, 0,1016,642, null);
return PAGE_EXISTS;
}
});
boolean ok = printJob.printDialog();
if (ok) {
try {
printJob.print();
} catch (PrinterException ex) {
System.out.println("Can't print");
}
}
}
此代码无法在pvc cr80卡片打印机(1016 * 642像素)上打印。我该如何解决?