Example usage for com.itextpdf.text.pdf PdfTemplate addImage

List of usage examples for com.itextpdf.text.pdf PdfTemplate addImage

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfTemplate addImage.

Prototype

public void addImage(final Image image) throws DocumentException 

Source Link

Document

Adds an Image to the page.

Usage

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

public Boolean createFrontCoverPage(Document document, String coverTitle, Bitmap photo)
        throws DocumentException, MalformedURLException, IOException {
    if (photo != null) {
        int imageMaxWidth;
        int imageMaxHeight;
        int imagePosX;
        int imagePosY;
        if (photo.getWidth() < photo.getHeight()) {
            imageMaxWidth = 900;/* w ww. java  2 s.c  om*/
            imageMaxHeight = 1200;
            imagePosX = 198;
            imagePosY = 379;
        } else {
            imageMaxWidth = 1200;
            imageMaxHeight = 900;
            imagePosX = 162;
            imagePosY = 379;
        }
        OutputStream stream = new ByteArrayOutputStream();
        ImageUtilities.scaleCenterCrop(photo, imageMaxWidth, imageMaxHeight).compress(CompressFormat.JPEG, 50,
                stream);
        Image coverImage = Image.getInstance(stream.toByteArray());
        coverImage.setAbsolutePosition(0.0f, 0.0f);
        PdfTemplate t = this.writer.getDirectContent().createTemplate((float) imageMaxWidth,
                (float) imageMaxHeight);
        t.newPath();
        t.moveTo(0.0f, (float) imageMaxHeight);
        t.lineTo(0.0f, 71.0f);
        t.lineTo(17.0f, 71.0f);
        t.lineTo(17.0f, 0.0f);
        t.lineTo(72.0f, 71.0f);
        t.lineTo((float) imageMaxWidth, 71.0f);
        t.lineTo((float) imageMaxWidth, (float) imageMaxHeight);
        t.lineTo(0.0f, (float) imageMaxHeight);
        t.closePath();
        t.clip();
        t.newPath();
        t.addImage(coverImage);
        t.setColorStroke(new BaseColor(0, 0, 0));
        t.setLineWidth(BUBBLE_TEXT_INDENT_ALTERNATE);
        t.newPath();
        t.moveTo(0.0f, (float) imageMaxHeight);
        t.lineTo(0.0f, 71.0f);
        t.lineTo(17.0f, 71.0f);
        t.lineTo(17.0f, 0.0f);
        t.lineTo(72.0f, 71.0f);
        t.lineTo((float) imageMaxWidth, 71.0f);
        t.lineTo((float) imageMaxWidth, (float) imageMaxHeight);
        t.lineTo(0.0f, (float) imageMaxHeight);
        t.closePathStroke();
        Image clipped = Image.getInstance(t);
        clipped.scalePercent(24.0f);
        clipped.setAbsolutePosition((float) imagePosX, (float) imagePosY);
        clipped.setCompressionLevel(this.settings.compressionLevel);
        clipped.setAlignment(5);
        document.add(clipped);
    }
    if (coverTitle != null && coverTitle.length() > 0) {
        PdfContentByte canvas = this.writer.getDirectContent();
        Paragraph coverTitleEl = new Paragraph(coverTitle, this.serifFont24);
        coverTitleEl.setAlignment(1);
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(311.0f);
        PdfPCell cell = new PdfPCell();
        cell.setBorder(0);
        cell.addElement(coverTitleEl);
        cell.setPadding(0.0f);
        cell.setIndent(0.0f);
        table.addCell(cell);
        table.completeRow();
        table.writeSelectedRows(0, -1, 147.0f, 390.0f, canvas);
    }
    return Boolean.valueOf(true);
}

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;/*  w ww  .  ja va2  s  .  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:Login.ventas.fproyectos.java

public void fondos(Document documento, PdfContentByte canvas) {
    try {/*  w w  w.  j a  va2 s  .c o m*/
        Image imghead = Image.getInstance(usuario.getDireccion() + "/plantilla.jpg");
        imghead.setAbsolutePosition(0, 0);
        imghead.setAlignment(Image.ALIGN_CENTER);
        float scaler = ((documento.getPageSize().getWidth() - documento.leftMargin() - documento.rightMargin())
                / imghead.getWidth()) * 100;
        imghead.scalePercent(scaler);
        PdfTemplate tp = canvas.createTemplate(PageSize.A4.getWidth(), PageSize.A4.getHeight()); //el rea destinada para el encabezado
        tp.addImage(imghead);
        x = (int) imghead.getWidth();
        y = (int) imghead.getHeight();
        canvas.addTemplate(tp, 0, 0);//posicin del tmplate derecha y abajo
    } catch (IOException | DocumentException io) {

    }
}

