Researching a bit about the handling of a PDF document I found a library called JPedal for manipulating PDF documents from Java.
Like other libraries I’ve seen among other iText and iReport, which are very comprehensive and allow you to do many tasks, including printing. Disadvantage that the latter is that you need to have installed Acrobat Reader and for many users this is not possible.
But even with JPedal lets you see the Acrobat Reader tools within the Java application, providing a task pane for the PDF to manipulate.
With PrinterJob.lookupPrintServices can get the services available and print them PrinterJob a concrete one.
PrintService[] psService = PrinterJob.lookupPrintServices(); PrinterJob pjPrintJob = PrinterJob.getPrinterJob(); pjPrintJob.setPrintService(psService[0]); // Assigns the size of the paper to A4. Paper paper = new Paper(); paper.setSize(595, 842); paper.setImageableArea(0, 0, 595, 842); PageFormat pf = pjPrintJob.defaultPage(); pf.setPaper(paper);
Loads the PDF to be printed. The file is called wc_PDF.pdf printed and given format.
PdfDecoder pdfD = null;
pdfD = new PdfDecoder(true);
pdfD.openPdfFile("wc_PDF.pdf");
pdfD.setPageFormat(pf);
It sends the file to print
printJob.setPageable(pdfD); printJob.print();
And to finalize the document is closed.
pdf.closePdfFile();
The example would then complete as follows:
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import javax.print.PrintService;
import org.jpedal.PdfDecoder;
public final void wc_PrintPDF() {
PdfDecoder pdfD = null;
try {
PrintService[] psService = PrinterJob.lookupPrintServices();
PrinterJob pjPrintJob = PrinterJob.getPrinterJob();
pjPrintJob.setPrintService(psService[0]);
Paper paper = new Paper();
paper.setSize(595, 842);
paper.setImageableArea(0, 0, 595, 842);
PageFormat pf = pjPrintJob.defaultPage();
pf.setPaper(paper);
pdfD = new PdfDecoder(true);
pdfD.openPdfFile("wc_PDF.pdf");
pdfD.setPageFormat(pf);
pjPrintJob.setPageable(pdfD);
pjPrintJob.print();
} catch (Exception e) {
e.printStackTrace();
} finally {
pdfD.closePdfFile();
}
}

