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:com.github.ukase.bulk.BulkRenderTask.java

License:Open Source License

private void appendPdf(Document document, PdfWriter writer, PdfContentByte cb, byte[] pdf) throws IOException {
    PdfReader reader = new PdfReader(pdf);
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        PdfImportedPage page = writer.getImportedPage(reader, i);
        document.newPage();//from  www.  j a va  2 s  .c o  m
        cb.addTemplate(page, 0, 0);
    }
}

From source file:com.ideationdesignservices.txtbook.pdf.TxtBookPdf.java

public void addConversationBackground(Document document, ColumnText ct, int column, float top,
        float messageWidth, float messageHeight, Boolean isMe)
        throws DocumentException, MalformedURLException, IOException {
    float[][] COLUMNS;
    if (messageWidth < MAX_COLUMN_CONTENT_WIDTH) {
        messageWidth += 8.0f;//from w w w. j a  v a  2s. co  m
    }
    messageHeight -= 35.0f;
    top -= BaseField.BORDER_WIDTH_THICK;
    if (COLUMNS_REG[0][0] == Txtbook.leftMargin(this.writer, document, this.settings.addFrontCover)) {
        COLUMNS = COLUMNS_REG;
    } else {
        COLUMNS = COLUMNS_ALT;
    }
    float left = COLUMNS[column][0];
    PdfContentByte under = this.writer.getDirectContentUnder();
    PdfTemplate bg = this.writer.getDirectContentUnder().createTemplate(
            (BUBBLE_L_WIDTH + messageWidth) + BUBBLE_R_WIDTH,
            (BUBBLE_T_HEIGHT + messageHeight) + BUBBLE_L_WIDTH);
    String num = isMe.booleanValue() ? "1" : "2";
    Image bgTL = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_tl.png"));
    bgTL.scaleAbsoluteWidth(BUBBLE_L_WIDTH);
    bgTL.scaleAbsoluteHeight(BUBBLE_T_HEIGHT);
    bgTL.setAbsolutePosition(0.0f, BUBBLE_L_WIDTH + messageHeight);
    bg.addImage(bgTL);
    Image bgT = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_t.png"));
    bgT.scaleAbsoluteWidth(messageWidth);
    bgT.scaleAbsoluteHeight(BUBBLE_T_HEIGHT);
    bgT.setAbsolutePosition(BUBBLE_L_WIDTH, BUBBLE_L_WIDTH + messageHeight);
    bg.addImage(bgT);
    Image bgTR = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_tr.png"));
    bgTR.scaleAbsoluteWidth(BUBBLE_R_WIDTH);
    bgTR.scaleAbsoluteHeight(BUBBLE_T_HEIGHT);
    bgTR.setAbsolutePosition(BUBBLE_L_WIDTH + messageWidth, BUBBLE_L_WIDTH + messageHeight);
    bg.addImage(bgTR);
    Image bgL = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_l.png"));
    bgL.scaleAbsoluteWidth(BUBBLE_L_WIDTH);
    bgL.scaleAbsoluteHeight(messageHeight);
    bgL.setAbsolutePosition(0.0f, BUBBLE_L_WIDTH);
    bg.addImage(bgL);
    bg.saveState();
    if (isMe.booleanValue()) {
        bg.setRGBColorFill(241, 241, 241);
    } else {
        bg.setRGBColorFill(208, 231, 196);
    }
    bg.rectangle(BUBBLE_L_WIDTH, BUBBLE_L_WIDTH, messageWidth, messageHeight);
    bg.fill();
    bg.restoreState();
    Image bgR = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_r.png"));
    bgR.scaleAbsoluteWidth(BUBBLE_R_WIDTH);
    bgR.scaleAbsoluteHeight(messageHeight);
    bgR.setAbsolutePosition(BUBBLE_L_WIDTH + messageWidth, BUBBLE_L_WIDTH);
    bg.addImage(bgR);
    Image bgBL = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_bl.png"));
    bgBL.scaleAbsoluteWidth(BUBBLE_L_WIDTH);
    bgBL.scaleAbsoluteHeight(BUBBLE_L_WIDTH);
    bgBL.setAbsolutePosition(0.0f, 0.0f);
    bg.addImage(bgBL);
    Image bgB = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_b.png"));
    bgB.scaleAbsoluteWidth(messageWidth);
    bgB.scaleAbsoluteHeight(BUBBLE_L_WIDTH);
    bgB.setAbsolutePosition(BUBBLE_L_WIDTH, 0.0f);
    bg.addImage(bgB);
    Image bgBR = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/bubble_" + num + "_br.png"));
    bgBR.scaleAbsoluteWidth(BUBBLE_R_WIDTH);
    bgBR.scaleAbsoluteHeight(BUBBLE_L_WIDTH);
    bgBR.setAbsolutePosition(BUBBLE_L_WIDTH + messageWidth, 0.0f);
    bg.addImage(bgBR);
    if (!isMe.booleanValue()) {
        bg.setMatrix(BUBBLE_TEXT_INDENT_MAIN, 0.0f, 0.0f, BaseField.BORDER_WIDTH_THIN, 0.0f, 0.0f);
        left += COLUMN_WIDTH;
    }
    under.addTemplate(bg, left, (top - messageHeight) - 20.0f);
}

