Note: To be able to use the samples you must install novaPDF SDK
as samples work only with it. Download it here: nova PDF SDK.
Hello World (Java) sample is a basic Java console application that generates (using the novaPDF 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 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. Also, to avoid the “Logon failure: unknown user name or bad password” error, configure DCOM for remote access (detailed
here) and make sure your firewall is not turned on.
Source code of the Hello World Java sample (Main.java):
package helloworld;
/*
* j-interop can be found at http://www.j-interop.org/
* and it is an open source project
*
*/
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 printer%>";
private static String MESSAGE = "Hello world from Java2!";
public static String PROFILE_NAME = "Test Java";
public static int PROFILE_IS_PUBLIC = 0;
public static String NOVAPDF_INFO_SUBJECT = "Java Hello World Document Subject";
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("novapi.NovaPdfOptions");
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);
JIString UNAME = new JIString("");
JIString UKEY = new JIString("");
JIString UAPP = new JIString("");
pNovaDispatch.callMethodA("Initialize2", new
Object[]{PRINTER, UNAME, UKEY, UAPP});
pNovaDispatch.callMethodA("LicenseShellExecuteFile", new Object[]
{new JIString("java")});
JIVariant ap[] = pNovaDispatch.callMethodA("GetActiveProfile2",
new Object[]{new JIVariant("", true), new JIVariant(0, true)});
String activeProfile = ap[2].getObjectAsString().getString();
int nActivePublic = ap[1].getObjectAsInt();
try {
pNovaDispatch.callMethod("CopyProfile2", new Object[]{new
JIString(activeProfile), new JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
} catch (JIException come) {
System.out.println("CopyProfile2:" + come.getMessage());
}
try {
// set the copy profile as active profile ...
pNovaDispatch.callMethod("SetActiveProfile2", new Object[]{new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
} catch (JIException come) {
System.out.println("SetActiveProfile2:" + come.getMessage());
}
try {
// and set some options
pNovaDispatch.callMethod("SetOptionString2", new Object[]{new
JIString("Document Subject"), new JIString(NOVAPDF_INFO_SUBJECT), new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
pNovaDispatch.callMethod("SetOptionString2", new Object[]{new
JIString("Save File"), new JIString("novaPDFJavaDocument"), new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
pNovaDispatch.callMethod("SetOptionString2", new Object[]{new
JIString("File Conflict Strategy"), new JIString("3"), new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
pNovaDispatch.callMethod("SetOptionString2", new Object[]{new
JIString("Post Save Open"), new JIString("1"), new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
pNovaDispatch.callMethod("SetOptionString2", new Object[]{new
JIString("Prompt Save Dialog"), new JIString("0"), new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
} catch (JIException come) {
System.out.println(come.getMessage());
}
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) {
throwable.printStackTrace();
}
break;
}
}
if(flag == Boolean.FALSE){
System.out.println("Printer not found...");
}else{
System.out.println("PDF Printed");
}
try {
pNovaDispatch.callMethod("SetActiveProfile2", new Object[]{new
JIString(activeProfile), nActivePublic});
} catch (JIException come) {
System.out.println("SetActiveProfile2" + come.getMessage());
}
try {
pNovaDispatch.callMethod("DeleteProfile2", new Object[]{new
JIString(PROFILE_NAME), PROFILE_IS_PUBLIC});
} catch (JIException come) {
System.out.println("DeleteProfile2:" + come.getMessage());
}
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;
}
}