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

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

Introduction

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

Prototype

public void addTemplate(final PdfTemplate template, final double x, final double y) 

Source Link

Document

Adds a template to this content.

Usage

From source file:userinterface.graph.Graph.java

License:Open Source License

/**
 * Exports the given chart to a pdf format file
 * @param file the file that has to be saved in pdf format
 * @param chart the chart that needs to be exported into pdf
 *//*from www .  j ava 2 s  . c om*/

public static void exportToPDF(File file, JFreeChart chart) {

    PdfWriter out = null;
    com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4.rotate());

    int width = 800, height = 500;

    try {

        out = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        PdfContentByte contentByte = out.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        @SuppressWarnings("deprecation")
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        chart.draw(graphics2d, rectangle2d);

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

    } catch (Exception e) {

        JOptionPane.showMessageDialog(GUIPrism.getGUI(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return;
    }

    document.close();

}

From source file:userinterface.graph.Graph3D.java

License:Open Source License

/**
 * Exports the 3d graph as a pdf/*from   w  w w  . j  a v a  2 s  . c o  m*/
 * 
 * @param file The pdf file to which the data should be written
 * @param panel The chart panel that has to be exported
 */
public static void exportToPDF(File file, Chart3DPanel panel) {

    PdfWriter out = null;
    Document document = new com.itextpdf.text.Document(PageSize.A4.rotate());

    int width = 800, height = 500;

    try {

        out = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        PdfContentByte contentByte = out.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        @SuppressWarnings("deprecation")
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);

        panel.getChart().draw(graphics2d, rectangle2d);

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

    } catch (Exception e) {
        // in case any error occurs tell the user what the error is (sometimes useful if there is a problem of writing rights)
        JOptionPane.showMessageDialog(GUIPrism.getGUI(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return;
    }

    document.close();
}

From source file:valstreamtools.ValStrSplitPage.java

private void splitDocument(String[] args) throws IOException, DocumentException {
    /**//from   w ww.  j a  v a  2  s.c  om
     * int splitNo = 4; String inputFileName =
     * "C:\\tmp\\valuestream_export_input_2015-03-03-22-18-37.pdf"; String
     * outputFileName = "C:\\tmp\\valuestream_export_op_2015-03-03-22-18-37.pdf";*
     */
    int splitNo = Integer.parseInt(args[0]);
    String inputFileName = args[1];
    if (splitNo > 1) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String splitOutFileName = "/var/tmp/PDF_SPLIT_" + sdf.format(new Date()) + ".pdf";
        PdfReader reader = new PdfReader(inputFileName);
        Rectangle pagesize = reader.getPageSize(1);
        float pageHeight = pagesize.getHeight();
        float newpagewidth = (pagesize.getWidth() / splitNo);
        Rectangle newPapeSize = new Rectangle(0, 0, newpagewidth, pageHeight);
        Document document = new Document(newPapeSize);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(splitOutFileName));
        document.open();
        int pageNos = reader.getNumberOfPages();
        PdfContentByte content = writer.getDirectContent();
        for (int i = 1; i <= pageNos; i++) {
            PdfImportedPage page = writer.getImportedPage(reader, i);
            if (i > 1) {// In case of a new inpit page, need to create this.
                document.newPage();
            }
            for (int j = 0; j < splitNo; j++) {
                if (j == 0) {//This condition is used to skip the adding of new page.
                    content.addTemplate(page, 0, 0);
                } else {
                    document.newPage();
                    content.addTemplate(page, (-1 * j * newpagewidth), 0);
                }
            }
        }
        document.close();
        reader.close();
        createPageNo(splitOutFileName, args[2], pageHeight);
        File f = new File(splitOutFileName);
        if (f.exists()) {
            f.delete();
        }
    } else {
        PdfReader reader = new PdfReader(inputFileName);
        float pageHeight = reader.getPageSize(1).getHeight();
        reader.close();
        createPageNo(inputFileName, args[2], pageHeight);
    }
}

From source file:WeeklyReport.WeeklyPDF.java

public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
    PdfWriter writer = null;//from   w w w  . j av  a  2  s  . com
    Document document = new Document();
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        document.add(new Paragraph("Here is the recommendation"));
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(5, -100, width, height);
        chart.draw(graphics2d, rectangle2d);
        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);
    } catch (FileNotFoundException | DocumentException ex) {
        ex.getMessage();
    }
    document.close();
}