From source file:com.kohmiho.mpsr.export.PDFGenerator.java

protected void addPageByTemplate(PdfWriter writer, PdfImportedPage page) {
    PdfContentByte directContentUnder = writer.getDirectContentUnder();
    // directContentUnder.addTemplate(page, 0.9f, 0, 0, 0.9f, 0, 0);
    directContentUnder.addTemplate(page, 0, 0);
}

From source file:com.miraflorescarwash.controller.PdfController.java

private void crearPdfCombosReporte(Document doc, List<ComboReporte> combos, PdfWriter writer) {
    Paragraph parrafo;//from ww w  .  ja va  2s  .  c  om
    PdfPTable tabla;
    String txt;
    PdfPCell cell;
    Phrase frase;
    String fuente;
    int i;
    JFreeChart chart;
    int w, h;
    int pos;
    pos = 0;
    w = h = 500;

    fuente = "arial";
    if (combos.isEmpty()) {
        return;
    }
    try {
        //

        // Se abre el documento.
        doc.open();

        txt = "Miraflores Car Wash";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);

        txt = "Reporte de Combos";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_CENTER);

        doc.add(parrafo);
        LineSeparator ls = new LineSeparator();
        doc.add(new Chunk(ls));

        tabla = new PdfPTable(3);

        frase = new Phrase("Id", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE));
        cell = new PdfPCell(frase);
        cell.setBackgroundColor(BaseColor.RED);
        tabla.addCell(cell);

        frase = new Phrase("Nombre", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE));
        cell = new PdfPCell(frase);
        cell.setBackgroundColor(BaseColor.RED);
        tabla.addCell(cell);

        frase = new Phrase("Dinero Recaudado", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE));
        cell = new PdfPCell(frase);
        cell.setBackgroundColor(BaseColor.RED);
        tabla.addCell(cell);

        i = 1;

        for (ComboReporte combo : combos) {

            if (i % 2 == 0) {
                cell = new PdfPCell();
                cell.setBackgroundColor(new BaseColor(244, 119, 119));

                frase = new Phrase(combo.getId() + "");
                cell.setPhrase(frase);
                tabla.addCell(cell);

                frase = new Phrase(combo.getNombre());
                cell.setPhrase(frase);
                tabla.addCell(cell);

                frase = new Phrase("S/. " + combo.getCantidad() + "0");
                cell.setPhrase(frase);
                tabla.addCell(cell);

            } else {
                tabla.addCell(combo.getId() + "");
                tabla.addCell(combo.getNombre());
                tabla.addCell("S/. " + combo.getCantidad() + "0");
            }
            i++;
        }
        doc.add(tabla);
        doc.newPage();
        txt = "Miraflores Car Wash";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);

        chart = comboService.generarChartReporte(combos);

        w = 500;
        h = 500;

        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h);
        chart.draw(g2d, r2d);
        cb.addTemplate(tp, 50, 250);
        g2d.dispose();
        doc.close();
    } catch (DocumentException ex) {

    }
}

From source file:com.miraflorescarwash.controller.PdfController.java

