Issue with Dynamic Tooltip Content and Positioning in PDF Generated by Aspose.PDF

Hi,

The hidden tooltip functionality in our PDF generation process using Aspose.PDF is not displaying correctly when hovering over a text fragment. The tooltip should dynamically reflect content related to the hovered text and be positioned relative to the text fragment, but it currently fails to appear or adjust as expected.

" string[] dimensions =
{
“Mentor”,
“Principal”,
“Binder”
};"

when I click or cursor moving that place displaying the tooltip.
for your reference:

ToolTip.zip (2.8 KB)

@sanjeevkumar.v

To address the issue with dynamic tooltip content and positioning in your PDF generated using Aspose.PDF, you can utilize the ButtonField class to create invisible buttons over the text fragments. These buttons can display tooltips when hovered over. Below is a sample implementation that demonstrates how to add tooltips to text fragments in a PDF document.

Sample Code

using Aspose.Pdf;
using Aspose.Pdf.Forms;
using Aspose.Pdf.Text;

namespace Aspose.Pdf.Examples
{
    public class AddTooltipToText
    {
        public static void Run()
        {
            // Create a new document
            Document document = new Document();
            document.Pages.Add().Paragraphs.Add(new TextFragment("Hover over this text to see a tooltip"));
            document.Pages[1].Paragraphs.Add(new TextFragment("Hover over this long text to see a tooltip"));

            // Save the document temporarily
            string outputFile = "Tooltip_out.pdf";
            document.Save(outputFile);

            // Open the document to add tooltips
            document = new Document(outputFile);

            // Create a TextFragmentAbsorber to find specific text
            TextFragmentAbsorber absorber = new TextFragmentAbsorber("Hover over this text to see a tooltip");
            document.Pages.Accept(absorber);
            TextFragmentCollection textFragments = absorber.TextFragments;

            // Loop through the found text fragments
            foreach (TextFragment fragment in textFragments)
            {
                // Create an invisible button over the text fragment
                ButtonField field = new ButtonField(fragment.Page, fragment.Rectangle);
                field.AlternateName = "This is a tooltip for the text.";
                document.Form.Add(field);
            }

            // Repeat for the second text fragment
            absorber = new TextFragmentAbsorber("Hover over this long text to see a tooltip");
            document.Pages.Accept(absorber);
            textFragments = absorber.TextFragments;

            foreach (TextFragment fragment in textFragments)
            {
                ButtonField field = new ButtonField(fragment.Page, fragment.Rectangle);
                field.AlternateName = "This is a tooltip for the long text. It can be very descriptive.";
                document.Form.Add(field);
            }

            // Save the final document
            document.Save(outputFile);
        }
    }
}

Explanation

  1. Creating the Document: A new PDF document is created, and text fragments are added.
  2. Finding Text Fragments: The TextFragmentAbsorber is used to locate specific text fragments in the document.
  3. Adding Tooltips: For each found text fragment, an invisible button is created at the same position. The AlternateName property of the button is set to the desired tooltip text.
  4. Saving the Document: Finally, the document is saved with the tooltips included.

This approach should help you achieve the desired tooltip functionality in your PDF. If you continue to experience issues with the tooltip not displaying correctly, ensure that the text fragments are correctly identified and that the button fields are positioned accurately over them.

Feel free to reach out if you have further questions or need additional assistance!

Sources:
[1]: Add Tooltip to Text Example