Free Support Forum - aspose.com

Positioning issue with rectangles (.net)

Hi

(running the latest nuget package for Aspose.Pdf)

What I want to achieve is a slightly opaque colour box around some part of the page that when viewed has a hover over that displays a note.

When trying to add a colour rectangle with hover over text using the button field to a PDF page and the positioning is off.

We get coordinates for the highlight from an png version of the page that has different dimensions so I’ve scaled them but no joy.

If I add/subtract the arbitrary values that are commented out then it’s closer to where it should be.

Code is:

    static void Main(string[] args)
    {
        Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
        pdfLicense.SetLicense("Aspose.Total.lic");

        string file = @"C:\temp\page.pdf";
        var pageDoc = new Aspose.Pdf.Document(file);

        // Details of the highlight and page
        var colour = "#602D91";
        var topLeftXPos = 102;
        var topLeftYPos = 196;
        var bottomRightXPos = 181;
        var bottomRightYPos = 234;
        var pageWidth = 794;
        var pageHeight = 1123;
        var note = "Hoverover text";

        var newPdf = new Document();

        var pdfPageHeight = pageDoc.PageInfo.Height;
        var pdfPageWidth = pageDoc.PageInfo.Width;

        var xRatio = pdfPageWidth / pageWidth;
        var yRatio = pdfPageHeight / pageHeight;

        var actualRect = new Aspose.Pdf.Rectangle(
            topLeftXPos * xRatio, // llx
            pdfPageHeight - (bottomRightYPos * yRatio), // lly
            bottomRightXPos * xRatio, // urx
            pdfPageHeight - (topLeftYPos * yRatio)); // ury

        var buttonField = new Aspose.Pdf.Forms.ButtonField(pageDoc.Pages[1], actualRect);
        buttonField.AlternateName = $"Note :\n{note}";

        var graph = new Aspose.Pdf.Drawing.Graph((float)pdfPageWidth, (float)pdfPageHeight);
        pageDoc.Pages[1].Paragraphs.Add(graph);
        double left = actualRect.LLX; // - 90; doing this gets it closer to where it should be
        double bottom = actualRect.LLY; // + 70; doing this gets it closer to where it should be
        Aspose.Pdf.Drawing.Rectangle colourrect = new Aspose.Pdf.Drawing.Rectangle(
            (float)left,
            (float)bottom,
            (float)actualRect.Width,
            (float)actualRect.Height);
        colourrect.GraphInfo.LineWidth = 0.1f;

        colourrect.GraphInfo.FillColor = Color.FromArgb(40, int.Parse(colour.Substring(1, 2), System.Globalization.NumberStyles.HexNumber),
                                                            int.Parse(colour.Substring(3, 2), System.Globalization.NumberStyles.HexNumber),
                                                            int.Parse(colour.Substring(5, 2), System.Globalization.NumberStyles.HexNumber));

        graph.Shapes.Add(colourrect);
        pageDoc.Form.Add(buttonField);
        newPdf.Pages.Add(pageDoc.Pages);
        newPdf.Save(@"C:\temp\newPDF.pdf");
    }

Have attached the page pdf and an image of where it should be

page.pdf (35.2 KB)
correctpositioning.png (69.3 KB)

Any help appreciated, especially if there is a better way to achieve this!

Added info, in the above the hover over seems to be in the correct position it’s only the colour rectangle that isn’t if you hover over where the box should be it shows the hover over correctly

Apologies figured out the issue, the new pdf has default margins set which for some reason throw out the graph but not the button!

@simon.fairey

Thank you for your kind feedback.

We are glad to know that things have started working in your environment. Please feel free to contact us in event of any further query, we will be more than happy to assist you.