怎麼用java呼叫用codesoft設計的條形碼文件

terryisme發表於2009-09-26

用ireport設定的jasper有相應的程式碼來呼叫,如 JasperPrint print = JasperFillManager.fillReport()。但用ireport製作條形碼不夠精細。但用codesoft來設計的時候,我們怎麼去呼叫生成的LAB文件,並列印出來。希望大蝦指點一下。

經過好多天的努力自己終於將這個問題解決了。現在程式碼共享如下,僅供參考因為本人還不精通。

package com.prodPrint;

import java.io.File;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.lppx2.OleDispatch;

public class ActiveXPrinter extends Composite{
private OleFrame myFrame = new OleFrame(this, SWT.NONE);
// Microsoft Internet Explorer ProgID: Shell.Explorer.2
// Codesoft ProgID : Lppx2.Application
private String progId = "Lppx2.Application";
private OleControlSite controlSite;
private OleAutomation automation;;
private OleDispatch appActiveDoc;
private OleDispatch appDocs;
File currentFile;

public ActiveXPrinter(Composite parent, int style) {
super(parent, style);
myFrame = new OleFrame(this, SWT.NONE);
controlSite = new OleControlSite(myFrame, SWT.NONE, progId);
automation = new OleAutomation(controlSite);
}

public void print (String fileName) {
currentFile = new File(fileName);
initialize();
}

private void initialize() {
try{
Variant documents = (new OleDispatch(automation)).Invoke("Documents",new Variant[0]);
appDocs = new OleDispatch(documents.getAutomation());
if (!currentFile.canRead() ) {
System.out.println("Unable to read file : " + currentFile);
} else {
Variant file[] = new Variant[1];
file[0] = new Variant(currentFile.getAbsolutePath());
if (appDocs.Invoke("Open", file) != null) {
System.out.println(currentFile.getAbsolutePath() + " is opened now");
} else {
System.out.println("Unable to open " + currentFile.getAbsolutePath());
}

Variant activeDocumentV = (new OleDispatch(automation)).Invoke(
"ActiveDocument", new Variant[0]);
try {
appActiveDoc = new OleDispatch(activeDocumentV.getAutomation());
} catch (Exception excpt) {

}
Variant[] quantity = new Variant[1];
quantity[0] = new Variant(1);
if (appActiveDoc.Invoke("PrintDocument", quantity) != null)
System.out.println("Print OK");
else {
System.out.println("Unable to print !");
}
}
}catch(Exception excpt) {
System.out.println("You can't print anything until a document is opened !");
excpt.printStackTrace();
return;
}
}

public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
new ActiveXPrinter(shell, 0).print("C:Document1.Lab");
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}



------------------------------------------------------------------------------------

package com.prodPrint;

import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.Variant;

public class OleDispatch {
public OleAutomation oa;

public OleDispatch(OleAutomation oa) {
this.oa = oa;
}

public Variant Invoke(String name, Variant[] arg) {
if (oa != null) {
int[] cmdId = oa.getIDsOfNames(new String[] { name });
if (cmdId != null) {
return oa.invoke(cmdId[0], arg);
} else {
return null;
}
} else {
return null;
}
}

public String Invoke2(String name, Variant arg[]) {
int cmdId[] = oa.getIDsOfNames(new String[] { name });
return Integer.toString(cmdId[0]);
}
}

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/93029/viewspace-1027376/,如需轉載,請註明出處,否則將追究法律責任。

相關文章