-
Notifications
You must be signed in to change notification settings - Fork 6
How to export to PDF, SVG & PNG
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.
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.
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.
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)
.
PDDocument doc = canvas.paintPDF();
doc.save("filename.pdf"); // specifies the filename of the export
doc.close(); // close resources
Document doc = canvas.paintSVG();
SVGUtils.documentToXMLFile(doc, new File("filename.svg")); // specifies the filename of the export
Img img = new Img(canvas.asComponent().getSize());
img.paint(g -> canvas.asComponent().paintAll(g));
ImageSaver.saveImage(img.getRemoteBufferedImage(), "canvas_export.png");
It is also possible to export a Container (JPanel, JFrame, ...) and its subcomponents (e.g. multiple canvases).
This can be done as shown below:
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();
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
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.