Forum Stats

  • 3,815,470 Users
  • 2,259,033 Discussions
  • 7,893,115 Comments

Discussions

Mac OS X - Printing - PrinterGraphicsConfig.getDefaultTransform() throws a NullPointerException

shruda
shruda Member Posts: 16
edited Jul 22, 2014 10:18AM in Abstract Window Toolkit (AWT)

Hello guys, I currently try to get our Swing application working on Mac OS X and I found one problem which blocks me. Our application usese the java.awt.print package to generate print-outs.

One very important information for us is the printers used DPI/PPI setting. We've found a long time ago a tutorial which uses the given Graphics object of the java.awt.Printable.print(Graphics, PageFormat, int) method to get this neseccary information.

The implementation of our looks like the following:

/**
 * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
 */
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
  if (pageIndex != 0)
  return Printable.NO_SUCH_PAGE;

  Graphics2D graphics2D = (Graphics2D) graphics;

  // get the current scale
  AffineTransform defaultDeviceTransform = graphics2D.getDeviceConfiguration().getDefaultTransform();
  double deviceScaleX = defaultDeviceTransform.getScaleX();
  double deviceScaleY = defaultDeviceTransform.getScaleY();

  /*
  * this method gets an graphics from the printer which is already scaled to the desired resolution and therefore
  * the dpi can be calculated by the scale of the transformation.
  * -> no other way to determine the dpi were found
  */

  double dpi = deviceScaleX * 72;
  System.out.println("The Printer uses the following DPI setting: " + dpi);

  myPrintingMethod(graphics2D, dpi);
  return Printable.PAGE_EXISTS;
}

This way to get the DPI/PPI setting of the printer works perfect if the operating system is Microsoft Windows. But in case of the Mac OS X throws the Line 13. the following NullPointerException:

java.lang.NullPointerException

at java.awt.geom.AffineTransform.<init>(AffineTransform.java:488)

at sun.print.PrinterGraphicsConfig.getDefaultTransform(PrinterGraphicsConfig.java:101)

at com.intergraph.web.plugin.printing.PrintableDocument.print(PrintableDocument.java:122)

at sun.lwawt.macosx.CPrinterJob$6.run(CPrinterJob.java:697)

at sun.lwawt.macosx.CPrinterJob.printAndGetPageFormatArea(CPrinterJob.java:707)

at sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)

at sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:299)

at com.intergraph.web.plugin.printing.PrintEngine.print(PrintEngine.java:177)

at com.intergraph.web.plugin.printing.controller.PrintDocumentJob.run(PrintDocumentJob.java:41)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:745)

I tried to find a workaround but I wasn't sucessful and I only found the following thread from 2008 Simple Printing example that raises many questions .... the result of this thread is not the same but it describes the same problem...

Does anybody of you know a way to figure out the DPI/PPI setting of a printer?

I'm tested it with:

JDK 8u5

OS X 10.9.4

Thanks in advance!

Best Regards,

Steve

Tagged:

Answers

This discussion has been closed.