使用iText将JUNG可视化输出为PDF

时间:2012-05-15 16:07:29

标签: networking pdf visualization itext jung

我正在尝试使用iText库将JUNG网络输出到PDF。

许多解决方案似乎在使用库捕获屏幕并将其输出为某些格式(如EPS)之前在帧中显示图形。但是,我的应用程序将自动构建许多网络并输出它们,以便在保存到文件之前在屏幕上显示每个网络不是一个很好的解决方案。

我做了一些搜索,但没有看到我正在做的事情。将图形输出到PNG似乎很简单(参见here),但PNG的质量不适合我的需要。

我目前的代码如下,基于将JFreeChart的图表编写为PDF(参见here)。我正在尝试获取一个Graphics2D对象,我可以“绘制”图形,然后输出到PDF。此代码生成空指针异常,因为visualise.getGraphics()返回的图形对象为null。

任何建议,代码段或在线示例都将不胜感激。

public void write() throws IOException, DocumentException {

    // Open the PDF file for writing
    this.document = new Document();
    this.writer = PdfWriter.getInstance(this.document, new FileOutputStream(this.fileName));
    document.open();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(WIDTH, HEIGHT);
    //Graphics2D graphics2d = template.createGraphics(WIDTH, HEIGHT, new DefaultFontMapper());

    // Apply a layout to the graph
    Layout<Vertex, Edge> layout = new CircleLayout<Vertex, Edge>(this.network);
    layout.setSize(new Dimension(WIDTH, HEIGHT));

    // Draw on the graphics 2D object
    VisualizationImageServer<Vertex, Edge> visualise = new VisualizationImageServer<Vertex, Edge>(layout, new Dimension(WIDTH, HEIGHT));
    visualise.setPreferredSize(new Dimension(WIDTH + 50, HEIGHT + 50));
    visualise.setDoubleBuffered(false);
    Graphics2D graphics2d = (Graphics2D) visualise.getGraphics();
    visualise.paintComponents(graphics2d);

    graphics2d.dispose();
    contentByte.addTemplate(template, 0, 0);

    this.document.close();
}

1 个答案:

答案 0 :(得分:0)

约书亚提供的答案是正确的。我想我永远都不会搜索无头!

我必须略微修改http://sourceforge.net/projects/jung/forums/forum/252062/topic/1407188处的代码,因为我发现从BufferedImage创建Graphics2D对象不合适。相反,Graphics2D应该由PDFTemplate制作。为了将来的参考,我发布了我的工作代码。

public void write() throws IOException, DocumentException {

    // Open the PDF file for writing - and create a Graphics2D object
    this.document = new Document();
    this.writer = PdfWriter.getInstance(this.document, new FileOutputStream(this.fileName));
    document.open();
    PdfContentByte contentByte = writer.getDirectContent();
    PdfTemplate template = contentByte.createTemplate(WIDTH, HEIGHT);
    Graphics2D graphics2d = template.createGraphics(WIDTH, HEIGHT, new DefaultFontMapper());

    // Apply a layout to the graph
    Layout<Vertex, Edge> layout = new CircleLayout<Vertex, Edge>(this.network);
    layout.setSize(new Dimension(WIDTH / 2, HEIGHT / 2));

    // Create a visualisation object - set background color etc
    VisualizationViewer<Vertex, Edge> visualise = new VisualizationViewer<Vertex, Edge>(layout, new Dimension(WIDTH, HEIGHT));
    visualise.setSize(WIDTH, HEIGHT);
    visualise.setBackground(Color.WHITE);

    // Create a container to hold the visualisation
    Container container = new Container();
    container.addNotify();
    container.add(visualise);
    container.setVisible(true);
    container.paintComponents(graphics2d);

    // Dispose of the graphics and close the document
    graphics2d.dispose();
    contentByte.addTemplate(template, 0, 0);
    this.document.close();

}