Example usage for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject createFromFileByContent

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject createFromFileByContent

Introduction

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

Prototype

public static PDImageXObject createFromFileByContent(File file, PDDocument doc) throws IOException 

Source Link

Document

Create a PDImageXObject from an image file.

Usage

From source file:com.baseprogramming.pdwriter.PdWriter.java

License:Apache License

/**
 * Draw an image on the current page./*from   ww w  .  j a v  a2  s.c  o m*/
 * @param imageFile File containing image.
 * @param style paragraph style to use
 * @param width image width. Uses image actual width if argument is less than or equal to 0
 * @param height image height. Uses image actual height if argument is less than or equal to 0
 * @throws RuntimeException 
 */
public void drawImage(File imageFile, PdParagraph style, float width, float height) {
    try {
        PDImageXObject imageObject = PDImageXObject.createFromFileByContent(imageFile, document);

        float actualWidth = (width <= 0) ? imageObject.getWidth() : width;
        float actualHeight = (height <= 0) ? imageObject.getHeight() : height;
        yPosition -= actualHeight;
        if (yPosition <= meta.getLowerLeftY()) {
            createNewPage();
            yPosition -= actualHeight;
            if (yPosition < meta.getLowerLeftY()) {
                yPosition = meta.getLowerLeftY();
            }
        }
        try (final PDPageContentStream stream = createStream()) {
            stream.drawImage(imageObject, style.getLeftX(), yPosition, actualWidth, actualHeight);
            yPosition -= style.getLineHeight();
        }
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}