public class ActiveXPrinter extends Composite{
private static Variant documents ;
private static ActiveXPrinter printer;
private static Display display;
private static Shell shell;
// Microsoft Internet Explorer ProgID: Shell.Explorer.2
// Codesoft ProgID : Lppx2.Application
private static String progId = "Lppx2.Application";
private static OleFrame. myFrame;
private static OleControlSite controlSite;
private static OleAutomation automation;;
private static OleDispatch appActiveDoc;
private static OleDispatch appDocs;
private File currentFile;
private 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);
documents = (new OleDispatch(automation)).Invoke("Documents",new Variant[0]);
appDocs = new OleDispatch(documents.getAutomation());
}
public static ActiveXPrinter getInstance() {
System.out.println("printer =========" + printer);
if (printer == null) {
try{
display = Display.getDefault();
shell = new Shell(display);
printer = new ActiveXPrinter(shell,0);
}catch (Exception e) {
e.printStackTrace();
quitAppNow();
}
}
return printer;
}
public void print (String fileName,DynamicModel varMap) throws AppException {
try {
currentFile = this.parseFile(fileName, varMap);
display.syncExec(new Runnable(){
public void run() {
try{
initialize();
}catch(Exception e){
e.printStackTrace();
}
}
});
currentFile.delete();
} catch (Exception e) {
e.printStackTrace();
quitAppNow();
this.dispose();
printer = null;
throw new AppException(e.getMessage());
}
}
public File parseFile(String template, DynamicModel varMap)
throws AppException {
String filePath = "D:/bea/user_projects/service/applications/service/template/";
// String filePath = "./applications/service/template/";
File printFile = new File(filePath + "temp_" + template);
File file = new File(filePath + template);
if (!file.exists() || !file.isFile()) {
throw new AppException("File '" + file.toString() + "' not found!");
}
if (!file.canRead()) {
throw new AppException("Can't access File '" + file.toString() + "'!");
}
BufferedReader reader = null;
BufferedWriter writer = null;
try {
StringBuffer buf = new StringBuffer();
String line = null;
reader = new BufferedReader(new FileReader(file));
writer = new BufferedWriter(new FileWriter(printFile));
reader.read();
while ((line = reader.readLine()) != null) {
while (line.indexOf("#{") >= 0) {
String start_str = "";
String bind_name = "";
String bind_value = "";
int bs = line.indexOf("#{");
start_str = line.substring(0, bs);
bind_name = line.substring(bs + 2, line.indexOf("}"));
bind_value = StringUtils.toString(varMap.getParam(bind_name));
String end_str = line.substring(line.indexOf("}") + 1);
line = start_str + bind_value + end_str;
}
writer.write(line);
writer.newLine();
buf.append(line + "\n");
}
reader.close();
reader = null;
return printFile;
} catch (IOException ioe) {
ioe.printStackTrace();
throw new AppException("Error reading File '" + file.toString() + "'!");
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
}
}
}
}
private void initialize() throws AppException {
try{
if (!currentFile.canRead() ) {
System.out.println("Unable to read file : " + currentFile);
} else {
Variant arg[] = new Variant[1];
arg[0] = new Variant(false);
appDocs.Invoke("CloseAll", arg);
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);System.out.println("appActiveDoc ========" + appActiveDoc);
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();
quitAppNow();
this.dispose();
printer = null;
throw new AppException(excpt.getMessage());
}
}
private static void quitAppNow() {
appDocs.Invoke("CloseAllDocuments", new Variant[] { new Variant(false) });
int cmdId[] = automation.getIDsOfNames(new String[] { "Quit" });
if (cmdId != null) {
automation.invoke(cmdId[0]);
}
automation.dispose();
controlSite.dispose();
myFrame.dispose();
automation = null;
controlSite = null;
myFrame. = null;
//this.dispose();
}
public static void main(String[] args) {
DynamicModel varMap = new DynamicMapModel();
varMap.addParam("SN", "11S43W8347YK1090818R03");
try{
ActiveXPrinter.getInstance().parseFile("42c8897.lab", varMap);
}catch(Exception e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------
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]);
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/1700919/viewspace-364452/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/1700919/viewspace-364452/