Skip to content
Erich Seifert edited this page Feb 6, 2018 · 6 revisions

A VectorGraphics2D object can be used as a replacement for a Graphics2D object. All calls to the VectorGraphics2D instance will be recorded and can later be retrieved as a CommandSequence:

Graphics2D vg2d = new VectorGraphics2D();
vg2d.draw(new Rectangle2D.Double(0.0, 0.0, 20.0, 20.0));
CommandSequence commands = ((VectorGraphics2D) vg2d).getCommands();

This command sequence can then be exported to a EPS, PDF or SVG document using a processor for the desired file type, i.e. EPSProcessor for EPS, PDFProcessor for PDF and SVGProcessor for SVG. Format specific output options can be passed to the processor when it is created. For example, a compression option can be passed for PDF.

For convenience, the Processors class can be used with the file format name to retrieve a processor:

Processor pdfProcessor = Processors.get("pdf");
Document doc = pdfProcessor.getDocument(commands, PageSize.A4);

Finally, the document can be written to a file:

doc.writeTo(new FileOutputStream("rect.pdf"));
Clone this wiki locally