有很多问题涉及打印机队列中的PrinterJobs而不是打印,但我还没有发现有关打印作业永远不会进入队列的讨论。
我的测试程序(参见下面的代码 - 两个类PrintIt,它是Printable,和Report,它是一个带有main的jFrame)似乎运行良好,没有例外,但从不在打印队列中放入任何东西。
按照代码我将包括正在运行的程序的屏幕截图。
package SVDP;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
/**
*
* @author Wesley Stupar
*/
public class Report extends javax.swing.JFrame {
static Graphics g;
static Paper pap;
static PageFormat pf;
static Report report;
/**
* Creates new form Report
*/
public Report() {
initComponents();
g = this.getGraphics();
pap = new Paper();
pap.setImageableArea(36, 36, 540, 720); //1/2" margins, 8.5x11
pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
pf.setPaper(pap);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
report = new Report();
report.setVisible(true);
}
});
PrintIt printer = new PrintIt();
if(g == null){
System.out.println("Graphics context is null.");
System.exit(0);
}
report.generate();
try {
System.out.println("Calling print");
printer.print(g, pf, 0);
} catch (Exception pe) {
System.out.println("Printer exception: "+pe);
}
// report.setVisible(false);
}
/**
* Generate the page to be printed
*/
public void generate(){
g.drawString("Hello printer!", 100, 100);
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
--------------------------------------------------------------------
package SVDP;
import java.awt.print.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
/**
*
* @author Wesley Stupar
*/
public class PrintIt implements Printable {
PrinterJob job;
public PrintIt() {
job = PrinterJob.getPrinterJob();
if (job.printDialog()) {
try {
job.print();
} catch (Exception PrintException) {
System.out.println("PrintIt exception: "+PrintException);
}
}
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
job.setPrintable(this);
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* tell the caller that this page is part of the printed document */
System.out.println("Finished print");
return PAGE_EXISTS;
}
我不允许发布图片,但我将描述在NetBeans下运行的程序显示的内容:
程序继续运行但没有任何内容进入打印队列。该程序必须手动终止。
问题结束。
答案 0 :(得分:0)
After looking at this very helpful post:
我看到我试图将图像渲染到Printable之外,而不是正确地实例化Printable。