code: private static void setDPI(IIOMetadata metadata, int dpi, String formatName)
              throws IIOInvalidTreeException
      {
          IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(MetaUtil.STANDARD_METADATA_FORMAT);
  
          IIOMetadataNode dimension = getOrCreateChildNode(root, "Dimension");
  
          // PNG writer doesn't conform to the spec which is
          // "The width of a pixel, in millimeters"
          // but instead counts the pixels per millimeter
          float res = "PNG".equals(formatName.toUpperCase())
                      ? dpi / 25.4f
                      : 25.4f / dpi;
  
          IIOMetadataNode child;
  
          child = getOrCreateChildNode(dimension, "HorizontalPixelSize");
          child.setAttribute("value", Double.toString(res));
  
          child = getOrCreateChildNode(dimension, "VerticalPixelSize");
          child.setAttribute("value", Double.toString(res));
  
          metadata.mergeTree(MetaUtil.STANDARD_METADATA_FORMAT, root);
      }
docs: sets the DPI metadata