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,
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,
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,