/*
* Copyright (c) 1997-2019 IDRsolutions (https://www.idrsolutions.com)
*/
package org.jpedal.examples.handlers;
import org.jpedal.external.ImageHandler;
import org.jpedal.io.ObjectStore;
import org.jpedal.objects.GraphicsState;
import org.jpedal.objects.raw.PdfObject;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
/**
* example of adding in custom image code , replacing code with a blank image of correct size
*/
public class ExampleImageHandler implements ImageHandler {
//tell JPedal if it ignores its own Image code or not
@Override
public boolean alwaysIgnoreGenericHandler() {
return true; //always use this code
}
/**
* tells JPedal not to scale image
*/
@Override
public boolean imageHasBeenScaled() {
return true;
}
@Override
public boolean drawImageOnscreen(final BufferedImage image, final int optionsApplied, final AffineTransform upside_down, final String currentImageFile, final Graphics2D g2, final boolean renderDirect, final ObjectStore objectStore, final boolean isPrinting) {
return false;
}
//pass in raw data for image handling - if valid image returned it will be used.
//if alwaysIgnoreGenericHandler() is true JPedal code always ignored. If false, JPedal code used if null
@Override
public BufferedImage processImageData(final GraphicsState gs, final PdfObject XObject) {
final BufferedImage img;
//see the raw data
//System.out.println(values);
/*
* example implementation creates a blank image of correct size
*/
/*
* workout final size from CTM (assumes no scaling or rotation)
*/
int finalWidth = (int) gs.CTM[0][0];
int finalHeight = (int) gs.CTM[1][1];
/*allow for image upside down or right to left*/
if (finalWidth < 0) {
finalWidth = -finalWidth;
}
if (finalHeight < 0) {
finalHeight = -finalHeight;
}
img = new BufferedImage(finalWidth, finalHeight, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2 = (Graphics2D) img.getGraphics();
final AffineTransform aff = new AffineTransform();
aff.translate(0, -finalHeight);
aff.scale(1, -1);
g2.setTransform(aff);
final String message = "Image removed";
final int fontSize = finalWidth / message.length();
final Font font = new Font("serif", Font.PLAIN, fontSize);
final Rectangle2D messageBounds = font.getStringBounds(message, 0, message.length(), g2.getFontRenderContext());
g2.setFont(font);
g2.drawString(message, (int) ((finalWidth - messageBounds.getWidth()) / 2), -finalHeight - ((finalHeight) / 2));
//NOTE - IMAGE is expected to be UPSIDE DOWN!!!!!
return img;
}
}
|