forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfExport.java
More file actions
executable file
·75 lines (66 loc) · 1.87 KB
/
PdfExport.java
File metadata and controls
executable file
·75 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.mxgraph.examples;
import java.awt.Graphics2D;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.mxgraph.canvas.mxGraphics2DCanvas;
import com.mxgraph.canvas.mxICanvas;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxCellRenderer;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxCellRenderer.CanvasFactory;
import com.mxgraph.view.mxGraph;
// This example requires iText from http://www.lowagie.com/iText/
public class PdfExport
{
public PdfExport() throws Exception
{
// Creates graph with model
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
30);
mxCell v2 = (mxCell) graph.insertVertex(parent, null, "World!",
240, 150, 80, 30);
graph.insertEdge(parent, null, "e1", v1, v2);
}
finally
{
graph.getModel().endUpdate();
}
mxRectangle bounds = graph.getGraphBounds();
Document document = new Document(new Rectangle((float) (bounds
.getWidth()), (float) (bounds.getHeight())));
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("example.pdf"));
document.open();
final PdfContentByte cb = writer.getDirectContent();
mxGraphics2DCanvas canvas = (mxGraphics2DCanvas) mxCellRenderer
.drawCells(graph, null, 1, null, new CanvasFactory()
{
public mxICanvas createCanvas(int width, int height)
{
Graphics2D g2 = cb.createGraphics(width, height);
return new mxGraphics2DCanvas(g2);
}
});
canvas.getGraphics().dispose();
document.close();
}
public static void main(String[] args)
{
try
{
new PdfExport();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}