主 GUI 是基于 SWT 的。我正在通过单击按钮从 printPDF 类运行打印操作。
public void startPDFPrint() throws Exception {
Display.getCurrent().syncExec(
new Runnable() {
public void run(){
try {
new AplotPdfPrintLocal().printPDF("c:\\temp\\file.pdf", "PDF Print Job");
}
catch (IOException e) {
e.printStackTrace();
}
catch (PrinterException e) {
e.printStackTrace();
}
}
});
}
printPDF 类没有任何组件或 GUI。它基本上只是创建一个运行打印作业。
public class PDFPrintPage implements Printable {
类中仅有的两个方法
public void printFile(String filename) throws IOException { (setups the print)
public int print(Graphics g, PageFormat format, int index)
throws PrinterException {
在 printFile 方法中有一行代码可以打开本地打印机对话框
pjob.printDialog()
该对话框基于 AWT。
如何打开此对话框,以便我的用户可以选择打印机和份数?
我已经阅读了 SWT_AWT 桥接文档,看起来您需要将 AWT 嵌入到 SWT 组件中,但我的类没有任何组件。
是否需要创建组件方法并在组件中运行 printFile 代码?
我知道如果我能弄清楚这件作品,它也将有助于解决我遇到的所有其他问题。
编辑
请查看我的代码并告诉我哪里出错了。它符合并运行,但我在 Dialog 行收到 SWT Thread 异常。
public class PDFPrintPage extends ApplicationWindow{
private String fileURL;
private PageFormat pfDefault;
private PrinterJob pjob;
private PDFFile pdfFile;
public PDFPrintPage(Shell parent, String inputFileName) {
super(parent);
this.fileURL = inputFileName;
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected Control createContents(Composite parent) {
final Composite swtAwtComponent = new Composite(parent, SWT.EMBEDDED);
final java.awt.Frame frame = SWT_AWT.new_Frame( swtAwtComponent );
final javax.swing.JPanel panel = new javax.swing.JPanel( );
frame.add(panel);
JButton swingButton = new JButton("Print");
panel.add(swingButton);
swingButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionevent) {
try {
printFile(fileURL, frame);
}
catch (IOException e) {
e.printStackTrace();
}
}
});
return swtAwtComponent;
}
public void printFile(String filename, Frame panel) throws IOException {
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
pdfFile = new PDFFile(bb); // Create PDF Print Page
final PrintPage pages = new PrintPage(pdfFile);
pjob = PrinterJob.getPrinterJob();
pfDefault = PrinterJob.getPrinterJob().defaultPage();
Paper defaultPaper = new Paper();
defaultPaper.setImageableArea(0, 0, defaultPaper.getWidth(), defaultPaper.getHeight());
pfDefault.setPaper(defaultPaper);
pjob.setJobName(file.getName());
final Dialog awtDialog = new Dialog(panel);
Shell parent = getParentShell();
Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
shell.setSize(100, 100);
shell.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
awtDialog.requestFocus();
awtDialog.toFront();
}
});
//if (pjob.printDialog()) {
pfDefault = pjob.validatePage(pfDefault);
Book book = new Book();
book.append(pages, pfDefault, pdfFile.getNumPages());
pjob.setPageable(book);
try {
pjob.print();
}
catch (PrinterException exc) {
System.out.println(exc);
}
//}
}
class PrintPage implements Printable {
private PDFFile file;
PrintPage(PDFFile file) {
this.file = file;
}
public int print(Graphics g, PageFormat format, int index) throws PrinterException {
int pagenum = index + 1;
if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
Graphics2D g2 = (Graphics2D) g;
PDFPage page = file.getPage(pagenum);
Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
(int) format.getImageableWidth(), (int) format.getImageableHeight());
g2.translate(0, 0);
PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
try {
page.waitForFinish();
pgs.run();
} catch (InterruptedException ie) {
}
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
}//End PrintPage Class
}//End PDFPrintPage Class
我可能会将您的建议代码添加到完全错误的位置。我的想法是在 focusGained(FocusEvent e) 方法中添加 printDialog 调用的位置。