Cropping images in Itext

2019-06-26 13:35发布

问题:

is there an easy way to crop an Image in Itext?

I have the following code:

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.

Now I want to "delete" a strip of let's say 20 pixels left and 20 pixels right. Is there an easy way to do this?

回答1:

You could investigate using the clipping path. You'll need to know the width and height of the JPEG. The code might look something like this:

PdfTemplate t = writer.getDirectContent().createTemplate(850, 600);
t.rectangle(x+20,y+20, width-40, height-40);
t.clip();
t.newPath();
t.addImage(img, width, 0, 0, height, x, y);


回答2:

Here is another way to crop an image using 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);
}

Notice this doesn't require calling t.rectangle(), t.clip() or t.newPath().



回答3:

Nathan's solution almost worked for me. The only problem left was white margins because of PdfTemplate having the same size as image to crop.

My solution:

public Image cropImage(PdfWriter writer, Image image, float leftReduction, float rightReduction, float topReduction, float bottomReduction) throws DocumentException {
    float width = image.getScaledWidth();
    float height = image.getScaledHeight();
    PdfTemplate template = writer.getDirectContent().createTemplate(
            width - leftReduction - rightReduction,
            height - topReduction - bottomReduction);
    template.addImage(image,
            width, 0, 0,
            height, -leftReduction, -bottomReduction);
    return Image.getInstance(template);
}


回答4:

//4x6 inch photo height=432 width =288

// scale
if (isMatchFrame) {
    /* width */
    proportion = image.getWidth() / width;
    image.scaleAbsolute((float) width, image.getHeight() / proportion);
} else {
    /* hight */
    proportion = image.getHeight() / height;
    image.scaleAbsolute(image.getWidth() / proportion, (float) height);
}
// crop
document.setMargins((height - image.getHeight()/proportion) / 2, 0, (width - image.getWidth()/proportion) / 2 , 0);

can crop center photo. enjoy it.



回答5:

I also encountered this problem, and here's what i did:

Concept: Get the image as Buffered Image Make the BufferedImage and render as Image (iText)

Document document = new Document(PageSize.A4.rotate());
document.open();

//Get the image as Buffere Image
BufferedImage awtImage = ImageIO.read(new URL("image url"));

//Crop: Sample, get Upper Half of the image
BufferedImage awtImageUpper = awtImage.getSubimage(0, 0, awtImage.getWidth(), awtImage.getHeight()/2);

//Make BufferedImage and render as Image (in iText)
ByteArrayOutputStream baosImage = new ByteArrayOutputStream();
ImageIO.write(awtImageUpper, "png", baosImage);
Image iTextImage = Image.getInstance(baosImage.toByteArray());


//Display Image in pdf
document.add(new Paragraph("image Upper half"));
document.add((Element) iTextImage);


标签: java itext crop