Example usage for com.itextpdf.text Image setCompressionLevel

List of usage examples for com.itextpdf.text Image setCompressionLevel

Introduction

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

Prototype

public void setCompressionLevel(final int compressionLevel) 

Source Link

Document

Sets the compression level to be used if the image is written as a compressed stream.

Usage

From source file:com.docdoku.server.extras.TitleBlockGenerator.java

License:Open Source License

private void generateLyfeCycleState(Paragraph preface, ResourceBundle bundle) {
    // Table title
    preface.add(new Paragraph(bundle.getString("lifecycle") + " : " + lifeCycleState, BOLD_12));
    addEmptyLine(preface, 1);// w  w w. j  av  a  2 s.  c o m

    PdfPTable lifeCycleTable = new PdfPTable(5);
    lifeCycleTable.setWidthPercentage(100f);
    PdfPCell cell;
    for (Activity activity : workflow.getActivities()) {

        boolean headerRendered = false;

        for (Task task : activity.getTasks()) {

            if (task.isApproved() || task.isRejected()) {

                if (!headerRendered) {
                    // Table head
                    cell = new PdfPCell(new Phrase(activity.getLifeCycleState(), BOLD_12));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    cell.setColspan(5);
                    lifeCycleTable.addCell(cell);

                    cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.task"), BOLD_12));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    lifeCycleTable.addCell(cell);

                    cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.date"), BOLD_12));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    lifeCycleTable.addCell(cell);

                    cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.author"), BOLD_12));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    lifeCycleTable.addCell(cell);

                    cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.comments"), BOLD_12));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    lifeCycleTable.addCell(cell);

                    cell = new PdfPCell(new Phrase(bundle.getString("lifecycle.signature"), BOLD_12));
                    cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    lifeCycleTable.addCell(cell);

                    headerRendered = true;
                }

                // Table body
                cell = new PdfPCell(new Phrase(task.getTitle(), NORMAL_12));
                lifeCycleTable.addCell(cell);

                SimpleDateFormat simpleFormat = new SimpleDateFormat(bundle.getString("date.format"));
                cell = new PdfPCell(new Phrase(simpleFormat.format(task.getClosureDate()), NORMAL_12));
                lifeCycleTable.addCell(cell);

                cell = new PdfPCell(new Phrase(task.getWorker().getName(), NORMAL_12));
                lifeCycleTable.addCell(cell);

                cell = new PdfPCell(new Phrase(task.getClosureComment(), NORMAL_12));
                lifeCycleTable.addCell(cell);

                if (task.getSignature() != null) {
                    try {
                        byte[] imageByte;
                        int indexOfFirstComma = task.getSignature().indexOf(",");
                        String base64 = task.getSignature().substring(indexOfFirstComma + 1,
                                task.getSignature().length());
                        imageByte = DatatypeConverter.parseBase64Binary(base64);
                        Image image = Image.getInstance(imageByte);
                        image.setCompressionLevel(Image.ORIGINAL_NONE);
                        image.scaleToFit(SIGNATURE_SIZE_W, SIGNATURE_SIZE_H);
                        cell = new PdfPCell(image);
                        lifeCycleTable.addCell(cell);
                    } catch (Exception e) {
                        cell = new PdfPCell(new Phrase(bundle.getString("signature.error"), NORMAL_12));
                        lifeCycleTable.addCell(cell);
                    }

                } else {
                    cell = new PdfPCell(new Phrase(""));
                    lifeCycleTable.addCell(cell);
                }

            }
        }

    }

    preface.add(lifeCycleTable);
}

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;//from   www .ja va 2s. co m
            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 Boolean createBackCoverPage(Document document, String backCoverNote)
        throws DocumentException, MalformedURLException, IOException {
    document.newPage();//from   w w  w. j  a v  a 2 s .  co  m
    Image backCoverImageFrame = Image
            .getInstance(ImageUtilities.getImageDataForFile(this.mContext, "pdf/txtbook_backpage_300.png"));
    backCoverImageFrame.scalePercent(24.0f);
    backCoverImageFrame.setAbsolutePosition(87.0f, 78.0f);
    backCoverImageFrame.setCompressionLevel(this.settings.compressionLevel);
    document.add(backCoverImageFrame);
    PdfContentByte canvas = this.writer.getDirectContent();
    PdfPTable table = new PdfPTable(1);
    table.setTotalWidth(215.0f);
    if (backCoverNote != null && backCoverNote.length() > 0) {
        Paragraph backCoverEl = new Paragraph(backCoverNote, this.serifFont14);
        PdfPCell cell = new PdfPCell();
        cell.setBorder(0);
        cell.addElement(backCoverEl);
        cell.setPadding(13.0f);
        cell.setIndent(0.0f);
        cell.setFixedHeight(310.0f);
        table.addCell(cell);
        table.completeRow();
    }
    Element backUrl = new Anchor("txt-book.com", this.sansFont11Gray);
    backUrl.setName("txt-book.com");
    backUrl.setReference("http://www.txt-book.com");
    Paragraph paragraph = new Paragraph();
    paragraph.setAlignment(2);
    paragraph.add(backUrl);
    PdfPCell cell2 = new PdfPCell();
    cell2.setBorder(0);
    cell2.setHorizontalAlignment(2);
    cell2.addElement(paragraph);
    cell2.setPadding(0.0f);
    cell2.setPaddingTop(0.0f);
    cell2.setIndent(0.0f);
    table.addCell(cell2);
    table.completeRow();
    table.writeSelectedRows(0, -1, 306.0f, 400.0f, canvas);
    return Boolean.valueOf(true);
}

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

