包com.prodPrint
導入Java . io . file;
導入Java . util . hashmap;
導入org . eclipse . swt . swt;
導入org . eclipse . swt . ole . win32 . ole automation;
導入org . eclipse . swt . ole . win32 . olecontrolsite;
導入org . eclipse . swt . ole . win32 . ole frame;
導入org . eclipse . swt . ole . win32 . variant;
導入org . eclipse . swt . widgets . composite;
導入org . eclipse . swt . widgets . display;
導入org . eclipse . swt . widgets . shell;
導入com . lppx 2 . ole dispatch;
公共類ActiveXPrinter擴展復合{
private OleFrame my frame = new OleFrame(這個,SWT。無);
//Microsoft Internet Explorer ProgID:Shell。Explorer.2
// Codesoft ProgID : Lppx2。應用
私有字符串progId = "Lppx2。申請”;
私有OleControlSite控件站點;
私有OleAutomation自動化;;
私有OleDispatch appActiveDoc
私有OleDispatch appDocs
文件currentFile
公共ActiveXPrinter(復合父級,int樣式){
超級(父母,風格);
myFrame =新OleFrame(這個,SWT。無);
control site = new OleControlSite(my frame,SWT。無,progId);
自動化=新ole automation(control site);
}
公共無效打印(字符串文件名){
currentFile =新文件(文件名);
initialize();
}
私有void initialize() {
嘗試{
變體文檔=(新OleDispatch(自動化))。Invoke("Documents ",new Variant[0]);
appDocs = new ole dispatch(documents . get automation());
如果(!currentFile.canRead() ) {
System.out.println("無法讀取文件:"+current file);
}否則{
變量文件[] =新變量[1];
file[0]= new Variant(currentfile . getabsolutepath());
如果(appDocs。Invoke("打開",文件)!= null) {
system . out . println(current file . getabsolutepath()+"現已打開));
}否則{
System.out.println("無法打開"+current file . getabsolutepath());
}
Variant activeDocumentV =(新OleDispatch(自動化))。調用(
" ActiveDocument ",new Variant[0]);
嘗試{
appActiveDoc = new ole dispatch(activedocumentv . get automation());
} catch(異常except){
}
變式[]數量=新變式[1];
數量[0] =新變量(1);
if (appActiveDoc。Invoke("PrintDocument ",數量)!=空)
System.out.println("打印OK ");
否則{
System.out.println("無法打印!");
}
}
}catch(異常except){
System.out.println("文檔打開後才能打印任何東西!");
ex CPT . printstacktrace();
返回;
}
}
公共靜態void main(String[] args) {
display display = display . get default();
Shell shell =新殼(顯示);
新建ActiveXPrinter(shell,0)。print("C:\Document1。實驗室”);
而(!shell.isDisposed()) {
如果(!display.readAndDispatch()) {
display . sleep();
}
}
dispose();
}
}