Example usage for com.itextpdf.text ImgTemplate ImgTemplate

List of usage examples for com.itextpdf.text ImgTemplate ImgTemplate

Introduction

In this page you can find the example usage for com.itextpdf.text ImgTemplate ImgTemplate.

Prototype

public ImgTemplate(PdfTemplate template) throws BadElementException 

Source Link

Document

Creates an Image from a PdfTemplate.

Usage

From source file:com.centurylink.mdw.pdf.PdfExportHelper.java

License:Apache License

private void printGraph(DocWriter writer, ProcessCanvas canvas, Process process, Rectangle pageSize,
        Chapter chapter) throws Exception {
    Dimension graphsize = getGraphSize(process);
    // we create a template and a Graphics2D object that corresponds with it
    int w;/*  w  ww.  j a  va 2s  . c o  m*/
    int h;
    float scale;
    if ((float) graphsize.width < pageSize.getWidth() * 0.8
            && (float) graphsize.height < pageSize.getHeight() * 0.8) {
        w = graphsize.width + 36;
        h = graphsize.height + 36;
        scale = -1f;
    } else {
        scale = pageSize.getWidth() * 0.8f / (float) graphsize.width;
        if (scale > pageSize.getHeight() * 0.8f / (float) graphsize.height)
            scale = pageSize.getHeight() * 0.8f / (float) graphsize.height;
        w = (int) (graphsize.width * scale) + 36;
        h = (int) (graphsize.height * scale) + 36;
    }
    Image img;
    canvas.setBackground(Color.white);
    PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
    PdfTemplate tp = cb.createTemplate(w, h);
    Graphics2D g2 = tp.createGraphics(w, h);
    if (scale > 0)
        g2.scale(scale, scale);
    tp.setWidth(w);
    tp.setHeight(h);
    canvas.paintComponent(g2);
    g2.dispose();
    img = new ImgTemplate(tp);
    chapter.add(img);
}

From source file:org.fhaes.neofhchart.PDFExportOptionsDialog.java

License:Open Source License

/**
 * Performs the export operation using the currentChart as the source.
 * /*from   w  w  w.j a  v a 2  s. co  m*/
 * @return true if the operation completed successfully, false otherwise
 */
private boolean doExportToPDF() {

    boolean completedSuccessfully = false;

    if (currentChart != null) {
        log.debug("Exporting to PDF...");
        Document document = null;

        if (cboPaperSize.getSelectedItem() instanceof Rectangle) {
            Rectangle rect = (Rectangle) cboPaperSize.getSelectedItem();

            if (radLandscape.isSelected()) {
                rect = rect.rotate();
            }

            document = new Document(rect, 10, 10, 10, 10);
        } else {
            Rectangle rect = new Rectangle(currentChart.getTotalWidth(), currentChart.getTotalHeight());
            document = new Document(rect, 10, 10, 10, 10);
        }

        try {
            currentChart.setVisibilityOfNoExportElements(false);

            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream(outputFile.getAbsolutePath()));
            document.open();

            int width = (int) document.getPageSize().getWidth();
            int height = (int) document.getPageSize().getHeight();

            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate template = cb.createTemplate(width, height);

            @SuppressWarnings("deprecation")
            Graphics2D g2 = template.createGraphics(width, height);

            PrintTranscoder prm = new PrintTranscoder();
            TranscoderInput ti = new TranscoderInput(currentChart.getSVGDocument());
            prm.transcode(ti, null);

            PageFormat pg = new PageFormat();
            Paper pp = new Paper();
            pp.setSize(width, height);
            pp.setImageableArea(0, 0, width, height);
            pg.setPaper(pp);
            prm.print(g2, pg, 0);
            g2.dispose();

            ImgTemplate img = new ImgTemplate(template);
            document.add(img);

            completedSuccessfully = true;
        } catch (DocumentException e) {
            System.err.println(e);
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            currentChart.setVisibilityOfNoExportElements(true);
        }

        document.close();
    }

    return completedSuccessfully;
}