Example usage for org.apache.pdfbox.pdmodel.graphics.image JPEGFactory createFromImage

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image JPEGFactory createFromImage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.image JPEGFactory createFromImage.

Prototype

public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException 

Source Link

Document

Creates a new JPEG PDImageXObject from a BufferedImage.

Usage

From source file:ambroafb.general.PDFHelper.java

public void replace(BufferedImage img, int index) throws IOException {
    PDPage old = doc.getPage(index);//from   w  ww.  ja  v  a  2  s. c  o  m
    float width, height;
    if (img.getWidth() > img.getHeight()) {
        width = PDRectangle.LETTER.getHeight();
        height = PDRectangle.LETTER.getWidth();
    } else {
        height = PDRectangle.LETTER.getHeight();
        width = PDRectangle.LETTER.getWidth();
    }
    PDPage newPage = new PDPage(new PDRectangle(width, height));
    doc.getPages().insertAfter(newPage, old);
    doc.removePage(old);
    PDImageXObject pdImage = JPEGFactory.createFromImage(doc, img);
    try (PDPageContentStream contents = new PDPageContentStream(doc, newPage)) {
        contents.drawImage(pdImage, 0, 0, width, height);
    }
}

From source file:boxtable.cell.ImageCell.java

License:Apache License

@Override
public void render(final PDDocument document, final PDPageContentStream stream, final float left,
        final float top, final float width, final float height) throws IOException {
    final BufferedImage image = ImageIO.read(file);

    float contentWidth = width - (leftBorder + rightBorder) / 2 - leftPadding - rightPadding;
    float contentHeight = height - (topBorder + bottomBorder) / 2 - topPadding - bottomPadding;

    float imgWidth = image.getWidth();
    float imgHeight = image.getHeight();
    final float ratio = imgWidth / imgHeight;
    if (imgWidth > contentWidth) {
        imgWidth = contentWidth;/*from  w w w .j  a  v  a2 s.  co  m*/
        imgHeight = 1 / ratio * imgWidth;
    }
    if (imgHeight > contentHeight) {
        imgHeight = contentHeight;
        imgWidth = ratio * imgHeight;
    }

    super.render(document, stream, left + (width - imgWidth) * hAlign, top - (height - imgHeight) * vAlign,
            imgWidth, imgHeight);

    final PDImageXObject imageObject = JPEGFactory.createFromImage(document, image);
    stream.drawImage(imageObject, left + (width - imgWidth) * hAlign + leftBorder / 2 + leftPadding,
            top - height + (height - imgHeight) * (1 - vAlign) - topBorder / 2 - topPadding, imgWidth,
            imgHeight);
}

From source file:gov.samhsa.c2s.common.pdfbox.enhance.HexPdf.java

License:Apache License

/**
 * Draw an image starting at current cursor location. If no flags are given,
 * the top-left corner of the image is positioned at the current cursor
 * position. If one of the flags//from   www. ja  va 2 s  . c o m
 * <code>HexPdf.LEFT | HexPdf.CENTER | HexPdf.RIGHT</code> is set, the image
 * is adjusted between <code>leftMargin, rightMargin</code> accordingly. The
 * cursor location is kept unchanged unless the flag
 * <code>HexPdf.NEWLINE</code> is set. This is useful for adding pictures as
 * layers. If <code>HexPdf.NEWLINE</code> is set, the cursor is positioned
 * at <code>leftMargin</code> immediately below the image.
 *
 * @param image the image to be added
 * @param flags see description
 */
public void drawImage(BufferedImage image, int flags) {
    PDImageXObject ximage = null;
    float imW = 0;
    float imH = 0;
    try {
        ximage = JPEGFactory.createFromImage(this, image);
        imW = ximage.getWidth();
        imH = ximage.getHeight();
    } catch (IOException ex) {
        Logger.getLogger(HexPdf.class.getName()).log(Level.SEVERE, null, ex);
    }
    // newpage if image cannot fit on rest of current page
    if ((cursorY - imH) < contentEndY) {
        newPage();
    }
    float imgX = cursorX;
    float imgY = cursorY - imH;
    if ((flags & HexPdf.CENTER) > 0) {
        imgX = contentStartX + ((contentWidth - imW) / 2);
    } else if ((flags & HexPdf.LEFT) > 0) {
        imgX = contentStartX;
    } else if ((flags & HexPdf.RIGHT) > 0) {
        imgX = contentEndX - imW;
    }

    try {
        cs.drawXObject(ximage, imgX, imgY, imW, imH);
    } catch (IOException ex) {
        Logger.getLogger(HexPdf.class.getName()).log(Level.SEVERE, null, ex);
    }

    if ((flags & HexPdf.NEWLINE) > 0) {
        setCursor(contentStartX, imgY - lineSep);
    }
}