From source file:se.billes.pdf.renderer.model.Image.java

License:Open Source License

@Override
public void onRender(PdfContentByte cb) throws PdfRenderException {

    ImageInstance instance = null;/*  w  w w. ja v  a2s. c om*/
    try {
        instance = new ImageFactory().getImageByFile(cb, file);
    } catch (Exception e) {
        throw new PdfRenderException(e);
    }

    float pageHeight = getPage().getPdfDocument().getSize()[1];
    float pageWidth = getPage().getPdfDocument().getSize()[0];

    float x = 0;
    float y = 0;
    float fitHeight = 0f;
    float imageHeight = 0;
    float imageWidth = 0;
    boolean alignImage = false;

    float[] positions = new BlockFactory().getBoundsInPs(this);

    float width = positions[2];
    float height = positions[3];

    com.itextpdf.text.Image image = instance.getImage();

    int dpiX = image.getDpiX();
    if (dpiX == 0) {
        dpiX = 300;

    }
    if (!new ImageFactory().isPdf(file)) {
        dpiX = getDotsPerInch();
        alignImage = true; // always align jpg
    }

    float realPercent = 72f / dpiX * 100; // only jpg
    if (isScaleToFit()) {
        image.scaleToFit(width, height);
        imageHeight = image.getScaledHeight();
        imageWidth = image.getScaledWidth();
    } else if (isFitContentProportionally()) {
        image.scaleToFit(width, height);
        imageHeight = image.getScaledHeight();
        imageWidth = image.getScaledWidth();
        fitHeight = height - imageHeight;
        alignImage = true;
    } else {
        if (isFillFrameProportionally()) {
            float percentWidth = width / image.getWidth();
            float percentHeight = height / image.getHeight();
            realPercent = Math.max(percentHeight, percentWidth) * 100;
            alignImage = true;
            if (new ImageFactory().isPdf(file)) {
                image.scalePercent(realPercent);
                imageHeight = image.getScaledHeight();
                imageWidth = image.getScaledWidth();
                fitHeight = height - imageHeight;
            }
        }

        if (isCenterImageToPageWidth() && new ImageFactory().isPdf(file)) {

            imageWidth = image.getScaledWidth();
            float middle = (pageWidth / 2) - (imageWidth / 2);
            positions[0] = SizeFactory.PostscriptPointsToMillimeters(middle);
        }

        if (!new ImageFactory().isPdf(file)) {
            image.scalePercent(realPercent);
            imageHeight = image.getScaledHeight();
            imageWidth = image.getScaledWidth();
            fitHeight = height - imageHeight;
        }
    }

    if (alignImage) {
        float[] result = handleAlignment(width, imageWidth, height, imageHeight, fitHeight);
        x = result[0];
        y = result[1];
    }
    try {
        PdfTemplate tp = cb.createTemplate(width, height);
        image.setAbsolutePosition(x, y);
        tp.roundRectangle(0, 0, width, height, SizeFactory.millimetersToPostscriptPoints(getRadius()));
        tp.clip();
        tp.newPath();
        tp.addImage(image);

        float left = getPosition()[0];
        float top = getPosition()[1];
        if (getPage().getPdfDocument().getCutmarks() != null) {
            left += SizeFactory.CUT_MARK;
            top -= SizeFactory.CUT_MARK;
        }

        cb.addTemplate(tp, SizeFactory.millimetersToPostscriptPoints(left),
                SizeFactory.millimetersToPostscriptPoints(pageHeight - (top + getPosition()[3])));
        if (getBorder() != null) {
            cb.setLineWidth(SizeFactory.millimetersToPostscriptPoints(getBorder().getThickness()));
            cb.setColorStroke(getBorder().getBaseColor());
            cb.roundRectangle(SizeFactory.millimetersToPostscriptPoints(left),
                    SizeFactory.millimetersToPostscriptPoints(pageHeight - (top + getPosition()[3])), width,
                    height, SizeFactory.millimetersToPostscriptPoints(getRadius()));
            cb.stroke();
        }

    } catch (Exception e) {
        throw new PdfRenderException(e);
    }

}