How to export to PDF or SVG

sheymann edited this page Jan 4, 2015 · 1 revision
Clone this wiki locally

This Toolkit Example is part of the toolkit-demos project, that can be downloaded from the website.


Preview

Preview is the last step before export and allows display customization and aesthetics refinements. It works with the current workspace. The example below shows how to change edge coloring mode, edge thickness and label font size. See PreviewAPI documentation.

//Preview
PreviewModel model = Lookup.getDefault().lookup(PreviewController.class).getModel();

PreviewProperties prop = model.getProperties();
prop.putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
prop.putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.GRAY));
prop.putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.1f));
prop.putValue(PreviewProperty.NODE_LABEL_FONT, prop.getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont(8));

Export to PDF

The PDF and SVG exporter works with settings made in Preview. As with all exporters, it is possible to write the result in a Writer or OutputStream. In the exemple below the PDFExporter is retrieved and configured.

//Simple PDF export
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
try {
   ec.exportFile(new File("simple.pdf"));
} catch (IOException ex) {
   ex.printStackTrace();
   return;
}

//PDF Exporter config and export to Byte array
PDFExporter pdfExporter = (PDFExporter) ec.getExporter("pdf");
pdfExporter.setPageSize(PageSize.A0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ec.exportStream(baos, pdfExporter);
byte[] pdf = baos.toByteArray();