2012-05-15 2 views
0

iText 라이브러리를 사용하여 JUNG 네트워크를 PDF로 출력하려고합니다.iText를 사용하여 JUNG 시각화를 PDF로 출력

많은 솔루션은 화면을 캡처하고이를 EPS와 같은 형식으로 출력하기 위해 라이브러리를 사용하기 전에 프레임에 그래프를 표시합니다. 그러나, 내 응용 프로그램은 자동으로 많은 네트워크를 구축하고 파일에 저장하기 전에 화면에 각 네트워크를 표시하는 출력 그래서 좋은 해결책은 아닙니다.

일부 검색을 수행했는데 정확히 무엇을하고 있는지 알지 못했습니다. 그래프를 PNG로 출력하는 것이 간단하지만 (PNG의 품질은 내 요구에 맞지 않습니다. here 참조)

현재 코드는 JFreeChart에서 PDF로 차트를 작성한 것입니다 (here 참조). Graph2D 객체를 얻으려고하고 있는데, 그래프를 그린 다음 PDF로 출력 할 수 있습니다. 이 코드는 visualise.getGraphics()에서 반환 된 그래픽 객체가 null이므로 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(); 
} 

답변

0

답변은 Joshua가 제공 한 것입니다. 나는 헤드리스를 검색 한 적이 없을 것 같아요!

코드를 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(); 

}