主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桥文档,看起来你需要在一个SWT组件中嵌入AWT,但是我的类没有任何组件。
我是否需要创建组件方法并在组件中运行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调用。
答案 0 :(得分:1)
打开打印机对话框时,需要打开一个零大小的shell,这样看起来主SWT Shell处于非活动状态,而Swing模式对话框就在它顶部。类似地,当您关闭挥杆对话框时,需要关闭零大小的Shell。
java.awt.Dialog awtDialog = ...
Shell shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.NO_TRIM);
shell.setSize(0, 0);
shell.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
awtDialog.requestFocus();
awtDialog.toFront();
}
});
答案 1 :(得分:0)
由于我没有信誉将其添加为评论,因此我将发布答案。对于其他偶然发现此问题的人来说,问题是“ Control类型的addFocusListener(FocusListener)方法不适用于参数(new FocusAdapter(){})”)。
问题是您的进口商品有误。您可能已经导入了awt FocusAdapter,而不是swt,反之亦然。