有没有简单的方法在 Itext 中裁剪图像?
我有以下代码:
URL url = new URL(imgUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
iStream = connection.getInputStream();
img = new Jpeg(url);
// a method like
// img.crop(x1, y1, x2, y2) would be nice.
现在我想“删除”一个 strip ,比方说左边 20 像素和右边 20 像素。 有没有简单的方法可以做到这一点?
最佳答案
这是使用 PdfTemplate 裁剪图像的另一种方法。
public static Image cropImage(Image image, PdfWriter writer, float x, float y, float width, float height) throws DocumentException {
PdfContentByte cb = writer.getDirectContent();
PdfTemplate t = cb.createTemplate(width, height);
float origWidth = image.getScaledWidth();
float origHeight = image.getScaledHeight();
t.addImage(image, origWidth, 0, 0, origHeight, -x, -y);
return Image.getInstance(t);
}
请注意,这不需要调用 t.rectangle()、t.clip() 或 t.newPath()。
关于java - 在 Itext 中裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8134421/