在Itext中裁剪图像

Lui*_*ixv 4 java crop itext

有没有一种简单的方法来在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.
Run Code Online (Sandbox Code Playgroud)

现在我想"删除"一条让我们说左边20像素和右边20像素的条带.是否有捷径可寻?

Nat*_*usa 5

这是使用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);
}
Run Code Online (Sandbox Code Playgroud)

请注意,这不需要调用t.rectangle(),t.clip()或t.newPath().