Java PDF Blog

PDF solutions for big and small customers

Java and PDF development - our personal experiences and discoveries

Download JPedal

Download JPedal PDF viewers

PDF to Image service

Try our PDF to image conversion service now.

Java PDF Ebook Solution

Try our Ebook solution now.

Subscribe

Your email:

Java PDF blog

Current Articles | RSS Feed RSS Feed

Convert PDF to grayscale or black and white

Posted by Mark Stephens on Fri, Mar 12, 2010 @ 02:16 AM
Submit to Digg digg it |  Add to delicious  delicious |  Submit to StumbleUpon StumbleUpon | Submit to Reddit reddit 

PDFs are designed to use full colour with transparency. We generally use ARGB to provide the best quality display of PDF content and we are always trying to improve our quality of output. So it was something of a culture shock to have several inquiries about making the output WORSE. They wanted the PDF displayed or printed in Grayscale or black and white...

Taking a PDF an making the output grayscale or black and white does result in some image deterioration which varies from file to file. On a page of black and white text it is negligible whereas it can have a big effect on images. Here is an example.

original PDFThe original...

grayscale PDF...as grayscale

black and white PDFand in black and white.

 As you can see in this example, grayscale works well whereas black and white does impact image appearance.

The advantages of having a PDF in grayscale or black and white are that it allows for a smaller image size and allows it use in faxes (which do not seem to be as extinct as I had believed). It also means that people who do lots of printing can use the cheaper print modes.

There are several ways to convert a PDF to grayscale or black and white. If you are using JPedal and want to use the feature we have added, there is a new tutorial here.

0 Comments Click here to read/write comments

Java printing of custom Swing Components

Posted by Mark Stephens on Wed, Feb 10, 2010 @ 09:50 AM
Submit to Digg digg it |  Add to delicious  delicious |  Submit to StumbleUpon StumbleUpon | Submit to Reddit reddit 

If you are implementing printing in Java, there is a really good tutorial showing how to add printing support to any Swing component. Printing generally works very well and we use it in our PDF library to provide printing of PDF files.

The example uses a PrinterJob to print via Java - essentially, your Swing component just needs to implement Pageable or Printable and have a print() method. This is often virtually identical to the draw method, but just renders onto a Print Graphics2D object rather than the Screen Graphics2D object.

However, PrinterJob is a rather basic function and does not have all the features of DocPrint. In particular, DocPrintJob offers a couple of very useful additional features:-

1. It can print different sized pages (with PrinterJob it seems hard-coded to the size of the first page).

2. It adds Listeners functionality to allow monitoring of Print activity. The method addPrintJobListener provides events to query the printer.

Converting from PrinterJob to DocPrintJob

It is very straight-forward to switch from using PrinterJob to DocPrintJob. In this example I have a custom Swing component decode_pdf which implements Pageable to print itself. Here are the steps:-

1. Alter your PrinterJob object to a DocPrintJob. In my code I just had to change

PrinterJob pj = PrinterJob.getPrinterJob();

to 

PrintService[] service=PrinterJob.lookupPrintServices(); //list of printers

printJob= service[i].createPrintJob(); //i is whichever printer you use

2. Wrap your Pageable or Printable object inside a doc, specifying which interface to use in the call

 //wrap in Doc as we can then add a listeners
                Doc doc=new SimpleDoc(decode_pdf, DocFlavor.SERVICE_FORMATTED.PAGEABLE,null);

 

3. Alter the print call from directly calling your Swing component to use the doc so 

decode_pdf.print();

becomes

printJob.print(doc,null);

 

Finally, you can now add a listener to track what happens.

printJob.addPrintJobListener(new PDFPrintJobListener());
 

Here is a simple example of the Listener class

private class PDFPrintJobListener implements PrintJobListener {
        public void printDataTransferCompleted(PrintJobEvent printJobEvent) {
            System.out.println("printDataTransferCompleted="+printJobEvent.toString());
        }

        public void printJobCompleted(PrintJobEvent printJobEvent) {
             System.out.println("printJobCompleted="+printJobEvent.toString());
        }

        public void printJobFailed(PrintJobEvent printJobEvent) {
             System.out.println("printJobEvent="+printJobEvent.toString());
        }

        public void printJobCanceled(PrintJobEvent printJobEvent) {
             System.out.println("printJobFailed="+printJobEvent.toString());
        }

        public void printJobNoMoreEvents(PrintJobEvent printJobEvent) {
             System.out.println("printJobNoMoreEvents="+printJobEvent.toString());
        }

        public void printJobRequiresAttention(PrintJobEvent printJobEvent) {
             System.out.println("printJobRequiresAttention="+printJobEvent.toString());
        }
    }

So switching to DocPrintJob is simple and easy and improves printing support. And if you use our PDF library for printing, you should see the benefits in the next release.

 

 

0 Comments Click here to read/write comments

Java PDF printing spool file size

Posted by Mark Stephens on Mon, Nov 02, 2009 @ 06:09 AM
Submit to Digg digg it |  Add to delicious  delicious |  Submit to StumbleUpon StumbleUpon | Submit to Reddit reddit 
Tags: 

Java Printing Services allows Java to provide printing in programs. JPS allows you to print some types of file directly. PDF is not a supported type so it needs to be printed by another program (like our JPedal PDF library) rendering it. From a coding point of view, this works very well - you add a print( ) method to your Swing components and just paint onto the Graphics2D object (as you would to draw onto screen). This is converted into a spool file which is then passed to the printer. From a usage point of view, it does however have some issues.

The main problem we have found is that the printing creates some very large spool files. This seems to be partly because the JPS sub-system is hard-coded to render the page to a high resolution output in 32bit RGB colour with transparency. This needs a lot of memory. In addition there is no compression on the spool file (whereas Windows programs do appear to compress the file size).

Another factor that contributes to the spool size when rendering PDFs is that embedded fonts cannot be sent to the printer - they need to be turned into Vector graphics and drawn as shapes (which uses much more memory).

A useful workaround for the second issue is to try to print using built-in Java fonts. Many PDF files use a set of Basic fonts (ie Arial, Courier, Helvetica) for which there are acceptable versions built into Java. Printing with these reduces the size of the spool file.

In JPedal we added a mode to support this which is detailled at http://www.jpedal.org/support_tutPrint.php

Unfortunately JPS remains one of the weaker areas of Java and I hope Oracle will look to improve it when they takeover. 

 

 

0 Comments Click here to read/write comments

All Posts