Search
Exporting the Diagram

Exporting Images

You can create a static image representing the current diagram using the createImage method. The method returns a java.awt.image.BufferedImage object. The BufferedImage can be further serialized as a Jpeg, Gif or Png file, as shown in the following example:

Java  Copy Code

try
{
    java.awt.image.BufferedImage image = diagram.createImage();
    javax.imageio.ImageIO.write(image, "png", new File("d:\\export.png"));
}
catch (IOException ex)
{
}

Exporting Pdf files

In order to export to PDF, import the com.mindfusion.diagramming.export and com.mindfusion.pdf packages. Create a PdfExporter instance and call its export method. The margins setter methods lets you specify the page margins for all pages in the document. If you need to set the page size and orientation, call the setPageSize, and setPageOrientation methods. The setAutoScale method allows scaling the diagram to fit in one or more PDF pages. The following example shows how to export a diagram to a Pdf file in response to pressing a button event and using the JFileChooser control where the location is selected and the name of the Pdf file is entered:

Java  Copy Code

btnExportPdf.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {    
        JFileChooser saveFileDialog = new JFileChooser();
        saveFileDialog.setCurrentDirectory (new File ("."));
        saveFileDialog.setFileFilter(
            new javax.swing.filechooser.FileFilter()
            {
                public boolean accept(File f)
                {
                    return f.getName().toLowerCase().endsWith(".pdf")
                        || f.isDirectory();
                }

                public String getDescription()
                {
                    return "PDF files|*.pdf";
                }
            });
    
        if (saveFileDialog.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION)
        {
            PdfExporter pdfExp = new PdfExporter();
            pdfExp.export(diagramView.getDiagram(), saveFileDialog.getSelectedFile().getAbsolutePath());
        }
    }
});

Exporting Svg files

Create an SvgExporter instance and call its export method, passing a Diagram object and the full path to the .svg file as parameters.

Export Excel files

The ExcelExporter class from com.mindfusion.diagramming.export package exports diagrams to Excel Open XML format (XSLX). Diagrams are exported as Excel AutoShapes drawings. The export overload that takes a DiagramDocument parameter creates a sheet for each DiagramPage in the document. The export(Diagram) overload creates a single Excel sheet.