Inserted image overlapping footer

We are programmatically generating documents with images embedded in them using aspose. When we insert a tall image partway down the page, is is forced up the page so that it doesn’t run off the end of the page, but it does overlap the footer and margin. Is there any way to avoid this behavior? If not, is there any way to detect it?

I have found references to WORDSNET-2978 related to determining the page position of elements, but it doesn’t appear that that issue is resolved yet.

The attached example demonstrates the problem. The code should generated three paragraphs of text with a tall image left aligned at the beginning of the third paragraph with the text wrapping around to the right. The image is too big to fit on the page with that starting point, so is moved up the page so that the entire image fits. This is the behavior we want, except that it overruns the footer and page margin. Is there something we can set so that the image stays completely within the document body?

Hi Shad,

Thanks for your inquiry. I have modified your code. Please check the highlighted section of following code snippet. I have attached the output document with this post. Please read following documentation links for your kind reference and let us know if you have any more queries.

https://reference.aspose.com/words/java/com.aspose.words/horizontalalignment/
https://reference.aspose.com/words/java/com.aspose.words/verticalalignment/
https://reference.aspose.com/words/java/com.aspose.words/relativehorizontalposition/
https://reference.aspose.com/words/java/com.aspose.words/relativeverticalposition/

StringBuilder sb = new StringBuilder();
for (int i = 0; i <10; i++)
{
    sb.append("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod. ");
}
String loremText = sb.toString();
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.writeln(loremText);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
builder.writeln(loremText);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
Shape shape = builder.insertImage(MYDir + "tall.png");
shape.setWrapType(WrapType.SQUARE);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
shape.setHorizontalAlignment(HorizontalAlignment.LEFT);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);
shape.setVerticalAlignment(VerticalAlignment.TOP);
shape.setAllowOverlap(false);
builder.writeln(loremText);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
document.updatePageLayout();
document.save(MYDir + "imagePlacement.docx", SaveFormat.DOCX);

That solution applies to really tall images if I’m okay with having them aligned at the top (or bottom) of the page, but what about smaller images where I want them to be lined up with a the paragraph where they are inserted. Here’s a modified example that adds a 2x2 inch square image to each of the paragraphs. The third image extends over the footer text.

If I use the vertical alignment code you list above:

shape.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);

shape.setVerticalAlignment(VerticalAlignment.TOP);

all three of the images are aligned at the top of the page rather than with their paragraph. If I try to align them with the paragraph (my original code), the bottom image overlaps the footer as shown in the screenshot attachment.

Here is the modified code that generated the new document (and I attached the square image so you can run it locally):

public static void main(String[] args) throws Exception
{

    // create placeholder text to fill up the paragraphs

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i sb.append("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod. ");

    }

    String loremText = sb.toString();

    Document document = new Document();

    DocumentBuilder builder = new DocumentBuilder(document);

    // add a footer

    builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);

    builder.writeln("This is some footer text that is long enough to show under the image that overlaps it");

    builder.moveToDocumentEnd();

    // now add three paragraphs with a square image inserted at the beginning of each one

    Shape shape = builder.insertImage("square.png");

    shape.setWrapType(WrapType.SQUARE);

    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);

    shape.setHorizontalAlignment(HorizontalAlignment.LEFT);

    shape.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);

    shape.setVerticalAlignment(VerticalAlignment.TOP);

    shape.setAllowOverlap(false);

    builder.writeln(loremText);

    builder.insertBreak(BreakType.PARAGRAPH_BREAK);

    shape = builder.insertImage("square.png");

    shape.setWrapType(WrapType.SQUARE);

    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);

    shape.setHorizontalAlignment(HorizontalAlignment.LEFT);

    shape.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);

    shape.setVerticalAlignment(VerticalAlignment.TOP);

    shape.setAllowOverlap(false);

    builder.writeln(loremText);

    builder.insertBreak(BreakType.PARAGRAPH_BREAK);

    shape = builder.insertImage("square.png");

    shape.setWrapType(WrapType.SQUARE);

    shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);

    shape.setHorizontalAlignment(HorizontalAlignment.LEFT);

    shape.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);

    shape.setVerticalAlignment(VerticalAlignment.TOP);

    shape.setAllowOverlap(false);

    builder.writeln(loremText);

    builder.insertBreak(BreakType.PARAGRAPH_BREAK);

    document.updatePageLayout();

    document.updateFields();

    document.save("imagePlacement2.docx", SaveFormat.DOCX);

}

Hi Shad,

Thanks for your inquiry. I have tested the scenario and have not found the shared issue while using latest version of Aspose.Words for Java. Please use the latest version of Aspose.Words for Java. I have attached the output document file with this post.

Please let us know if you have any more queries.

Ah, in my previous post, the loop that generates the paragraph text got cut off because I included it inline and the less than sign was treated as an html tag hiding part of the code.

Longer paragraphs of text are needed to illustrate this layout problem, so that loop changed slightly from my previous example. If you ran it with my previous code, the layout problem does not show up.

The code that generates the paragraph text should be:

for (int i = 0; i lessthan 14; i++)
{

    sb.append("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod. ");

}

I have also included the full source code as an attachment.

Can you please retest on your end and see if the problem shows up? Thanks

-Shad

Hi Shad,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word do. If you set horizontal/vertical relative position to Margin/Line for shape, you will get the same output.

In this case, please use the absolute position of shape. Please use the following code snippet for your requirements.

shape = builder.insertImage(MYDir + "square.png");
shape.setWrapType(WrapType.SQUARE);
// shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
// shape.setHorizontalAlignment(HorizontalAlignment.LEFT);
// shape.setRelativeVerticalPosition(RelativeVerticalPosition.LINE);
// shape.setVerticalAlignment(VerticalAlignment.TOP);
// shape.setAllowOverlap(false);
shape.setLeft((0.0));
shape.setTop(-100.0);
builder.writeln(loremText);

Please let us know if you have any more queries.

This doesn’t solve my problem (see the attached screenshot). It would work if I used it only for the third image, but that assumes that I knew ahead of time which image was going to layout over the footer. We’re laying out complex 50 page documents and have no way of knowing where on the page elements are going to fall (at least until WORDSNET-2978 gets resolved).

Do you have any other ideas that might solve our problem?

Hi Shad,

Thanks for sharing the additional information. Unfortunately, Aspose.Words does not support the requested feature at the moment. I have linked this forum thread to the feature ID WORDSNET-2978. You will be notified via this forum thread once this feature is available.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-2978) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(67)