Example usage for org.apache.pdfbox.pdmodel PDPageContentStream drawXObject

List of usage examples for org.apache.pdfbox.pdmodel PDPageContentStream drawXObject

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPageContentStream drawXObject.

Prototype

@Deprecated
public void drawXObject(PDXObject xobject, float x, float y, float width, float height) throws IOException 

Source Link

Document

Draw an xobject(form or image) at the x,y coordinates and a certain width and height.

Usage

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.TableDrawUtils.java

License:EUPL

public static void drawImage(PDPage page, PDPageContentStream contentStream, float contentx, float contenty,
        float width, float height, float padding, PDFBoxTable abstractTable, PDDocument doc, Entry cell,
        PDResources formResources, Map<String, ImageObject> images, ISettings settings, IDGenerator generator)
        throws PdfAsException {
    try {//from w  ww .j av a 2s  . c o  m
        float innerHeight = height;
        float innerWidth = width;

        String img_ref = generator.createHashedId((String) cell.getValue());
        if (!images.containsKey(img_ref)) {
            logger.warn("Image not prepared! : " + img_ref);
            throw new PdfAsException("Image not prepared! : " + img_ref);
        }
        ImageObject image = images.get(img_ref);
        PDImageXObject pdImage = image.getImage();

        float imgx = contentx;
        float hoffset = innerWidth - image.getWidth();
        if (cell.getStyle().getImageHAlign() != null && cell.getStyle().getImageHAlign().equals(Style.LEFT)) {
            hoffset = hoffset / 2.0f;
            imgx += hoffset;
        } else if (cell.getStyle().getImageHAlign() != null
                && cell.getStyle().getImageHAlign().equals(Style.RIGHT)) {
            imgx += hoffset;
        } else {
            hoffset = hoffset / 2.0f;
            imgx += hoffset;
        }

        float imgy = contenty;
        float voffset = innerHeight - image.getHeight();
        if (cell.getStyle().getImageVAlign() != null && cell.getStyle().getImageVAlign().equals(Style.MIDDLE)) {
            voffset = voffset / 2.0f;
            imgy -= voffset;
        } else if (cell.getStyle().getImageVAlign() != null
                && cell.getStyle().getImageVAlign().equals(Style.BOTTOM)) {
            imgy -= voffset;
        }

        drawDebugLine(contentStream, imgx, imgy, image.getWidth(), image.getHeight(), settings);

        // logger.debug("Image: " + imgx + " : " + (imgy -
        // image.getHeight()));
        contentStream.drawXObject(pdImage, imgx, imgy - image.getHeight(), image.getWidth(), image.getHeight());
    } catch (IOException e) {
        logger.warn("IO Exception", e);
        throw new PdfAsException("Error", e);
    }

}