Example usage for com.itextpdf.text.pdf PdfContentByte sanityCheck

List of usage examples for com.itextpdf.text.pdf PdfContentByte sanityCheck

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte sanityCheck.

Prototype

public void sanityCheck() 

Source Link

Document

Checks for any dangling state: Mismatched save/restore state, begin/end text, begin/end layer, or begin/end marked content sequence.

Usage

From source file:figtree.application.FigTreePDF.java

License:Open Source License

static public void createGraphic(int width, int height, String treeFileName, String graphicFileName) {

    try {/*from  www  . j av  a2  s.  c  o m*/
        BufferedReader bufferedReader = new BufferedReader(new FileReader(treeFileName));
        String line = bufferedReader.readLine();
        while (line != null && line.length() == 0) {
            line = bufferedReader.readLine();
        }

        bufferedReader.close();

        boolean isNexus = (line != null && line.toUpperCase().contains("#NEXUS"));

        Reader reader = new FileReader(treeFileName);

        Map<String, Object> settings = new HashMap<String, Object>();

        ExtendedTreeViewer treeViewer = new ExtendedTreeViewer();
        ControlPalette controlPalette = new BasicControlPalette(200,
                BasicControlPalette.DisplayMode.ONLY_ONE_OPEN);
        FigTreePanel figTreePanel = new FigTreePanel(null, treeViewer, controlPalette);

        // First of all, fully populate the settings map so that
        // all the settings have defaults
        controlPalette.getSettings(settings);

        List<Tree> trees = new ArrayList<Tree>();

        if (isNexus) {
            FigTreeNexusImporter importer = new FigTreeNexusImporter(reader);
            trees.add(importer.importNextTree());

            // Try to find a figtree block and if found, parse the settings
            while (true) {
                try {
                    importer.findNextBlock();
                    if (importer.getNextBlockName().equalsIgnoreCase("FIGTREE")) {
                        importer.parseFigTreeBlock(settings);
                    }
                } catch (EOFException ex) {
                    break;
                }
            }
        } else {
            NewickImporter importer = new NewickImporter(reader, true);
            trees.add(importer.importNextTree());
        }

        if (trees.size() == 0) {
            throw new ImportException("This file contained no trees.");
        }

        treeViewer.setTrees(trees);

        controlPalette.setSettings(settings);

        treeViewer.getContentPane().setSize(width, height);

        OutputStream stream;
        if (graphicFileName != null) {
            stream = new FileOutputStream(graphicFileName);
        } else {
            stream = System.out;
        }

        Document document = new Document();
        document.setPageSize(new com.itextpdf.text.Rectangle(width, height));
        try {
            PdfWriter writer = PdfWriter.getInstance(document, stream);
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(width, height);
            Graphics2D g2 = tp.createGraphics(width, height);
            tp.setWidth(width);
            tp.setHeight(height);
            treeViewer.getContentPane().print(g2);
            g2.dispose();
            tp.sanityCheck(); // all the g2 content is written to tp, not cb
            cb.addTemplate(tp, 0, 0);
            cb.sanityCheck();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();

    } catch (ImportException ie) {
        throw new RuntimeException("Error writing graphic file: " + ie);
    } catch (IOException ioe) {
        throw new RuntimeException("Error writing graphic file: " + ioe);
    }

}