Skip to content

How to export to PDF, SVG & PNG

Luca edited this page Aug 31, 2023 · 4 revisions

How to export to PDF & SVG

JPlotter also supports exporting to SVG, PDF and PNG.
JPlotter can export two different types of objects: A JPlotterCanvas instance (e.g. BlankCanvas) or a Java Awt Container which may contain multiple subcomponents. Those subcomponents will be contained in the export.
With the Imaging Kit library (which is included in JPlotter) it is also easily possible to export a frame (or a canvas) to png.

The file(s) will then be exported to the root folder of the project.

Several utility methods are included in JPlotter to make the exporting process as easy as possible.

Exporting using the ExportUtil class

Simply exporting a canvas

The ExportUtil class contains methods to export a canvas to PDF, SVG and PNG, called canvasToPDF(JPlotterCanvas canvas, String path), canvasToSVG(...), canvasToPNG(...).

BlankCanvas canvas = new BlankCanvas();
canvas.setRenderer(new CoordSysRenderer());
// ... add content to the canvas
canvasToPDF(canvas, "filename.pdf"); // specifies the filename of the export

There are also overloaded methods that don't have the path argument, but use the current timestamp as the filename.

Simply exporting a frame

The ExportUtil class contains methods to export a frame to PDF, SVG and PNG, called frameToPDF(JFrame frame, String path), frameToSVG(...), frameToPNG(...).

JFrame frame = new JFrame();
frame.getContentPane().add(canvas.asComponent());
// ... add content to the canvas & frame
frameToPDF(canvas, "filename.pdf"); // specifies the filename of the export

There are also overloaded methods that don't have the path argument, but use the current timestamp as the filename.

Creating a filechooser menu

It is also possible to create a file chooser for the user to save the exported canvas to a specific location.

BlankCanvas canvas = new BlankCanvas();
canvas.setRenderer(new CoordSysRenderer());
// ... add content to the canvas
createSaveFileChooserMenu(canvas)

An equivalent method for frames is also available: createSaveFileChooserMenu(JFrame frame).

Exporting manually without helper methods

Exporting a single canvas

Exporting to PDF

PDDocument doc = canvas.paintPDF(); 
doc.save("filename.pdf"); // specifies the filename of the export
doc.close(); // close resources

Exporting to SVG

Document doc = canvas.paintSVG();
SVGUtils.documentToXMLFile(doc, new File("filename.svg")); // specifies the filename of the export

Exporting to PNG

Img img = new Img(canvas.asComponent().getSize());
img.paint(g -> canvas.asComponent().paintAll(g));
ImageSaver.saveImage(img.getRemoteBufferedImage(), "canvas_export.png");

Exporting a Java container

It is also possible to export a Container (JPanel, JFrame, ...) and its subcomponents (e.g. multiple canvases).
This can be done as shown below:

Exporting to PDF

PDDocument doc = PDFUtils.containerToPDF(frame.getContentPane()); // pass the container to be exported
doc.save("frame_export.pdf"); // specifies the filename of the export
doc.close();

Exporting to SVG

Document svg = SVGUtils.containerToSVG(frame.getContentPane()); // pass the container to be exported
SVGUtils.documentToXMLFile(svg, new File("frame_export.svg")); // specifies the filename of the export

Exporting to PNG

Img img = new Img(frame.getContentPane().getSize());
img.paint(g -> frame.getContentPane().paintAll(g));
ImageSaver.saveImage(img.getRemoteBufferedImage(), "frame_export.png");

A use case for an export like this would be the following frame:

In an export both the slider at the bottom and the canvas will be contained.