Code with Finding: |
class PdfGraphics2D {
private boolean drawImage(Image img, Image mask, AffineTransform xform, Color bgColor, ImageObserver obs) {
if (xform==null)
xform = new AffineTransform();
else
xform = new AffineTransform(xform);
xform.translate(0, img.getHeight(obs));
xform.scale(img.getWidth(obs), img.getHeight(obs));
AffineTransform inverse = this.normalizeMatrix();
AffineTransform flipper = AffineTransform.getScaleInstance(1,-1);
inverse.concatenate(xform);
inverse.concatenate(flipper);
double[] mx = new double[6];
inverse.getMatrix(mx);
if (currentFillGState != 255) {
PdfGState gs = fillGState[255];
if (gs == null) {
gs = new PdfGState();
gs.setFillOpacity(1);
fillGState[255] = gs;
}
cb.setGState(gs);
}
try {
com.itextpdf.text.Image image = null;
if(!convertImagesToJPEG){
image = com.itextpdf.text.Image.getInstance(img, bgColor);
}
else{
BufferedImage scaled = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g3 = scaled.createGraphics();
g3.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
g3.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwparam.setCompressionQuality(jpegQuality);//Set here your compression rate
ImageWriter iw = ImageIO.getImageWritersByFormatName("jpg").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
iw.setOutput(ios);
iw.write(null, new IIOImage(scaled, null, null), iwparam);
iw.dispose();
ios.close();
scaled.flush();
scaled = null;
image = com.itextpdf.text.Image.getInstance(baos.toByteArray());
}
if (mask!=null) {
com.itextpdf.text.Image msk = com.itextpdf.text.Image.getInstance(mask, null, true);
msk.makeMask();
msk.setInverted(true);
image.setImageMask(msk);
}
cb.addImage(image, (float)mx[0], (float)mx[1], (float)mx[2], (float)mx[3], (float)mx[4], (float)mx[5]);
Object url = getRenderingHint(HyperLinkKey.KEY_INSTANCE);
if (url != null && !url.equals(HyperLinkKey.VALUE_HYPERLINKKEY_OFF)) {
PdfAction action = new PdfAction(url.toString());
cb.setAction(action, (float)mx[4], (float)mx[5], (float)(mx[0]+mx[4]), (float)(mx[3]+mx[5]));
}
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
if (currentFillGState != 255) {
PdfGState gs = fillGState[currentFillGState];
cb.setGState(gs);
}
return true;
}
}
|