BCL easyPDF SDK
easyPDF SDK Usermanual
PDF Creator Programming API  |  Download Free Trial  |  Contact Us to Purchase

Native Java Printer API

Usage

The native Java API is very similar to the JACOB API, with only a few distinct differences.

First, you need to import com.bcl.easypdf.printer.*;.

The jar file is called easypdf.jar, and it is under c:\Program Files\Common Files\BCL Technologies\easyPDF 8\easypdf.jar.

Oracle JAVA SE Development Kit 1.5 or above is required.

COM exceptions were replaced by the new PrinterException.

The Printer class has a dispose() method, which means it really needs to be deterministically disposed. Relying on the garbage collector is not recommended, because each Printer object launches a separate worker process. Even though these worker processes are sleeping while not executing a function, they are still in the memory, and only really quit when the Printer object is disposed.

The Printer object should be treated as if it were an expensive resource, such as a file, mutex, or a database connection. If you would like to know what really is inside Printer, it is just a named pipe. However, the worker process is programmed to only quit when the pipe is closed.

If the customer's application crashes, the system automatically closes all pipes belonging to the process, which means all related worker processes automatically quit as well.

The minimal Java sample code looks like this:
(Note: all Native Java sample code and declarations have a light red background color)

Printer printer = new Printer();
try
{
   printer.getPrintJob().PrintOut("c:\\test\\input.docx", "c:\\test\\output.pdf");
}
catch(PrinterException e)
{
   System.out.println(e);
}
finally
{
   printer.dispose();
}

The key here is the finally block, which calls dispose().

Printer's constructor is designed to never throw exceptions. That's because it does not launch a worker process and does not create a named pipe, it merely initializes a few variables to their default values. In other words, creating a Printer object is extremely lightweight, like creating a Color object.

However, as soon as you do anything else, even getting a property, it instantly launches a worker process.

Notification Events

Notification events are new to the Java API, as they did not exist in JACOB. Here is an example that catches OnPageStart, which is called before printing each page:

SampleEvents handler = new SampleEvents();
Printer printer = new Printer();
try
{
   PrintJob printJob = printer.getPrintJob();
   printer.getPrintJobMonitor().events = handler;
   printJob.PrintOut("c:\\test\\input.docx", "c:\\test\\output.pdf");
}
catch(PrinterException e)
{
   System.out.println(e);
}
finally
{
   printer.dispose();
}

SampleEvents is your class, where you have a chance to respond to all events:

import com.bcl.easypdf.printer.*;

public class SampleEvents implements IPrintJobMonitorEvents
{
        public prnMonitorResponse OnPrinterInitEventHandler(int uID, PrintJobInfo jobInfo)
        {
                return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
        }

        public prnMonitorResponse OnPrinterUpdateEventHandler(int uID, PrintJobInfo jobInfo)
        {
                return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
        }

        public prnMonitorResponse OnPrinterStartEventHandler(int uID)
        {
                return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
        }

        public prnMonitorResponse OnPageStartEventHandler(int uID, int pageNumber)
        {
                System.out.print("Page ");
                System.out.print(pageNumber + 1);
                System.out.print("...");
                return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
        }

        public prnMonitorResponse OnPrinterEndEventHandler(int uID, int errCode)
        {
                return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
        }
}

Note that even if you just want to handle a single event, you are still required to handle them all at once.

Server-Side Operation

Printing from the server side has always required easyPDF Loader, a special service that helps execute functions under a different user account.

The Native Java SDK offers two possible solutions for server-side printing. One is called the Loader Service, and the other one is called Impersonation.

Customers who are printing from a web server or a system service should study both alternatives, and decide which one they prefer.

PrinterMonitor

PrinterMonitor is a desktop-only feature designed to enable traditional File > Print functionality in a desktop environment. It also supports notification callback events, and the output PDF files can be named programmatically.

Use PrintJobMonitor, instead of PrinterMonitor, to monitor a PrintJob.PrintOut function. PrinterMonitor is only for enabling interactive prints.

PrinterMonitor should never be used on the server side, or from a service. It is not compatible with Impersonation or the Loader Service.

You can start the printer monitor by calling printer.getPrinterMonitor(). The printer monitor will only stop when the printer object is disposed. PrinterMonitor is not blocking, because it is running in another thread in an external process.

It is a good idea to specify the output file path before you begin printing:

Printer printer = new Printer();
printer.useLoader = false;
try
{
   printer.getPrinterMonitor().setOutputFileName("c:\\temp\\java.pdf");

   System.out.println("You can begin printing now. When finished, press Enter to stop the printer monitor.");
   try { System.in.read(); } catch(IOException e) { }
}
catch(PrinterException e)
{
   System.out.println(e);
}
finally
{
   printer.dispose();
}

The same file name is used for all subsequent operations. If you don't explicitly set the file name, it is automatically chosen, usually based on the input file name. Be careful — even if you normally have access to a directory, the printing application may not.

You may also print programmatically after the printer monitor is started. However, it is preferred to use GenericPrintJob.PrintOut for all non-interactive printing.

You may choose to listen on print progress notification events. Since Java does not have native events, all callbacks must be implemented in a separate class via inheritance:

public class MonitorEvents implements IPrinterMonitorEvents
{
   public prnMonitorResponse OnPrinterInitEventHandler(int uID, PrintJobInfo jobInfo)
   {
      System.out.println("Init");
      jobInfo.OutputFileName = "c:\\test\\output.pdf";
      return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
   }

   public prnMonitorResponse OnPrinterUpdateEventHandler(int uID, PrintJobInfo jobInfo)
   {
      System.out.println("Update");
      return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
   }

   public prnMonitorResponse OnPrinterStartEventHandler(int uID)
   {
      System.out.println("Start");
      return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
   }

   public prnMonitorResponse OnPageStartEventHandler(int uID, int pageNumber)
   {
      System.out.print("Page ");
      System.out.print(pageNumber + 1);
      System.out.println("...");
      return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
   }

   public prnMonitorResponse OnPrinterEndEventHandler(int uID, int errCode)
   {
      System.out.println("End");
      return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
   }
}

Now it just takes a few extra lines to set this up:

Printer printer = new Printer();
printer.useLoader = false;
try
{
   printer.getPrinterMonitor().events = handler;

   System.out.println("You can begin printing now. When finished, press Enter to stop the printer monitor.");
   try { System.in.read(); } catch(IOException e) { }
}
catch(PrinterException e)
{
   System.out.println(e);
}
finally
{
   printer.dispose();
}

The example above shows an alternative way of setting up the output file name.

If you want to pop up the Save dialog and let the user choose the output file name, you may add the following lines:

   public prnMonitorResponse OnPrinterInitEventHandler(int uID, PrintJobInfo jobInfo)
   {
      System.out.println("Init");
      jobInfo.OutputFileName = "c:\\test\\DefaultOutputFileName.pdf";
      jobInfo.getPDFSetting().UiAlerts = true;
      jobInfo.getPDFSetting().UiFileDialog = true;
      jobInfo.getPDFSetting().UiPropertiesDialog = true;
      return prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION;
   }

If you specify OutputFileName, that's going to be the default. If you do not, the default file name is automatically generated based on the print job's name. Either way, the user will get a chance to override it.

Please note that callback notifications are coming from a worker thread, which is listening in the background. This worker thread is generally sleeping while there are no notifications. The thread starts when printer.getPrinterMonitor() is called, and it only stops when the printer object is explicitly disposed. Please do not rely on garbage collection alone; easyPDF requires deterministic destruction.

It is not wise to update the program's main GUI from those event handlers without proper thread synchronization. Writing to the console is natively synchronized.