public float addConversationPart(ColumnText ct, int column, String dateString, String senderString,
        String contentString, Bitmap contentBitmap, Boolean isVideo, Boolean isMe)
        throws DocumentException, MalformedURLException, IOException {
    float messageWidth = 196.0f;
    Chunk dateChunk = new Chunk(new StringBuilder(String.valueOf(dateString))
            .append(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR).toString(), this.sansFont6Gray);
    float dateWidth = dateChunk.getWidthPoint();
    Paragraph contentParagraph = new Paragraph();
    if (contentString.length() > 0) {
        Element contentChunk = new Chunk(contentString, this.sansFont9);
        messageWidth = contentChunk.getWidthPoint();
        contentParagraph.add(contentChunk);
    }//from w w  w .  j  a  v  a2  s  .  c om
    if (messageWidth < dateWidth) {
        messageWidth = dateWidth;
    }
    if (messageWidth > MAX_COLUMN_CONTENT_WIDTH) {
        messageWidth = MAX_COLUMN_CONTENT_WIDTH;
        dateWidth += 7.0f;
    }
    Paragraph dateParagraph = new Paragraph(dateChunk);
    if (isMe.booleanValue()) {
        dateParagraph.setAlignment(0);
        dateParagraph
                .setIndentationLeft((((BUBBLE_L_WIDTH + messageWidth) + BUBBLE_R_WIDTH) + 7.0f) - dateWidth);
    } else {
        dateParagraph.setAlignment(2);
        dateParagraph
                .setIndentationRight((((BUBBLE_L_WIDTH + messageWidth) + BUBBLE_R_WIDTH) + 7.0f) - dateWidth);
    }
    ct.addElement(dateParagraph);
    contentParagraph.setExtraParagraphSpace(10.0f);
    if (contentString.length() > 0) {
        contentParagraph.setAlignment(0);
        if (isMe.booleanValue()) {
            contentParagraph.setIndentationLeft(8.6f);
            contentParagraph.setIndentationRight(BUBBLE_TEXT_INDENT_ALTERNATE);
        } else {
            contentParagraph.setIndentationRight(8.6f);
            float indentLeft = COLUMN_WIDTH - (BUBBLE_L_WIDTH + messageWidth);
            if (messageWidth == MAX_COLUMN_CONTENT_WIDTH) {
                indentLeft += BUBBLE_TEXT_INDENT_ALTERNATE;
            }
            contentParagraph.setIndentationLeft(indentLeft);
        }
        ct.addElement(contentParagraph);
    } else if (contentBitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if (isVideo.booleanValue()) {
            contentBitmap.compress(CompressFormat.PNG, 50, stream);
        } else {
            contentBitmap.compress(CompressFormat.JPEG, 50, stream);
        }
        Image contentImage = Image.getInstance(stream.toByteArray());
        contentImage.scaleToFit(198.0f, 198.0f);
        if (isVideo.booleanValue()) {
            contentImage.setCompressionLevel(this.settings.compressionLevel);
        }
        contentImage.setSpacingBefore(10.0f);
        contentImage.setSpacingAfter(10.0f);
        if (isMe.booleanValue()) {
            contentImage.setAlignment(1);
        } else {
            contentImage.setAlignment(1);
        }
        ct.addElement(contentImage);
    }
    Paragraph senderParagraph = new Paragraph(new Chunk(senderString, this.sansFont9Gray));
    if (!isMe.booleanValue()) {
        senderParagraph.setAlignment(2);
    }
    senderParagraph.setSpacingAfter(BUBBLE_TEXT_INDENT_ALTERNATE);
    ct.addElement(senderParagraph);
    return messageWidth;
}

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

