Free Support Forum - aspose.com

FloatingBox Position Not Applied

I want to absolutely position a floating box at the top left of the page, but it never seems to align properly. The box ends up in the middle of the page. Aspose.PDF .NET 18.8

[Test]
public void SimpleFloatingBoxAbsolutePositionTest()
{
    string methodName = MethodBase.GetCurrentMethod().Name;
    string path = DocConfig.GetAssetOutputPath(methodName + ".pdf");

    Document doc = new Document();
    Page page = doc.Pages.Add();

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

    const double width = 222;
    const double height = 333;

    var box = new FloatingBox((float)width, (float)height)
    {
        Left = 0f,
        Top = 0f,
        ZIndex = 345,
        Margin = new MarginInfo(0, 0, 0, 0),
        Padding = new MarginInfo(0, 0, 0, 0),
        IsInLineParagraph = false,
        IsInNewPage = false,
        IsNeedRepeating = false,
        Border = new BorderInfo(BorderSide.None),
        HorizontalAlignment = HorizontalAlignment.Left,
        BackgroundColor = Color.Tomato,
        VerticalAlignment = VerticalAlignment.Top
    };

    page.Paragraphs.Add(box);

    doc.Save(path, SaveFormat.Pdf);
    doc.Dispose();

    FileAssert.Exists(path);
}

SimpleFloatingBoxAbsolutePositionTest.pdf (1.5 KB)

@dirq

Thank you for contacting support.

We have worked with the data shared by you but we have not been able to reproduce this issue in our environment. The FloatingBox is rightly positioned at top left corner of the page. We have attached the PDF file generated by your code snippet, for your kind reference FloatingBox_18.8.pdf. Would you please create a narrowed down sample application reproducing this issue so that we may look into this further, to help you out.

You’re right. Sorry, I missed one line in my example.

[Test]
public void SimpleFloatingBoxAbsolutePositionTest()
{
string methodName = MethodBase.GetCurrentMethod().Name;
string path = DocConfig.GetAssetOutputPath(methodName + ".pdf");

var doc = new Document();
Page page = doc.Pages.Add();

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

page.Paragraphs.Add(new TextFragment($"{methodName} created on {DateTime.Now}.") {HorizontalAlignment = HorizontalAlignment.Center, Margin = new MarginInfo(36, 36, 36, 36)});

const double width = 222;
const double height = 333;

var box = new FloatingBox((float)width, (float)height)
{
    Left = 0f,
    Top = 0f,
    ZIndex = 345,
    Margin = new MarginInfo(0, 0, 0, 0),
    Padding = new MarginInfo(0, 0, 0, 0),
    IsInLineParagraph = false,
    IsInNewPage = false,
    IsNeedRepeating = false,
    Border = new BorderInfo(BorderSide.None),
    HorizontalAlignment = HorizontalAlignment.Left,
    BackgroundColor = Color.Tomato,
    VerticalAlignment = VerticalAlignment.Top
};

page.Paragraphs.Add(box);

doc.Save(path, SaveFormat.Pdf);
doc.Dispose();

FileAssert.Exists(path);
}

Adding page.Paragraphs.Add(new TextFragment($"{methodName} created on {DateTime.Now}.") {HorizontalAlignment = HorizontalAlignment.Center, Margin = new MarginInfo(36, 36, 36, 36)});

Before adding the floatingBox causes the floating box position to move down, and not be positioned to the document. Am I doing this wrong?

SimpleFloatingBoxAbsolutePositionTest.pdf (2.0 KB)

@dirq

Thank you for elaborating it further.

Margin values of TextFragment is moving the FloatingBox down because of Top and Bottom margin in addition to text height. This is expected behavior and not a bug in the API.

Makes sense. I didn’t think it was a bug. I’m just unclear as to how to position something in the top left corner without other things moving it. Should I be using FoatingBox for that?

@dirq

You are adding a TextFragment and a FloatingBox into the Paragraph collection of the PDF page. Since two paragraphs can not overlap one another so they are relocated by moving downwards. So, you can add a TextFragment into Paragraph collection of FloatingBox instead of the collection of Page object. Below code snippet implements the suggested approach so that the floating box does not move, as per your requirements.

    public static void SimpleFloatingBoxAbsolutePositionTest1()
    {
        string methodName = MethodBase.GetCurrentMethod().Name;
        string path = DocConfig.GetAssetOutputPath(methodName + ".pdf");

        Document doc = new Document();
        Aspose.Pdf.Page page = doc.Pages.Add();
        page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

        const double width = 222;
        const double height = 333;

        var box = new FloatingBox((float)width, (float)height)
        {
            Left = 0f,
            Top = 0f,
            ZIndex = 345,
            Margin = new MarginInfo(0, 0, 0, 0),
            Padding = new MarginInfo(0, 0, 0, 0),
            IsInLineParagraph = false,
            IsInNewPage = false,
            IsNeedRepeating = false,
            Border = new BorderInfo(BorderSide.None),
            HorizontalAlignment = HorizontalAlignment.Left,
            BackgroundColor = Aspose.Pdf.Color.Tomato,
            VerticalAlignment = VerticalAlignment.Top
        };

        //Add the Text into paragraphs collection of FloatingBox
        box.Paragraphs.Add(new TextFragment("Sample Text added in FloatingBox") { HorizontalAlignment = HorizontalAlignment.Center, Margin = new MarginInfo(36, 0, 36, 0) });
        page.Paragraphs.Add(box);
        
        doc.Save(path, SaveFormat.Pdf);
        doc.Dispose();

        FileAssert.Exists(path);
    }

We hope this will be helpful. Please feel free to contact us if you need any further assistance.