private void crearPdfVentasReporte(Document doc, JFreeChart chart, PdfWriter writer, String fechaInicio,
        String fechaFin) {/*from w  w  w  . java 2 s.co  m*/
    Paragraph parrafo;
    Phrase frase;
    String txt;
    String fuente;
    int w, h;

    fuente = "arial";
    if (chart == null) {
        return;
    }
    try {
        //

        // Se abre el documento.
        doc.open();

        txt = "Miraflores Car Wash";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);

        txt = "Reporte de Ventas";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_CENTER);

        doc.add(parrafo);
        LineSeparator ls = new LineSeparator();
        doc.add(new Chunk(ls));

        doc.add(Chunk.NEWLINE);

        txt = "Inicio: ";
        frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK));
        doc.add(frase);
        txt = fechaInicio;
        frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK));
        doc.add(frase);
        doc.add(Chunk.NEWLINE);

        txt = "Fin: ";
        frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK));
        doc.add(frase);
        txt = fechaFin;
        frase = new Phrase(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK));
        doc.add(frase);

        w = 500;
        h = 500;

        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h);
        chart.draw(g2d, r2d);
        cb.addTemplate(tp, 50, 50);
        g2d.dispose();
        doc.close();
    } catch (DocumentException ex) {

    }
}

From source file:com.miraflorescarwash.controller.PdfController.java

private void crearPdfClientesFreq(Document doc, List<ClienteReporte> reporteCompras, PdfWriter writer) {
    Paragraph parrafo;//from www. j  a  v a2s  .  c om
    PdfPTable tabla;
    String txt;
    PdfPCell cell;
    Phrase frase;
    String fuente;
    JFreeChart chart;
    int i, w, h;

    fuente = "arial";
    if (reporteCompras.isEmpty()) {
        return;
    }
    try {
        //

        // Se abre el documento.
        doc.open();

        txt = "Miraflores Car Wash";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);

        txt = "Clientes Frecuentes";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_CENTER);

        doc.add(parrafo);
        LineSeparator ls = new LineSeparator();
        doc.add(new Chunk(ls));

        doc.add(Chunk.NEWLINE);

        tabla = new PdfPTable(3);

        frase = new Phrase("Id", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE));
        cell = new PdfPCell(frase);
        cell.setBackgroundColor(BaseColor.RED);
        tabla.addCell(cell);

        frase = new Phrase("Nombre", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE));
        cell = new PdfPCell(frase);
        cell.setBackgroundColor(BaseColor.RED);
        tabla.addCell(cell);

        frase = new Phrase("Dinero", FontFactory.getFont(fuente, 12, Font.BOLD, BaseColor.WHITE));
        cell = new PdfPCell(frase);
        cell.setBackgroundColor(BaseColor.RED);
        tabla.addCell(cell);

        i = 1;

        for (ClienteReporte clienteReporte : reporteCompras) {

            if (i % 2 == 0) {
                cell = new PdfPCell();
                cell.setBackgroundColor(new BaseColor(244, 119, 119));

                frase = new Phrase(clienteReporte.getId() + "");
                cell.setPhrase(frase);
                tabla.addCell(cell);

                frase = new Phrase(clienteReporte.getApellido() + " " + clienteReporte.getNombre());
                cell.setPhrase(frase);
                tabla.addCell(cell);

                frase = new Phrase("S/. " + clienteReporte.getTotal() + "0");
                cell.setPhrase(frase);
                tabla.addCell(cell);

            } else {
                tabla.addCell(clienteReporte.getId() + "");
                tabla.addCell(clienteReporte.getApellido() + " " + clienteReporte.getNombre());
                tabla.addCell("S/. " + clienteReporte.getTotal() + "0");
            }
            i++;
        }
        doc.add(tabla);

        doc.newPage();
        txt = "Miraflores Car Wash";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);

        chart = clienteService.generarChartReporte(reporteCompras);

        w = 500;
        h = 500;

        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h);
        chart.draw(g2d, r2d);
        cb.addTemplate(tp, 50, 250);
        g2d.dispose();
        doc.close();
    } catch (DocumentException ex) {

    }
}

From source file:com.miraflorescarwash.controller.PdfController.java

