Hello World (Java) sample is a basic Java console application that generates (using the novaPDF SDK printer) one PDF file containing the text "Hello World from Java2!". It demonstrates the basic use of the INovaPDFOptions interface with j-Interop.
What this sample does:
- determines the active profile, makes a copy of it and names it "Test Java"
- sets the new profile (Test Java) as active as well as some mandatory settings (e.g. the subject of the PDF)
- generates a test PDF file
- restores the original settings of the novaPDF SDK printer driver.
“j-Interop is a Java Open Source library (under LGPL) that implements the DCOM wire protocol (MSRPC) to enable development of Pure, Bi-Directional, Non-Native Java applications which can interoperate with any COM component.“ j-Interop can be found at http://www.j-interop.org/
Note
If you encounter problems or have questions on using j-Interop, visit their FAQ page at http://www.j-interop.org/faq.html
Also, to avoid the “Logon failure: unknown user name or bad password” error, configure DCOM for remote access (detailed here - http://j-integra.intrinsyc.com/support/com/doc/remoteaccess.html#winxp) and make sure your firewall is not turned on.
Source code of the Hello World Java sample (Main.java):
package helloworld;
import java.awt.*;
import java.awt.print.*;
import java.net.UnknownHostException;
import java.util.logging.Level;
import javax.print.PrintService;
import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.common.JISystem;
import org.jinterop.dcom.core.IJIComObject;
import org.jinterop.dcom.core.JIComServer;
import org.jinterop.dcom.core.JIProgId;
import org.jinterop.dcom.core.JISession;
import org.jinterop.dcom.core.JIString;
import org.jinterop.dcom.core.JIVariant;
import org.jinterop.dcom.impls.JIObjectFactory;
import org.jinterop.dcom.impls.automation.IJIDispatch;
public class Main implements Printable {
private static String PRINTER_NAME = "novaPDF SDK 11";
private static String MESSAGE = "Hello world from Java2!";
public static String PROFILE_NAME = "Test Java";
public static int PROFILE_IS_PUBLIC = 0;
public static long NOVAPDF_DOCINFO_SUBJECT = 68;
public static String NOVAPDF_INFO_SUBJECT = "Java Hello World Document Subject";
public static long NOVAPDF_SAVE_FILE_NAME = 104;
public static long NOVAPDF_SAVE_FILEEXIST_ACTION = 108;
public static long FILE_CONFLICT_STRATEGY_AUTONUMBER_NEW = 1;
public static long NOVAPDF_INFO_VIEWER_ENABLE = 8;
public static long NOVAPDF_SAVE_PROMPT_TYPE = 101;
public static long PROMPT_SAVE_SIMPLE = 2;
JIComServer comStub = null;
IJIDispatch pNovaDispatch = null;
IJIComObject pNova = null;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws JIException, UnknownHostException {
if (args.length < 4) {
System.out.println("Please provide address domain username password");
return;
}
new Main().doPrint(args);
}
public void doPrint(String[] args) throws JIException, UnknownHostException {
//disable J-Interop Log
try {
JISystem.getLogger().setLevel(Level.INFO);
JISystem.setInBuiltLogHandler(false);
} catch (Exception e) {
//System.out.println(e.getMessage());
}
//connecting to COM/DCOM server
JISession session = JISession.createSession(args[1], args[2], args[3]);
session.useSessionSecurity(true);
JIProgId pid = JIProgId.valueOf("novapi11.NovaPdfOptions11");
pid.setAutoRegistration(true);
comStub = new JIComServer(pid, args[0], session);
pNova = comStub.createInstance();
pNovaDispatch = (IJIDispatch) JIObjectFactory.narrowObject(pNova.queryInterface(IJIDispatch.IID));
JIString PRINTER = new JIString(PRINTER_NAME);
try {
pNovaDispatch.callMethodA("Initialize2", new Object[]{PRINTER}, new JIString(""));
pNovaDispatch.callMethodA("LicenseShellExecuteFile", new Object[]{new JIString("java")});
} catch ( JIException except ) {
System.out.println("Initialize/LicenseShellExecuteFile");
except.printStackTrace() ;
}
String oldActiveProfile = "";
try {
JIVariant ap[] = pNovaDispatch.callMethodA("GetActiveProfile2", new Object[]{new JIVariant("", true)});
oldActiveProfile = ap[1].getObjectAsString().getString();
} catch ( JIException except ) {
System.out.println("GetActiveProfile2");
except.printStackTrace() ;
}
//ADD A NEW PROFILE
String activeProfile = "";
try {
JIVariant ap[] = pNovaDispatch.callMethodA("AddProfile2", new Object[]{new JIString(PROFILE_NAME), PROFILE_IS_PUBLIC, new JIVariant("", true)});
activeProfile = ap[1].getObjectAsString().getString();
} catch (JIException except) {
System.out.println("AddProfile2");
except.printStackTrace();
}
//LOAD PROFILE
try {
pNovaDispatch.callMethod("LoadProfile2", new Object[]{new JIString(activeProfile)});
} catch (JIException except) {
System.out.println("LoadProfile2");
except.printStackTrace();
}
//CHANGE SOME OPTIONS
try {
// and set some options
pNovaDispatch.callMethod("SetOptionString2", new Object[]{NOVAPDF_DOCINFO_SUBJECT, new JIString(NOVAPDF_INFO_SUBJECT)});
pNovaDispatch.callMethod("SetOptionString2", new Object[]{NOVAPDF_SAVE_FILE_NAME, new JIString("novaPDFJavaDocument")});
pNovaDispatch.callMethod("SetOptionLong", new Object[]{NOVAPDF_SAVE_FILEEXIST_ACTION, FILE_CONFLICT_STRATEGY_AUTONUMBER_NEW});
pNovaDispatch.callMethod("SetOptionBool", new Object[]{NOVAPDF_INFO_VIEWER_ENABLE, 1});
pNovaDispatch.callMethod("SetOptionLong", new Object[]{NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_SIMPLE});
} catch (JIException except) {
System.out.println("Set options");
except.printStackTrace();
}
//SAVE PROFILE
try {
pNovaDispatch.callMethod("SaveProfile2");
} catch (JIException except) {
System.out.println("SaveProfile2");
except.printStackTrace();
}
//SET IT AS ACTIVE PROFILE
try {
// set the copy profile as active profile ...
pNovaDispatch.callMethod("SetActiveProfile2", new Object[]{new JIString(activeProfile)});
} catch (JIException except) {
System.out.println("SetActiveProfile2");
except.printStackTrace();
}
//PRINT SOMETHING WITH NEW PROFILE
PrinterJob job = PrinterJob.getPrinterJob();
PrintService[] services = PrinterJob.lookupPrintServices();
Boolean flag = Boolean.FALSE;
for (int i = 0; i < services.length; i++) {
if (services[i].getName().equalsIgnoreCase(PRINTER_NAME)) {
flag = Boolean.TRUE;
try {
job.setPrintService(services[i]);
job.setPrintable(this);
job.print();
} catch (Throwable throwable) {
System.out.println("Printing Exception");
throwable.printStackTrace();
}
break;
}
}
if(flag == Boolean.FALSE){
System.out.println("Printer not found...");
}else{
System.out.println("PDF Printed");
}
//RESTORE PREVIOUS ACTIVE PROFILE
try {
if(oldActiveProfile.length() > 0) {
pNovaDispatch.callMethod("SetActiveProfile2", new Object[]{new JIString(oldActiveProfile)});
}
} catch (JIException except) {
System.out.println("SetActiveProfile2 Old");
except.printStackTrace();
}
//DELETE THE NEW PROFILE
try {
pNovaDispatch.callMethod("DeleteProfile2", new Object[]{new JIString(activeProfile)});
} catch (JIException except) {
System.out.println("DeleteProfile2");
except.printStackTrace();
}
JISession.destroySession(session);
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.drawString(MESSAGE, 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
}