Question

I'm measuring some font metrics in my program and then use those to create an output PDF file.

For that I'm using Graphics2D, FontContextRendered, GlyphVectors and all that goes along with it. I'm calling getGraphics() on a JTextArea with specified font.

However, if two users create the same document with the same data, but on two different devices, I need those documents to be exactly the same.

But, since Graphics2D uses current device's DPI, which my differ from device to device, that may not be the case.

Now I can do two things:

  1. I can force Graphics2D to use a specific DPI (which would be 96 in my case).
  2. I can find out which DPI is Graphics2D using and then augment the results according to that.

I'm guessing that second way is easier, but I don't know how to do it either way. Can you help me?

Was it helpful?

Solution

Graphics2D g2d;

final AffineTransform trans = g2d.getDeviceConfiguration().getNormalizingTransform();
System.out.println(trans.getScaleX()*72+" DPI horizontally");
System.out.println(trans.getScaleY()*72+" DPI vertically");

You can set the transformation to the Graphics2D to force 72 DPI or scale it to any other DPI using the ratio between your desired DPI and 72 DPI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top