public void addStorybookPart(Document document, int i, Long date, Long previousDate, String senderString,
        String previousSenderString, String receiverString, String contentString, Bitmap contentBitmap,
        Boolean isVideo) throws DocumentException, MalformedURLException, IOException {
    Paragraph paragraph = new Paragraph();
    if (i == 0) {
        paragraph.add(new Chunk("T", this.serifFont24));
        paragraph.add(new Chunk("his story begins", this.serifFont11));
        if (!this.settings.useTimestamps.booleanValue()) {
            paragraph.add(new Chunk(MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR, this.serifFont11));
        }//from w ww .  java 2  s.  c  om
    } else if (!senderString.equals(previousSenderString)) {
        document.add(Chunk.NEWLINE);
    }
    Boolean dateChanged = Boolean.valueOf(false);
    if (this.settings.useTimestamps.booleanValue()) {
        SimpleDateFormat dateFormat;
        if (i == 0) {
            dateFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
            dateFormat.setTimeZone(TimeZone.getDefault());
            paragraph.add(new Chunk(" on " + dateFormat.format(new Date(date.longValue()))
                    + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR, this.serifFont11));
        } else {
            dateFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
            dateFormat.setTimeZone(TimeZone.getDefault());
            String dateString = dateFormat.format(new Date(date.longValue()));
            String previousDateString = PdfObject.NOTHING;
            if (previousDate != null) {
                previousDateString = dateFormat.format(new Date(previousDate.longValue()));
            }
            if (!previousDateString.equals(dateString)) {
                dateChanged = Boolean.valueOf(true);
                document.add(Chunk.NEWLINE);
                paragraph.add(new Chunk("O", this.serifFont24));
                paragraph.add(new Chunk("n " + dateString + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR,
                        this.serifFont11));
            }
        }
    }
    if (i == 0) {
        paragraph.add(new Chunk("when " + senderString + " texted " + receiverString + ", ", this.serifFont11));
    }
    if (contentString.length() > 0) {
        Boolean addPhrase = Boolean.valueOf(false);
        String phrase = null;
        if (dateChanged.booleanValue()) {
            paragraph.add(new Chunk(new StringBuilder(String.valueOf(senderString)).append(" texted ")
                    .append(receiverString).append(", ").toString(), this.serifFont11));
        } else if (!(i == 0 || dateChanged.booleanValue() || senderString.equals(previousSenderString))) {
            Boolean isOpening = Boolean
                    .valueOf(this.wasLastPhraseOpening.booleanValue() ? this.generator.nextBoolean() : true);
            int phraseIdx = this.lastPhraseIdx;
            if (isOpening.booleanValue() && !this.wasLastPhraseOpening.booleanValue()) {
                this.lastPhraseIdx = -1;
            }
            if (isOpening.booleanValue()) {
                do {
                    phraseIdx = this.generator.nextInt(this.openings.length);
                } while (phraseIdx == this.lastPhraseIdx);
                phrase = this.openings[phraseIdx];
            } else {
                do {
                    phraseIdx = this.generator.nextInt(this.closings.length);
                } while (phraseIdx == this.lastPhraseIdx);
                phrase = this.closings[phraseIdx];
            }
            this.lastPhraseIdx = phraseIdx;
            this.wasLastPhraseOpening = isOpening;
            phrase = phrase.replace("[speaker]", senderString);
            addPhrase = Boolean.valueOf(true);
        }
        if (addPhrase.booleanValue() && this.wasLastPhraseOpening.booleanValue()) {
            paragraph.add(new Chunk(phrase, this.serifFont11));
        }
        paragraph.add(new Chunk("\"" + contentString.trim() + "\"", this.serifFont11));
        if (addPhrase.booleanValue() && !this.wasLastPhraseOpening.booleanValue()) {
            paragraph.add(new Chunk(phrase, this.serifFont11));
        }
        document.add(paragraph);
    } else if (contentBitmap != null) {
        if (i != 0) {
            paragraph.add(new Chunk(new StringBuilder(String.valueOf(senderString)).append(" sent ")
                    .append(receiverString).append(":").toString(), this.serifFont11));
            document.add(paragraph);
            document.add(Chunk.NEWLINE);
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if (isVideo.booleanValue()) {
            contentBitmap.compress(CompressFormat.PNG, 50, stream);
        } else {
            contentBitmap.compress(CompressFormat.JPEG, 50, stream);
        }
        Image contentImage = Image.getInstance(stream.toByteArray());
        contentImage.scaleToFit(445.0f, 195.0f);
        contentImage.setAlignment(1);
        if (isVideo.booleanValue()) {
            contentImage.setCompressionLevel(this.settings.compressionLevel);
        }
        document.add(contentImage);
        document.add(Chunk.NEWLINE);
    }
}

From source file:mobac.program.atlascreators.PaperAtlasPdf.java

License:Open Source License

@Override
protected void processPage(BufferedImage image, int pageNumber) throws MapCreationException {
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    if (document == null) {
        double width = UnitSystem.pixelsToPoints(imageWidth, s.dpi);
        double height = UnitSystem.pixelsToPoints(imageHeight, s.dpi);
        width += s.marginLeft + s.marginRight;
        height += s.marginTop + s.marginBottom;
        Rectangle r = new Rectangle((float) width, (float) height);
        document = createDocument(r);/*from  w  w  w  .ja  va 2  s  . c om*/
    }

    Image iTextImage;
    try {
        iTextImage = Image.getInstance(image, Color.WHITE);
    } catch (BadElementException e) {
        throw new MapCreationException(map, e);
    } catch (IOException e) {
        throw new MapCreationException(map, e);
    }
    iTextImage.setCompressionLevel(s.compression);
    iTextImage.setDpi(s.dpi, s.dpi);

    float width = (float) UnitSystem.pixelsToPoints(imageWidth, s.dpi);
    float height = (float) UnitSystem.pixelsToPoints(imageHeight, s.dpi);
    iTextImage.scaleAbsolute(width, height);

    try {
        document.add(iTextImage);
    } catch (DocumentException e) {
        throw new MapCreationException(map, e);
    }
    document.newPage();
}

From source file:org.bonitasoft.studio.migration.utils.PDFMigrationReportWriter.java

License:Open Source License

private com.itextpdf.text.Image getImageForStatus(int status)
        throws BadElementException, MalformedURLException, IOException {
    switch (status) {
    case IStatus.OK:
        Image im = Image.getInstance(FileLocator
                .toFileURL(MigrationPlugin.getDefault().getBundle().getResource("/icons/valid.png")));
        im.setCompressionLevel(12);
        im.scaleToFit(12, 12);//from  ww  w .j av a  2  s  .  c o  m
        return im;
    case IStatus.WARNING:
        im = Image.getInstance(FileLocator
                .toFileURL(MigrationPlugin.getDefault().getBundle().getResource("/icons/warning.gif")));
        im.setCompressionLevel(12);
        im.scaleToFit(16, 16);
        return im;
    case IStatus.ERROR:
        im = Image.getInstance(FileLocator
                .toFileURL(MigrationPlugin.getDefault().getBundle().getResource("/icons/error.png")));
        im.setCompressionLevel(12);
        im.scaleToFit(12, 12);
        return im;
    default:
        break;
    }

    return null;
}