Print PDF in JAVA

Q: When I print my pdf in C++ sample application it is printed
However when I use jar with Java the printed page is slightly larger
and cut off from the page (margins maybe?). I used you sample java
code for printing. I suspect some settings defaults are not the same.
A: I guess the only difference in how the margins are selected. The C+
+ sample is using Win32 functions (GetDeviceCaps()) to obtain the
printer margins whereas the JAVA sample is ignoring margins.

To account for printer margins you will need to fetch this info
(perhaps from java.awt.print.PageFormat -> getPaper() -> imageable
dimensions) and pass them to drawInRect() method.

I have found a solution to fix PDF print problem in JAVA. Here is the
modified code:

PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();

int w =(int)pf.getWidth();
int h =(int)pf.getHeight();

HashPrintRequestAttributeSet pattribs=new HashPrintRequestAttributeSet
pattribs.add(new MediaPrintableArea(0, 0, w, h,

try {
catch (PrinterException ex)
  //The Print did not complete successfully

Basically attributes fix the print margins.