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

Free Support Forum - aspose.com

Create floating text that can hide and show


Is it possible to create floating text areas in PDF that can hide and show on hover over a text content.

You have examples where this is achieved using text annotations, but they are comments in PDF and show up in the comments section. We could control them to be non-deleteable, but they are comments still.

Can we have a rectangle that shows and hides on hover over a text. Similar to this example from another site:

The second link has a floating rectangle with text in it.



Thanks for contacting support.

We have already logged a feature enhancement request as PDFNET-42750 in our issue tracking system. Our product team will investigate the feasibility of the feature and as soon as we have some updates in this regard, we will let you know within this forum thread.

However, as a workaround, you can add a text annotation inside PDF and add some JavaScript which will toggle its visibility on mouse over/out. Please check following code snippet where I have implemented this workaround, so that a tooltip feature can be achieved.

string name = "TXTANNOT";
string title = "Mouse Over Annotation";
string comment = "This is test text annotation.";
// Document creating
Document doc = new Document();
doc.Pages.Add().Paragraphs.Add(new TextFragment("This is simple sentence."));
doc.Save(dataDir + "mouseover_annot.pdf");
doc = new Document(dataDir + "mouseover_annot.pdf");
TextFragmentAbsorber absorber = new TextFragmentAbsorber("This is simple sentence.");
Page page = doc.Pages[1];
// Add text annotation
TextAnnotation text = new TextAnnotation(page, absorber.TextFragments[1].Rectangle);
text.Name = name;
text.Title = title;
text.Contents = comment;
// These flags must be raised to suppress showing of annotation icon
text.Flags = AnnotationFlags.NoView | AnnotationFlags.ReadOnly;
Aspose.Pdf.Rectangle popupRect = new Aspose.Pdf.Rectangle(90, 610, 235, 710);
// Add popup annotation
PopupAnnotation popup = new PopupAnnotation(page, popupRect);
// Link text and popup annotations
text.Popup = popup;
popup.Parent = text;
// Add button
Field field = new ButtonField(page, absorber.TextFragments[1].Rectangle);
// Set ButtonField actions
string fieldName = field.PartialName;
string openScript = "var t = this.getAnnot(this.pageNum, '" + name + "'); t.popupOpen = true; var w = this.getField('" + fieldName + "'); w.setFocus();";
string closeScript = "var t = this.getAnnot(this.pageNum, '" + name + "'); t.popupOpen = false;";
field.Actions.OnEnter = new JavascriptAction(openScript);
field.Actions.OnExit = new JavascriptAction(closeScript);
// Save document
doc.Save(dataDir + "mouseover_annot.pdf");

For your reference, I have attached an output, generated by above code snippet. In case of any further assistance, please feel free to contact us.

mouseover_annot.pdf (2.7 KB)

We are sorry for the inconvenience faced.


Regarding adding tooltip in the PDF documents, we have further found that you can achieve this by adding invisible button over the searched text. Please check following code snippet to do so.

string outputFile = GetOutputPath("42750_codeSnippet_N2.pdf");

// Create sample document with text
Document doc = new Document();
doc.Pages.Add().Paragraphs.Add(new TextFragment("Move the mouse cursor here to display a tooltip"));
doc.Pages[1].Paragraphs.Add(new TextFragment("Move the mouse cursor here to display a very long tooltip"));

// Open document with text
Document document = new Document(outputFile);
// Create TextAbsorber object to find all the phrases matching the regular expression
TextFragmentAbsorber absorber = new TextFragmentAbsorber("Move the mouse cursor here to display a tooltip");
// Accept the absorber for the document pages
// Get the extracted text fragments
TextFragmentCollection textFragments = absorber.TextFragments;

// Loop through the fragments
foreach (TextFragment fragment in textFragments)
    // Create invisible button on text fragment position
    ButtonField field = new ButtonField(fragment.Page, fragment.Rectangle);
    // AlternateName value will be displayed as tooltip by a viewer application
    field.AlternateName = "Tooltip for text.";
    // Add button field to the document

// Next will be sapmle of very long tooltip
absorber = new TextFragmentAbsorber("Move the mouse cursor here to display a very long tooltip");
textFragments = absorber.TextFragments;

foreach (TextFragment fragment in textFragments)
    ButtonField field = new ButtonField(fragment.Page, fragment.Rectangle);
    // Set very long text
    field.AlternateName = "Lorem ipsum dolor sit amet, consectetur adipiscing elit," +
                            " sed do eiusmod tempor incididunt ut labore et dolore magna" +
                            " aliqua. Ut enim ad minim veniam, quis nostrud exercitation" +
                            " ullamco laboris nisi ut aliquip ex ea commodo consequat." +
                            " Duis aute irure dolor in reprehenderit in voluptate velit" +
                            " esse cillum dolore eu fugiat nulla pariatur. Excepteur sint" +
                            " occaecat cupidatat non proident, sunt in culpa qui officia" +
                            " deserunt mollit anim id est laborum.";

// Save document

For your reference, an output document is also attached. In case of any further assistance, please feel free to let us know.

42750_codeSnippet_N2.pdf (2.8 KB)