Example usage for com.lowagie.text.pdf DefaultFontMapper DefaultFontMapper

List of usage examples for com.lowagie.text.pdf DefaultFontMapper DefaultFontMapper

Introduction

In this page you can find the example usage for com.lowagie.text.pdf DefaultFontMapper DefaultFontMapper.

Prototype

DefaultFontMapper

Source Link

Usage

From source file:tracer.application.TracerFrame.java

License:Open Source License

/**
 * export full statistic summary of selected traceList (log) to a tab-delimited txt file
 *///ww w  .j  a  v a 2 s  .  c om
//    public final void doExportStatisticSummary() {
//        final JFrame frame = this;
//
//        FileDialog dialog = new FileDialog(frame, "Export Statistic Summary...", FileDialog.SAVE);
//
//        dialog.setVisible(true);
//        if (dialog.getFile() != null) {
//            File file = new File(dialog.getDirectory(), dialog.getFile());
//
//            // todo use LongTask
//            final String statSummTxt = TraceAnalysis.getStatisticSummary(currentTraceLists);
//
//            try {
//
//                FileWriter writer = new FileWriter(file);
//                writer.write(statSummTxt);
//                writer.close();
//
//            } catch (IOException ioe) {
//                JOptionPane.showMessageDialog(this, "Unable to write file: " + ioe,
//                        "Unable to write file",
//                        JOptionPane.ERROR_MESSAGE);
//            }
//        }
//    }

public final void doExportPDF() {
    FileDialog dialog = new FileDialog(this, "Export PDF Image...", FileDialog.SAVE);

    dialog.setVisible(true);
    if (dialog.getFile() != null) {
        File file = new File(dialog.getDirectory(), dialog.getFile());

        Rectangle2D bounds = tracePanel.getExportableComponent().getBounds();
        Document document = new Document(
                new com.lowagie.text.Rectangle((float) bounds.getWidth(), (float) bounds.getHeight()));
        try {
            // step 2
            PdfWriter writer;
            writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            // step 3
            document.open();
            // step 4
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate((float) bounds.getWidth(), (float) bounds.getHeight());
            Graphics2D g2d = tp.createGraphics((float) bounds.getWidth(), (float) bounds.getHeight(),
                    new DefaultFontMapper());
            tracePanel.getExportableComponent().print(g2d);
            g2d.dispose();
            cb.addTemplate(tp, 0, 0);
        } catch (DocumentException de) {
            JOptionPane.showMessageDialog(this, "Error writing PDF file: " + de, "Export PDF Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(this, "Error writing PDF file: " + e, "Export PDF Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        document.close();
    }
}