Problem with rectangle and page margins on existing page

Hi,

I am having problems with adding a rectangle to a page in an existing document. I am trying to draw a box around the very edge of the page using the following code:

    // Open document
    Document document = new Document(_dataDir + "sample.pdf");

    // Get the first page
    Page page = document.getPages().get_Item(1);

    // Create a MarginInfo object
    MarginInfo mi = new MarginInfo(0, 0, 0, 0);

    // Set the page margins
    page.getPageInfo().setMargin(mi);

    // Get the media box size
    Rectangle mb = page.getMediaBox();

    // Create a Graph object
    Graph g = new Graph(mb.getWidth(), mb.getHeight());
    
    // Set the graph margins
    g.setMarginInfo(mi);

    // Create a rectangle the size of the page
    Rectangle r = new Rectangle(0, 0, mb.getWidth(), mb.getHeight());

    // Add it to the Graph object
    g.getShapes().add(r);

    // Add the graph object to the page
    page.getParagraphs().add(g);

    document.save(_dataDir + "new.pdf");

My problem is that the rectangle is drawn the correct size but is offset on the page by what looks to be the default margin size:

image.png (15.3 KB)

If I modify my code to add a new page to the document by changing:

Page page = document.getPages().get_Item(1);

To:

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

And draw the rectangle on this new page then the rectangle is drawn in the correct position:

image.png (11.5 KB)

How can I position the rectangle at the very edge of the page on the existing page?

Thanks
Steve

@SteveHawes

Can you please share the sample PDF document for our reference as well so that we can test the scenario in our environment and address it accordingly.

Document1.pdf (8.3 KB)

This is the document I used for the sample and the screenshots

Thanks

@SteveHawes

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): PDFJAVA-43728

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.

One further bit of information - we get exactly the same issue if we try to add a FloatingBox to the existing page.

Thanks
Steve

@SteveHawes

Sure, we have updated the ticket information accordingly and will inform you once we have some updates about ticket resolution. Please spare us some time.