private void crearPdfClientesEvolucion(Document doc, List<ClienteReporte> reporteCompras, PdfWriter writer) {
    Paragraph parrafo;// w  w  w .  j  av  a  2s .com
    PdfPTable tabla;
    String txt;
    PdfPCell cell;
    Phrase frase;
    String fuente;
    JFreeChart chart;
    ClienteReporte cliente;
    int i, w, h;

    fuente = "arial";
    if (reporteCompras.isEmpty()) {
        return;
    }
    try {
        //
        cliente = reporteCompras.get(0);
        // Se abre el documento.
        doc.open();

        txt = "Miraflores Car Wash";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 10, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);

        txt = "Evolucin de Clientes ";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 30, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_CENTER);

        doc.add(parrafo);
        LineSeparator ls = new LineSeparator();
        doc.add(new Chunk(ls));

        txt = "Id: ";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);
        txt = cliente.getId() + "";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);
        //            doc.add(Chunk.NEWLINE);

        txt = "Nombres: ";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);
        txt = cliente.getNombre();
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);
        //            doc.add(Chunk.NEWLINE);

        txt = "Apellidos: ";
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.BOLD, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);
        txt = cliente.getApellido();
        parrafo = new Paragraph(txt, FontFactory.getFont(fuente, 14, Font.NORMAL, BaseColor.BLACK));
        parrafo.setAlignment(Element.ALIGN_LEFT);
        doc.add(parrafo);
        //            doc.add(Chunk.NEWLINE);

        chart = clienteService.generarChartReporteMes(reporteCompras);

        w = 500;
        h = 500;

        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(w, h);
        Graphics2D g2d = tp.createGraphics(w, h, new DefaultFontMapper());
        Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h);
        chart.draw(g2d, r2d);
        cb.addTemplate(tp, 50, 50);
        g2d.dispose();
        doc.close();
    } catch (DocumentException ex) {

    }
}

From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java

License:Apache License

/**
 * @param titleSection//from   w w w. j  a v  a  2s  .c  o  m
 * @param writer
 * @param docu
 * @throws PhrescoException 
 */
public static void addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu)
        throws PhrescoException {
    if (isDebugEnabled) {
        S_LOGGER.debug(
                "Entering Method DocumentUtil.addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu)");
    }
    try {
        PdfReader reader = new PdfReader(titleSection);
        reader.consolidateNamedDestinations();
        PdfContentByte cb = writer.getDirectContent();

        int pages = reader.getNumberOfPages();
        for (int i = 1; i <= pages; i++) {
            PdfImportedPage importedPage = writer.getImportedPage(reader, i);
            cb.addTemplate(importedPage, 0, 0);
            docu.newPage();
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new PhrescoException(e);
    }
}

From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java

License:Apache License

/**
 * @param titleSection/*  ww  w  . jav a 2s.c  o m*/
 * @param writer
 * @param docu
 * @throws IOException
 * @throws DocumentException
 */
public static void addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu)
        throws IOException, DocumentException {
    if (isDebugEnabled) {
        S_LOGGER.debug(
                "Entering Method DocumentUtil.addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu)");
    }
    PdfReader reader = new PdfReader(titleSection);
    reader.consolidateNamedDestinations();
    PdfContentByte cb = writer.getDirectContent();

    int pages = reader.getNumberOfPages();
    for (int i = 1; i <= pages; i++) {
        PdfImportedPage importedPage = writer.getImportedPage(reader, i);
        cb.addTemplate(importedPage, 0, 0);
        docu.newPage();
    }
}

From source file:com.tommontom.pdfsplitter.PdfMerge.java

public static void doMerge(java.util.List<InputStream> list, String[] imageList, String[] listWordExcels,
        OutputStream outputStream) throws DocumentException, IOException {
    Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    writer.setFullCompression();/*from  w ww  .j  a v a  2 s .  c  o  m*/
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    Image img;
    for (InputStream in : list) {
        PdfReader reader = new PdfReader(in);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {

            document.newPage();
            //import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);
            //add the page to the destination pdf
            cb.addTemplate(page, 0, 0);
        }

    }
    for (int i = 0; i < imageList.length; i++) {
        document.newPage();
        if (imageList[i] != null) {
            img = Image.getInstance(String.format("%s", imageList[i]));
            Rectangle one = new Rectangle(img.getPlainWidth(), img.getPlainHeight());
            document.setPageSize(one);
            if (img.getScaledWidth() > img.getScaledHeight()) {
                img.rotate();
            }
            if (img.getScaledWidth() > 792 || img.getScaledHeight() > 792) {
                img.scaleToFit(792, 792);

            }
            img.setDpi(150, 150);
            document.add(img);
        }
    }
    for (int i = 0; i < listWordExcels.length; i++) {
        if (imageList[i] != null) {
            File input = new File(listWordExcels[i]);
            File output = new File(listWordExcels[i] + ".pdf");
            String outputS = listWordExcels[i] + ".pdf";
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
            connection.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(input, output);
            PdfReader readerWord = new PdfReader(outputS);
            PdfImportedPage page = writer.getImportedPage(readerWord, readerWord.getNumberOfPages());
            cb.addTemplate(page, 0, 0);
        }
    }

    outputStream.flush();
    document.close();
    outputStream.close();
}