Aspose.Words: no image if size too large

Hi,
on our side an effect occurred when inserting images into a document. Beginning from a specific HEIGHT of the image, it is not inserted any more although the document height is much larger. This occurs only for large-sized documents. Please have a look at the attached java code along with the provided image.
The image is created in the document if you use height <1585pt. If height is larger, no image is in the document.
Our main demand is that PDF is created correctly for any image size, but please also have a look at the doc (MS-Word) format, here it is not possible to open the document at all, Word seems to ‘hang’ somehow.

We tested with latest version 13.1.0.
Could you please have a look at this issue?
Please find attached the PDF and Word output, java code and an example image.

BR,
Torsten

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behavior as MS Word do. The maximum size of either side of the page is 55.87cm and the measurement for images must be between 0 and 55.88cm. Please see the attached images for detail.
You can use the following code to resize such images. Hope this helps you. Please let us know if you have any more queries.

public static void resizeLargeImage(Shape image) throws  Exception
{
    // Return if this shape is not an image.
    if (!image.hasImage())
        return;

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    PageSetup ps = image.getParentParagraph().getParentSection().getPageSetup();
    double freePageWidth = image.isInline() ? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin() : ps.getPageWidth();
    double freePageHeight = image.isInline() ? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin() : ps.getPageHeight();

    // Is one of the sides of this image too big for the page?
    ImageSize size = image.getImageData().getImageSize();
    boolean exceedsMaxPageSize = size.getWidthPoints() > freePageWidth || size.getHeightPoints() > freePageHeight;

    if (exceedsMaxPageSize)
    {
        // Calculate the ratio to fit the page size based on which side is longer.
        boolean widthLonger = (size.getWidthPoints() > size.getHeightPoints());
        double ratio = widthLonger ? freePageWidth / size.getWidthPoints() : freePageHeight / size.getHeightPoints();

        // Set the new size.
        image.setWidth(size.getWidthPoints() * ratio);
        image.setHeight(size.getWidthPoints() * ratio);
    }
}

Hi Tahir,

I understand these limitations are valid for MS Word, but why are they also applied to PDF output, which can have any page size? Would it be possible to apply this limitation only when the document is saved as Word document, because at this time you know the output format?

I am asking because my goal is to create a PDF having the original image size (which can be up to 3x3m).

Thank you and best regards,
Torsten

Hi Torsten,


Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behavior as MS Word do. Due the limitations of MS Word (shared in my previous post), you can not insert such image (with size greater than 55.88cm) into Doc/Docx by using MS Word and convert the document to Pdf.

By using Aspose.Words, you can load the file formats mentioned here into Aspose.Words’ DOM (Document Object Model) and convert the loaded document to Pdf. Your image size is greater than 55.88cm (height/width) so you can not insert this image into following file formats with the same page setting (mentioned in your first post) by using Aspose.Words and MS Word.

However, you can insert such big images into PDF file by using our product Aspose.Pdf. Please post your query at Aspose.Pdf forum.

Hope this answers your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi

I was facing a similar problem, and the solution you provided works very well if there are no headers or footers. However, I have a document containing a footer, and as far as I can tell the size of this footer is not included in the “freePageHeight” you mentioned in the code.
Because of this, the image is still to large. When exporting to Word, this means the image overrides the footer. When exporting to pdf, the footers overrides the bottom part of my image.

I’ve been looking for a way to get the size of the header, but so far without success.
Is there any way this can be achieved?

Thanks

Kind regards

Matthias

Maybe i should elaborate a bit more:

I call the “resizeLargeImage”-method inside the “imageFieldMerging(ImageFieldMergingArgs arg0)” of a IFieldMergingCallback implementation.

In code:

@Override
public void imageFieldMerging(ImageFieldMergingArgs arg0) throws Exception
{
     Picture attachement = (Picture) arg0.getFieldValue();
    BufferedImage image = pictureFacade.getFullImage(attachement);
    arg0.setImage(image);
    resizeLargeImage(image, doc, attachement.getFullImageHeight(), attachement.getFullImageWidth(), arg0);
}

public static void resizeLargeImage(BufferedImage image, Document doc, int hoogte, int breedte, ImageFieldMergingArgs arg0) throws Exception
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    PageSetup ps = builder.getPageSetup();

    // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
    double freePageWidth = ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin();
    double freePageHeight = ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin();

    // Is one of the sides of this image too big for the page?
    ImageSize size = new ImageSize(breedte, hoogte);

    boolean exceedsMaxPageSize = size.getWidthPoints() > freePageWidth || size.getHeightPoints() > freePageHeight;
    if (exceedsMaxPageSize) {
        // Calculate the ratio to fit the page size based on which side is longer.
        boolean widthLonger = (size.getWidthPoints() > size.getHeightPoints());
        double ratio = widthLonger ? freePageWidth / size.getWidthPoints() : freePageHeight / size.getHeightPoints();

        // Set the new size.
        arg0.setImageWidth(new MergeFieldImageDimension(size.getWidthPoints() * ratio));
        arg0.setImageHeight(new MergeFieldImageDimension(size.getHeightPoints() * ratio));
    }
}

Image resizes perfectly in a document without header/footer. Image is to big if header/footer is present. I want to be able to calculate the “freePageWidth” and “freePageHeight” variables without the headers/footers. I currently work with version 11.9.0

Hi Matthias,


Thanks for your inquiry. I would suggest you please upgrade to the latest version (v13.4.0) from here.

Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Hi

Thanks for your reply. However, because I needed a quick fix and couldn’t afford to spend more more time on this, I implemented a different solution: because we make our own templates, I was able to determine the (fixed) height of the headers/footers when creating the templates.

Each image now resizes properly for each document.

Thank you anyway

Kind Regards

Matthias

Hi Matthias,


Thanks for your feedback. It is nice to hear from you that you have solved your problem. However, we had already logged this issue as WORDSNET-4229 in our issue tracking system. We will update you via this forum thread once this issue is resolved.

We apologize for your inconvenience.