Clipping / Scaling images

Hiyas

Three little questions:
a) Is it possible using Aspose.Words to clip a picture to its container? Say, when the image itself is 10x10mm but the container should only be like 5x5mm so it would display the first 5x5mm only.
b) is it possible to scale an image, so that, when the contianer is 100x70mm and the picture is 7x7mm, that it would display the image as 70x70mm ?
c) When adding the same image, say, 30 times (say, for a logo that needs to appear on all pages or so), is it possible to create like a template, so that the image is physically inserted only once but can appear on different places to save storage space so that the document doesn’t grow too much in size?

Best regards,
Rog

Hi
Thanks for your interest in Aspose.Words.

  1. Yes, you can do this using Aspose.Words. Please see the following code snippet.
// Create ne document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Image img = Image.FromFile("test.jpg");
// Insert Image
Shape croppedImage = builder.InsertImage(img, 50, 50);
// Crop image
if (croppedImage.Width / ConvertUtil.PixelToPoint(img.Width) < 1)
    croppedImage.ImageData.CropRight = 1 - croppedImage.Width / ConvertUtil.PixelToPoint(img.Width);
if (croppedImage.Height / ConvertUtil.PixelToPoint(img.Height) < 1)
    croppedImage.ImageData.CropBottom = 1 - croppedImage.Height / ConvertUtil.PixelToPoint(img.Height);
// Save output document
doc.Save("out.doc");
  1. You can do this using Aspose.Words too. For example if you use the following code and image is 30x30 then image will be resized.
builder.InsertImage(img, 50, 50);

But note that shape size is defined in points and size of image is defined in pixels. You can use ConvertUtil class to convert between various measurement units.

  1. You can add your logo into the header of footer of document then this image will be repeated on each page of document. See the following link for more information.
    https://docs.aspose.com/words/net/working-with-headers-and-footers/

I hope this could help you.
Best regards.

Thanks for your answer. Your answer is for .NET I guess.
Using Java, I imagine cropping an image would be better done using a separate BufferedImage to do the crop and then using that cropped image for insertImage(), right?

As for the logos… the problem is, that the logo can appear in the content part of the document as well and doesn’t need to be in the header or footer. I was thinking that it might be possible to add the image using new Shape()… and the reusing that same node and adding that one using insertNode() later on several times, but I’m not sure whether this would work and if it would be a brutal hack or actually something that I should try out

Best regards and many thanks
Roger

Hi

Thanks for your request.

  1. Yes, of course you can crop image manually before inserting it into the document. But you can also crop image in the same way as I mentioned in the previous post. See the following link to learn more.
    https://reference.aspose.com/words/net/aspose.words.drawing/imagedata/cropbottom/
  2. You can insert the save Shape in the document several times ,but before you should clone source Shape. For example see the following code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape wordImg = builder.insertImage("test.jpg");
builder.insertNode(wordImg.deepClone(true));
doc.save("out.doc");

But the images do get saved 100 times if you have repeated it 100 times. It is simply because we have not implemented a mechanism for reusing same images when saving files.
Best regards.

>> But
the images do get saved 100 times if you have repeated it 100 times. It
is simply because we have
>> not implemented a mechanism for reusing same
images when saving files.

That is understandable, but I’m more concerned about the memory usage whilst it is running (the memory usage inside the Java VM that is). If I add it 100 times with the method mentioned above, is the memory usage roughly a bit more as if its inserted once, or is it also 100 times the memory I need to load the file?

Hi
When you duplicate content with images in Aspose.Words, the images are not deep-cloned. Therefore it does not matter if you have 100 records or one, in memory it will be only one image.
Image will be repeated when you save that document to disk.
Best regards.