Close Documents

As already seen in the openDocument section, documents can be closed automatically, if they are opened and processed within in a try-with-resource block.
try (OpenedDocument document = ((Renderer) renderer).openDocument("DSN", new TmpCopyFromString("w2mlstring", W2MLDocumentType.class))) {
  // document will automatically be closed, when we leave the try
  // block - no matter, if regularly or caused by exception.
} 
catch (RendererException e) {
  // Exceptions during opening or rendering
} 
catch (Exception e) {
  // Exception during close
}<

If try-with-resource is not applicable in your code, the close method can also be invoked directly. The following code is exactly equivalent to the solution above:

OpenedDocument document = null;
try {
  document = ((Renderer) renderer).openDocument("DSN", ew TmpCopyFromString("w2mlString", W2MLDocumentType.class));
}
catch (RendererException e) {
  // Exceptions during opening or rendering
  // ...
}
finally {
  try {
    if (document != null)
      document.close();
    }
    catch (Exception e) {
      // Exception during close
    }
}

Note: Calling close several times for the same document doesn't make sense, but will not cause an Exception. Calling (almost) any other method will cause an Exception.