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

Free Support Forum - aspose.com

Image Margin Issue in FloatingBox

Hi, I’m trying to create a page header with color background and an image like this:

image.png (4.2 KB)

I’m using ASPOSE pdf for Java and here’s my code:

    Document document = new Document();

Page pdfPage = document.getPages().add();

// setting page widths and margins
pdfPage.getPageInfo().setWidth(918);
pdfPage.getPageInfo().setHeight(1188);
pdfPage.getPageInfo().setMargin(new MarginInfo(60, 60, 60, 60));

double pageWidth = pdfPage.getPageInfo().getWidth();

FloatingBox coverHeaderBox = new FloatingBox((float) (pageWidth - 120), 120);
coverHeaderBox.setBackgroundColor(Color.getBlack());


Image logo;
try (InputStream is = bannerResource.getInputStream()) {
  Image image = new Image();
  image.setImageStream(is);
  image.setMargin(new MarginInfo(120, 0, 30, 30));
  logo = image;
}

coverHeaderBox.getParagraphs().add(logo);


// adds the floating box to the page
pdfPage.getParagraphs().add(coverHeaderBox);


// saving the document
try (OutputStream os = Files.newOutputStream(resultResource.getFile().toPath())) {
  document.save(os, SaveFormat.Pdf);
}

and the result doesn’t respect left margin, screenshot from the resulting document:

image.png (26.3 KB)

@lqutom,

Can you attack the logo so I can properly try to replicate?

attached.
JNR.png (2.9 KB)

@lqutom,

Can you claryfy what do you mean it does not respect the left margin?

The left margin is 60 pixels.

And 0.83 inches at 72 PPI is aproximatedl 60 pixels

@carlos.molina I’m talking about the image margin, left margin should be 120px within the box:
image.setMargin(new MarginInfo(120, 0, 30, 30));

@lqutom,

okay, got it. Let me review it and get back to you.

@lqutom,

So it seems it is a bug. I will be creating a ticket for it but I found a workaround for now by using the padding of the floatingBox.

The code sample:

public void Logic() throws Exception
{
    Document document = new Document();

    Page page = document.getPages().add();

    // setting page widths and margins
    page.getPageInfo().setWidth(918);
    page.getPageInfo().setHeight(1188);
    page.getPageInfo().setMargin(new MarginInfo(60, 60, 60, 60));

    double pageWidth = page.getPageInfo().getWidth();

    FloatingBox coverHeaderBox = new FloatingBox((float) (pageWidth - 120), 120);
    coverHeaderBox.setBackgroundColor(Color.getBlack());


    try
    {
        File initialFile = new File(PartialPath + ".png");
        InputStream stream = new FileInputStream(initialFile);
        Image image = new Image();
        image.setImageStream(stream);
        image.setMargin(new MarginInfo(0, 0, 30, 30));
        coverHeaderBox.getPadding().setLeft(120);

        coverHeaderBox.getParagraphs().add(image);
    }
    catch (Exception ex)
    {}

    // adds the floating box to the page
    page.getParagraphs().add(coverHeaderBox);


    // saving the document
    document.save(PartialPath + "_output.pdf");
}

The output:
MarginsNotWorking_output.pdf (22.5 KB)

Hopefully this allows you to create what you wanted.

@lqutom
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-53744

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.