W2ML Manipulation

The Java Rendering Library contains classes to process and manipulate W2ML data. Basically, any W2ML input can be processed by the pdf renderer and InDesign®, you could even create a W2ML document from scratch using the classes in the SDK / com.priint.comet.metadata package.

However, W2ML creation and manipulation can be dangerous. Mainly, in the renderer we assume, that the document structure is valid and consistent - which usually is the case, if the W2ML document has been exported from InDesign or has been generated by the renderer.

Therefore, W2ML creation and manipulation is not officially supported, unless some operations collected in the com.priint.comet.renderer.util.W2ML class.

Example

W2ML manipulation can be useful to optimize some processes. Consider the following situation:

This is the typical "Planning & Briefing" preview process. With "normal" processing, the template would be placed on the last page, the entire document would be rendered (8 pages), just to get the preview of a bit of the last page in the end.

Depending on the source document, we can achieve up to 30% performance gain (and less memory usage) by pre-processing the document, i.e.: simply removing all content before rendering.

The optimized version would be:

import javax.activation.DataHandler;

import com.priint.comet.metadata.DocumentMetadata;
import com.priint.comet.metadata.w2ml.W2MLDocument;
import com.priint.comet.model.RecordId;
import com.priint.comet.renderer.Renderer;
import com.priint.comet.renderer.OpenedDocument;
import com.priint.comet.renderer.exception.RendererException;
import com.priint.comet.renderer.io.providers.TmpCopyFromString;
import com.priint.comet.renderer.util.RendererUtils;
import com.priint.comet.renderer.util.W2ML;

public class W2MLManipulation {

  private final Renderer renderer = new Renderer();

  public void optimizedPreviewGeneration() {
    String   eightPagesW2ml = "...";
    String   processedW2ml = null;
    int   templateId = 2;
    String   itemStringId = "product.309";
    try {
      W2MLDocument w2mlDocument = W2ML.documentFromString(eightPagesW2ml);
      // clear all content
      W2ML.clearContent(w2mlDocument);
      processedW2ml = RendererUtils.objectToXML(w2mlDocument, W2MLDocument.class).toString();
    }
    catch (RendererException e) {
      // exceptions during java parsing and processing of the w2ml document
    }
     
    try (OpenedDocument document = renderer.openDocument(
                                        "DSN", 
                                        new TmpCopyFromString(processedW2ml, W2MLDocumentType.class))) {
      int groupId = document.placeTemplate(-1, templateId, new RecordId(1, 0, 0, itemStringId), 0.0, 0.0);

      DocumentMetadata dmd = document.getMetadata();
      DataHandler[] previews = dmd.getCometGroupPreviews(groupId);
      if (previews != null && previews.length > 0) {
        DataHandler itemPreview = previews[0];
        // ... + save etc.
      }
    } catch (RendererException e) {
      // exception during rendering
    } catch (Exception e) {
      // exception during close
    }
  }
}