We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Image position is changed after re-sizing it using Java

Hi there,

I’m working to resize an image to the height remaining on a page (to prevent the image from being bumped to the next page when there is a section title above the image), and I’ve used the following function to determine the remaining page size:

private double getTitleOffsetModifier(DocumentBuilder builder, boolean hasTitle) throws Exception {
  if (hasTitle) {
    Document doc = builder.getDocument();
    LayoutCollector lc = new LayoutCollector(doc);
    LayoutEnumerator le = new LayoutEnumerator(doc);
    ParagraphCollection paragraphs = doc.getLastSection().getBody().getParagraphs();
    double height = 0;
    for (Paragraph para : paragraphs) {
      le.setCurrent((lc.getEntity(para)));
      double paragraphHeight = le.getRectangle().getHeight();
      height += paragraphHeight;
    }
    return Math.ceil(height);
  } else {
    return 0;
  }
}

the problem is that it seems like this function resets my builder’s location to the beginning of the document. the image is resized appropriately, but it is placed at the top left of the document, bumping down everything already added to the page. below is the full image processing flow:

private void transformFullPageImage(
  BrandDocumentModel brandInfo,
  DocumentBuilderExtension builder,
  BufferedImage image,
  boolean isFirstImage,
  boolean isLastImage,
  boolean isSectionTitleHidden,
  boolean isSubsectionTitleHidden)
  throws Exception {
  boolean hasTitle = hasTitle(isSectionTitleHidden, isSubsectionTitleHidden) && isFirstImage;

  Shape fullPageImage = builder.insertImage(image);

  setAbsolutePositioningOnShape(fullPageImage);

  // Size Image:
  double pageHeight = builder.getPageSetup().getPageHeight();
  double pageWidth = builder.getPageSetup().getPageWidth();
  
  setFullPageImageSize(brandInfo, builder, fullPageImage, pageHeight, pageWidth, hasTitle);

  // Position Image:
  double fullPageImageHeight = fullPageImage.getHeight();
  double headerOffsetModifier = getHeaderOffsetModifier(builder, brandInfo);
  double titleOffsetModifier = getTitleOffsetModifier(builder, hasTitle);
  double footerOffsetModifier = getFooterOffsetModifier(builder, brandInfo);
  double topPositioningOffset =
    getTopPositioningOffset(
        pageHeight, fullPageImageHeight, headerOffsetModifier, titleOffsetModifier, 
        footerOffsetModifier);
  fullPageImage.setTop(topPositioningOffset);

  double leftPositionOffset = getLeftPositioningOffset(pageWidth, fullPageImage.getWidth());
  fullPageImage.setLeft(leftPositionOffset);

}

everything works as expected when hasTitle is false, so I am guessing the problem is somewhere in the if (hasTitle) block.

@zackforbing

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.