Printing is not an easy task. It is easy to get tied into intricate details of environment specific configuration settings such as margins and paper size. Therefore, printing a PDF without a library is not recommended unless you have a lot of time or are really interested in that sort of thing. There are two libraries that can perform this task already. One will be exampled today, and another tomorrow.
Today’s library is PDFRenderer https://pdf-renderer.dev.java.net/. This library was released by Sun a couple of years ago. Note that this example does not map a FileChannel object as some examples do. Using a FileChannel made the file not able to be deleted until garbage collection was run. See the bug in the code comments for more details.
try
{
File f = null;
RandomAccessFile fis = null;
FileChannel fc = null;
ByteBuffer bb = null;
String printer = YOUR_PRINTER_NAME;
PrintService printService = PrintHelper.getPrintService(printer);
f = YOUR_PDF_FILE;
//Read only access would work too
fis = new RandomAccessFile(f, "rw");
fc = fis.getChannel();
bb = ByteBuffer.allocate((int)fc.size());
fc.read(bb);
//Do not map the file to a ByteBuffer as the examples show.
// There is a reason why in java bug #474038
// http://bugs.sun.com/view_bug.do?bug_id=4724038
//fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
//bb = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
PDFPrintPage pages = new PDFPrintPage(pdfFile);
// Create Print Job
PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setPrintService(printService);
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pf.setOrientation(PageFormat.PORTRAIT);
Paper paper = new Paper();
//This is to fix an error in PDF-Renderer
//View http://juixe.com/techknow/index.php/2008/01/17/print-a-pdf-document-in-java/ for details
//Printing a PDF is also possible by sending the bytes directly to the printer, but
// the printer would have to support it.
paper.setImageableArea(0,0,paper.getWidth() * 2,paper.getHeight());
pf.setPaper(paper);
pjob.setJobName(f.getName());
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
pjob.print();
}
catch (FileNotFoundException e)
{
//do your error action
}
catch (IOException e)
{
//do your error action
}
catch (PrinterException e)
{
//do your error action
}
finally
{
try
{
if (fc != null)
{
fc.close();
fc = null;
}
}
catch (IOException e)
{
log.error(e);
//handle error here
}
try
{
if (fis != null)
{
fis.close();
fis = null;
}
}
catch (IOException e)
{
//handle error here
}
if (bb != null)
{
bb.clear();
}
}
Because this library has the bug that makes it not want to print in half size when printing a portrait-oriented PDF, I cannot recommend using this library unless you find it is the only thing that will do the job. Such obvious bugs that have been around for over a year point to unmaintained code or some other upstream issues. Tune in tomorrow for another library that is maintained that will get the job done as well.


