Image alignment in floating box not working

Hi,

When we try to align 2 images on one line, one to the left and one to the right, the results are not what we expected:

Document document = new Document();
File generated = File.createTempFile("generated", ".pdf");
Page page = document.getPages().add();
page.getPageInfo().setMargin(new MarginInfo(50,50,50,50));
FloatingBox box = new FloatingBox((float) (page.getPageInfo().getWidth() - 100), 200);
box.setPadding(new MarginInfo(30,30,30,30));
box.setTop(10);
box.setLeft(10);
box.setBackgroundColor(Color.getBlue());
Image image = new Image();
image.setHorizontalAlignment(HorizontalAlignment.Left);
image.setInLineParagraph(true);
image.setFixHeight(140);
image.setFixWidth(100);
image.setFile("/home/ahashem/Downloads/bananas.png");

Image image2 = new Image();
image2.setHorizontalAlignment(HorizontalAlignment.Right);
image2.setInLineParagraph(true);
image2.setFixHeight(140);
image2.setFixWidth(100);
image2.setFile("/home/ahashem/Downloads/bananas.png");

box.getParagraphs().add(image);
box.getParagraphs().add(image2);

page.getParagraphs().add(box);

document.save(generated.getAbsolutePath());

Sample output:
generated6631338179035582061.pdf (72.9 KB)

Sample image we used:
bananas.png (2.1 KB)

Aspose version: 21.12

@ahashem,

Is there a way for you to upgrade your version?

The one you are using is a couple of years behind, and if there is an issue, it won’t be fixed in that version.

Can you see if this still happens with your code on version 23.2?

Hi @carlos.molina,

We did upgrade the version to the latest one 23.3 but still, the image alignment is not working, the image on the right go beyond the floating box borders

@ahashem,

You can use the following code:

private void Logic()
{
    var doc = new Document();

    var page = doc.Pages.Add();

    // Creating document that will be where we put the images
    doc.Save($"{PartialPath}_output.pdf");

    // Open document
    PdfFileMend mender = new PdfFileMend();

    // Create PdfFileMend object to add text
    mender.BindPdf($"{PartialPath}_output.pdf");

    // Add image in the PDF file
    mender.AddImage($"{PartialPath}_bananas.png", 1, 100, 600, 200, 700);

    // Add image in the PDF file
    mender.AddImage($"{PartialPath}_bananas.png", 1, 400, 600, 500, 700);

    // Save changes
    mender.Save($"{PartialPath}_output.pdf");

    // Close PdfFileMend object
    mender.Close();
}

The output:
AlignedImages_output.pdf (70.0 KB)

The problem is our business requires dynamic images with different heights and widths so this will not work, I will try to develop some code that uses two columns with dynamic size depending on the image width.

ColumnInfo worked here by calculating the size of the first column on the fly and the second image will align to the right afterward.

@ahashem,

But the answer was to show you you can post images aligned. Now the calculations you need to do using image sizes are custom logic and not a problem in the API.

So is there an issue with the API? or do you need to design some code?

This was not clear from the original post.

@carlos.molina
From the API the setHorizantalAlignment is still not working, so there is a problem in the API, what you suggested and the solution I implemented as well are workarounds.

@ahashem,

I am confused by your statements. What do you want to do? Do you want to use a table?
And the image being random does not matter. You can easily use

var image =  System.Drawing.Image.FromFile(@"image path");

Then use image.Width and image.Height to calculate where on the page you want them. You have the size of the page and the size of the picture. That is all you need.

I understand that when setting images in paragraphs looks wrong, but that’s why I give you code to instead of relying on flow positioning, you can use Box positioning.

1 Like

I just wanted to highlight the paragraphic positioning of the images is wrong, if that’s any expected behavior and not a bug you can close the topic.

@ahashem,

After trying lots of different tests,I wasn’t able to make it load correctly when the image is aligned to the right side. I made a very simple example for the dev team to check the issue.

So I will be creating a bug for the dev team now.

The code I made to show the issue in case you are interested:

private void Logic2()
{
    var doc = new Document();

    var page = doc.Pages.Add();

    page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

    Image image = new Image();
    image.HorizontalAlignment = HorizontalAlignment.Left;
    image.IsInLineParagraph = true;
    image.FixHeight = 50;
    image.FixWidth = 50;
    image.File = $"{PartialPath}_bananas.png";

    Image image2 = new Image();            
    image2.HorizontalAlignment = HorizontalAlignment.Right;            
    image2.IsInLineParagraph = true;
    image2.FixHeight = 75;
    image2.FixWidth = 50;
    image2.File = $"{PartialPath}_parrot.jpg";
    
    Image image3 = new Image();            
    image3.HorizontalAlignment = HorizontalAlignment.Center;
    image3.IsInLineParagraph = false;
    image3.FixHeight = 50;
    image3.FixWidth = 50;
    image3.File = $"{PartialPath}_bananas.png";

    Image image4 = new Image();            
    image4.HorizontalAlignment = HorizontalAlignment.Center;
    image4.IsInLineParagraph = false;
    image4.FixHeight = 75;
    image4.FixWidth = 50;
    image4.File = $"{PartialPath}_parrot.jpg";

    Image image5 = new Image();            
    image5.HorizontalAlignment = HorizontalAlignment.Right;
    image5.IsInLineParagraph = false;
    image5.FixHeight = 75;
    image5.FixWidth = 50;
    image5.File = $"{PartialPath}_parrot.jpg";

    Image image6 = new Image();
    image6.HorizontalAlignment = HorizontalAlignment.Left;
    image6.IsInLineParagraph = false;
    image6.FixHeight = 75;
    image6.FixWidth = 50;
    image6.File = $"{PartialPath}_parrot.jpg";

    Image image7 = new Image();
    image7.HorizontalAlignment = HorizontalAlignment.Right;
    image7.IsInLineParagraph = false;
    image7.FixHeight = 50;
    image7.FixWidth = 50;
    image7.File = $"{PartialPath}_bananas.png";

    page.Paragraphs.Add(image);
    page.Paragraphs.Add(image2);
    page.Paragraphs.Add(image3);
    page.Paragraphs.Add(image4);
    page.Paragraphs.Add(image5);
    page.Paragraphs.Add(image6);
    page.Paragraphs.Add(image7);

    doc.Save($"{PartialPath}_1_output.pdf");
}

Al the images aligned to the right side are out of the page size but you can check them on Adobe Acrobat.

@ahashem
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-54301

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as PDFNET-54301) have been fixed in Aspose.PDF for .NET 23.9.

Can you clarify if this is also fixed in Aspose.Total for JAVA?

@kperry1

Equivalent version of both .NET and Java APIs contain similar enhancements and fixes. You can surely try using the latest available version of the Java API and feel free to let us know in case you notice any issues.