Add Tooltip in PDF document in C# using Aspose.PDF

The company I work for is currently looking to evaluate your product. It is currently going through our procurement process to get a trial license, but I had a few questions if your product can accommodate our needs.

We are looking to modify pdf documents to do a couple of things.

1. Replace existing text with a hyperlink to an external glossary.

Example:

Unless the context otherwise specifies or requires, capitalized terms

Replace “capitalized” with an external link to an external glossary like http://myglossary.com#capitalized

2. Append an existing PDF to embed this definition in a tooltip.

I think this would require finding the text and placing a hidden button over it and setting the tooltip information to the definition.

Are there any limits to the length of a tooltip?

3. Append an existing PDF to create a separate text block that is hidden until a given word is rolled over.

I think this involves find the correct word and adding a hidden button with AcroScript/Javascript for the mouse over event to show and hide the block of text.

Can Aspose handle all of these scenarios? Do you have any links to your documentation that would demonstrate this?

francis.knebels:

We are looking to modify pdf documents to do a couple of things.

1. Replace existing text with a hyperlink to an external glossary.

Example:

Unless the context otherwise specifies or requires, capitalized terms

Replace “capitalized” with an external link to an external glossary like http://myglossary.com#capitalized

Hi Francis,


Thanks for contacting support.


We are pleased to share that this feature is supported by Aspose.Pdf for .NET. For further details, please visit Search Text Based on Regex and add Hyperlink

2.

francis.knebels:
Append an existing PDF to embed this definition in a tooltip.

I think this would require finding the text and placing a hidden button over it and setting the tooltip information to the definition.

Are there any limits to the length of a tooltip?

Aspose.Pdf for .NET supports the feature to add button control inside PDF file and also set tool tip text. Please try using following code snippet.

[C#]

// instantiate Document instance

Document doc = new Document();

// add page to pages collection of PDF file

doc.Pages.Add();

// create ButtonField object

Aspose.Pdf.Forms.ButtonField button = new Aspose.Pdf.Forms.ButtonField(doc, new Aspose.Pdf.Rectangle(20,20,100,100));

button.AlternateName = "button1";

button.PartialName = "button1";

// TextBoxField.Border = new Border(

Aspose.Pdf.Annotations.Border border = new Aspose.Pdf.Annotations.Border(button);

border.Width = 5;

border.Dash = new Aspose.Pdf.Annotations.Dash(1, 1);

button.Border = border;

// add Field to forms collection of PDF file

doc.Form.Add(button,1);

// Set the tooltip for textfield

(doc.Form["button1"] as Aspose.Pdf.Forms.Field).AlternateName = "Button Field tool tip";

// Save the updated document

doc.Save(“c:/pdftest/ButtonField.pdf”);

3.

francis.knebels:
Append an existing PDF to create a separate text block that is hidden until a given word is rolled over.

I think this involves find the correct word and adding a hidden button with AcroScript/Javascript for the mouse over event to show and hide the block of text.

Do you need the feature to add tool tip to text inside existing PDF file ?

Please share the related details, so that we can reply accordingly.

Do you need the feature to add tool tip to text inside existing PDF file ?



Yes. There are a couple of scenarios that we are trying to find the capabilities for. All scenarios deal with an existing PDF

1. Can I create bookmarks then tag existing text so that when a user gets to a specific piece of text, if he clicks on it it takes him to

Example. I have a pdf document with an appendix called Definitions. So in the body of the pdf when it sees specific text, I can click on that text and the pdf goes to the glossary definition.

So in Definitions I have a definition for Affected System

This is the definition.

“Affected System” shall mean an electric system other than the Transmission Provider’s
Transmission System that may be affected by a proposed interconnection or on which a proposed
interconnection or addition of facilities or upgrades may require modifications or upgrades to the
Transmission System.


So anywhere in the pdf document that I see “affected system,” I want to have a link back to the definition.


2. can I search the pdf for specific text and replace that text with a button of overlay a button on the text. So again I’ve got this definition section with a defnition for Affected System. I want to find ever instance of Affected System in the pdf document and place a tooltip with the entire definition in the tooltip so that when I mouse over the text, affected system, I see the tooltip.

3. So like 2 instead of a tooltip, I want to embed the definition of a block of text. I’m guessing I can define this text in another layer of the pdf and then on mouse over of the text, I show the block of text containing the text of the definition.

These are the 3 scenarios I would like to know that aspose supports.


francis.knebels:
There are a couple of scenarios that we are trying to find the capabilities for. All scenarios deal with an existing PDF

1. Can I create bookmarks then tag existing text so that when a user gets to a specific piece of text, if he clicks on it it takes him to

Example. I have a pdf document with an appendix called Definitions. So in the body of the pdf when it sees specific text, I can click on that text and the pdf goes to the glossary definition.

So in Definitions I have a definition for Affected System

This is the definition.

“Affected System” shall mean an electric system other than the Transmission Provider’s
Transmission System that may be affected by a proposed interconnection or on which a proposed
interconnection or addition of facilities or upgrades may require modifications or upgrades to the
Transmission System.


So anywhere in the pdf document that I see “affected system,” I want to have a link back to the definition.
Hi <o:p style=“color: rgb(51, 51, 51);”></o:p>Francis,

Thanks for sharing the details.

For above stated scenario, you may consider the approach to Set Link Target to a Page in the Same Document and in order to add hyperlink to all matching results of affected system, you may consider following the steps specified over Search Text Based on Regex and add Hyperlink.

The link to


Search and Get Text from Pages of PDF|Aspose.PDF for .NET

looks promising, but only shows how to create a hyperlink to an external url.

Could I also use this technique to add a tooltip to the found text, like scenario #2?

What about dynamically adding the block of text like in scenario #3 that i mentioned in my previous posts?

francis.knebels:
Do you need the feature to add tool tip to text inside existing PDF file ?
Yes.
2. can I search the pdf for specific text and replace that text with a button of overlay a button on the text. So again I’ve got this definition section with a defnition for Affected System. I want to find ever instance of Affected System in the pdf document and place a tooltip with the entire definition in the tooltip so that when I mouse over the text, affected system, I see the tooltip.
Hi,

Thanks for sharing the details.

I am afraid currently Aspose.Pdf for .NET do not support the feature to add ToolTip to Text inside PDF document. However for the sake of implementation, I have logged it as PDFNET-42750 in our issue tracking system. We will further look into the details of this requirement and will keep you updated on the status of correction. Please be patient and spare us little time.
francis.knebels:
3. So like 2 instead of a tooltip, I want to embed the definition of a block of text. I’m guessing I can define this text in another layer of the pdf and then on mouse over of the text, I show the block of text containing the text of the definition.

These are the 3 scenarios I would like to know that aspose supports.
The above stated requirement appears identical to what you have explained for Scenario 2. The only difference I can see is adding the information in separate layer but the intended purpose/behavior is same as explained in Scenario 2. Can you please share some further details, which can help us in further understanding this requirement.

francis.knebels:
The link to


looks promising, but only shows how to create a hyperlink to an external url.

Could I also use this technique to add a tooltip to the found text, like scenario #2?
Hi <o:p style=“background-color: rgb(255, 255, 255); color: rgb(51, 51, 51);”></o:p>Francis,

Thanks for sharing the details.

The above shared link can help in searching text inside PDF document but I am afraid currently Aspose.Pdf for .NET do not support the feature to add ToolTip text to Text element.

francis.knebels:
What about dynamically adding the block of text like in scenario #3 that i mentioned in my previous posts?
As requested in my recent post, please share some further details related to this requirement.

@francis.knebels

Thanks for your patience.

After further investigation of the logged ticket, we have found that may create tooltips by adding invisible button over the searched text. (Scenario #2) It is already possible with current version of Aspose.Pdf. Please check following code snippet, in order to do this. For your reference, we have attached an output as well.

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"));
doc.Save(outputFile);

// 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
document.Pages.Accept(absorber);
// 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
    document.Form.Add(field);
}

// Next will be sapmle of very long tooltip
absorber = new TextFragmentAbsorber("Move the mouse cursor here to display a very long tooltip");
document.Pages.Accept(absorber);
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.";
    document.Form.Add(field);
}

// Save document
document.Save(outputFile);

42750_codeSnippet_N2.pdf (2.8 KB)

Please note that tooltip text is contained in the PDF document as PDF string type outside of content stream. There is no effective restriction on such strings in PDF files. (See PDF Reference Appendix C.) However, a conforming reader (e.g. Adobe Acrobat) running on a particular processor and in a particular operating environment does have such limit. Refer to your PDF reader application documentation.

We are pleased to inform you that our product team is already working over its implementation and as soon as we have some definite updates regarding feature availability, we will let you know. Please be patient and spare us little time.

We are sorry for this inconvenience.

@francis.knebels

Thanks for your patience.

We are pleased to inform you that in latest version Aspose.Pdf for .NET 17.11 PDF hide actions have been implemented and now it is possible to show/hide text box field (or any other type of annotation) on mouse enter/exit over some rectangle (invisible button).

Please consider the following code snippet:

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

// Create sample document with text
Document doc = new Document();
doc.Pages.Add().Paragraphs.Add(new TextFragment("Move the mouse cursor here to display floating text"));
doc.Save(outputFile);

// 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 floating text");
// Accept the absorber for the document pages
document.Pages.Accept(absorber);
// Get the first extracted text fragment
TextFragmentCollection textFragments = absorber.TextFragments;
TextFragment fragment = textFragments[1];

// Create hidden text field for floating text in the specified rectangle of the page
TextBoxField floatingField = new TextBoxField(fragment.Page, new Rectangle(100, 700, 220, 740));
// Set text to be displayed as field value
floatingField.Value = "This is the \"floating text field\".";
// We recommend to make field 'readonly' for this scenario
floatingField.ReadOnly = true;
// Set 'hidden' flag to make field invisible on document opening
floatingField.Flags |= AnnotationFlags.Hidden;

// Setting a unique field name isn't necessary but allowed
floatingField.PartialName = "FloatingField_1";

// Setting characteristics of field appearance isn't necessary but makes it better
floatingField.DefaultAppearance = new DefaultAppearance("Helv", 10, System.Drawing.Color.Blue);
floatingField.Characteristics.Background = System.Drawing.Color.LightBlue;
floatingField.Characteristics.Border = System.Drawing.Color.DarkBlue;
floatingField.Border = new Border(floatingField);
floatingField.Border.Width = 1;
floatingField.Multiline = true;

// Add text field to the document
document.Form.Add(floatingField);

// Create invisible button on text fragment position
ButtonField buttonField = new ButtonField(fragment.Page, fragment.Rectangle);
// Create new hide action for specified field (annotation) and invisibility flag.
// (You also may reffer floating field by the name if you specified it above.)
// Add actions on mouse enter/exit at the invisible button field
buttonField.Actions.OnEnter = new HideAction(floatingField, false);
buttonField.Actions.OnExit = new HideAction(floatingField);

// Add button field to the document
document.Form.Add(buttonField);

// Save document
document.Save(outputFile);

42750_codeSnippet_N3.pdf (3.2 KB)

Please also take into account that PDF actions in the documents work fine in the conforming readers (e.g. Adobe Reader) but no warranties for another PDF readers (e.g. web browser plugins). We have provided brief investigation and found:

a) All implementations of the hide action in PDF documents works fine in the Internet Explorer v.11.0;
b) All implementations of the hide action also works in Opera v.12.14, but we spot some response delay at first opening of the document;
c) Only implementation using HideAction constuctor accepting field name works if Google Chrome v.61.0 browses the document; Please use corresponding constuctors if browsing in the Google Chrome is significant:

buttonField.Actions.OnEnter = new HideAction(floatingField.FullName, false);
buttonField.Actions.OnExit = new HideAction(floatingField.FullName);

d) Any implementation of the hide action in PDF documents doesn’t work if Mozilla Firefox v.47.0 browses the document.

In event of any further query, please feel free to ask.

Hello, when i tried provided code snipped with this changes:

Message was stopped appearing in pdf-viewer, and not worked in Chrome.
Do i need add something, to make it work at least in Chrome?

@Victor_Boschuk

Would you kindly share your sample PDF document along with complete code snippet that you have tried. Furthermore, please share a screenshot of difference between expected and current output so that the scenario can be tested accordingly.

@asad.ali

Here is a code:

// Create hidden text field for floating text in the specified rectangle of the page
TextBoxField floatingField1 = new TextBoxField(fragment.Page, new Rectangle(100, 700, 220, 740));
// Set text to be displayed as field value
floatingField1.Value = "This is the \"floating text field\".";
// We recommend to make field 'readonly' for this scenario
floatingField1.ReadOnly = true;
// Set 'hidden' flag to make field invisible on document opening
floatingField1.Flags |= AnnotationFlags.Hidden;

// Setting a unique field name isn't necessary but allowed
floatingField1.PartialName = "FloatingField_1";

// Setting characteristics of field appearance isn't necessary but makes it better
floatingField1.DefaultAppearance = new DefaultAppearance("Helv", 10, System.Drawing.Color.Blue);
floatingField1.Characteristics.Background = System.Drawing.Color.LightBlue;
floatingField1.Characteristics.Border = System.Drawing.Color.DarkBlue;
floatingField1.Border = new Border(floatingField1);
floatingField1.Border.Width = 1;
floatingField1.Multiline = true;

// Add text field to the document
document.Form.Add(floatingField1);

// Create invisible button on text fragment position
ButtonField buttonField2 = new ButtonField(fragment.Page, fragment.Rectangle);
// Create new hide action for specified field (annotation) and invisibility flag.
// (You also may reffer floating field by the name if you specified it above.)
// Add actions on mouse enter/exit at the invisible button field
buttonField2.Actions.OnEnter = new HideAction(floatingField1.FullName, false);
buttonField2.Actions.OnExit = new HideAction(floatingField1.FullName);

// Add button field to the document
document.Form.Add(buttonField2);

Here is a file

337afd1a-fdc8-4160-9ada-1d36b641b17d_original.pdf (159.7 KB)

Note: example PDF Tooltip using C#|Aspose.PDF for .NET with C# section for Chrome not worked for me.

Thanks in Advance

@Victor_Boschuk

The code snippet which you have shared was incomplete. However, we have tested the scenario in our environment by creating a PDF document from scratch with Aspose.PDF for .NET 19.12 and following code snippet. The tooltip was working in Chrome and for your kind reference, both document and screenshot are attached as well.

string outputFile = dataDir + "TextBlock_HideShow_MouseOverOut_out.pdf";

// Create sample document with text
Document doc = new Document();
doc.Pages.Add().Paragraphs.Add(new TextFragment("Move the mouse cursor here to display floating text"));
doc.Save(outputFile);

// 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 floating text");
// Accept the absorber for the document pages
document.Pages.Accept(absorber);
// Get the first extracted text fragment
TextFragmentCollection textFragments = absorber.TextFragments;
TextFragment fragment = textFragments[1];

// Create hidden text field for floating text in the specified rectangle of the page
TextBoxField floatingField = new TextBoxField(fragment.Page, new Rectangle(100, 700, 220, 740));
// Set text to be displayed as field value
floatingField.Value = "This is the \"floating text field\".";
// We recommend to make field 'readonly' for this scenario
floatingField.ReadOnly = true;
// Set 'hidden' flag to make field invisible on document opening
floatingField.Flags |= AnnotationFlags.Hidden;

// Setting a unique field name isn't necessary but allowed
floatingField.PartialName = "FloatingField_1";

// Setting characteristics of field appearance isn't necessary but makes it better
floatingField.DefaultAppearance = new DefaultAppearance("Helv", 10, System.Drawing.Color.Blue);
floatingField.Characteristics.Background = System.Drawing.Color.LightBlue;
floatingField.Characteristics.Border = System.Drawing.Color.DarkBlue;
floatingField.Border = new Border(floatingField);
floatingField.Border.Width = 1;
floatingField.Multiline = true;

// Add text field to the document
document.Form.Add(floatingField);

// Create invisible button on text fragment position
ButtonField buttonField = new ButtonField(fragment.Page, fragment.Rectangle);
// Create new hide action for specified field (annotation) and invisibility flag.
// (You also may reffer floating field by the name if you specified it above.)
// Add actions on mouse enter/exit at the invisible button field
buttonField.Actions.OnEnter = new HideAction(floatingField.FullName, false);
buttonField.Actions.OnExit = new HideAction(floatingField.FullName);

// Add button field to the document
document.Form.Add(buttonField);

// Save document
document.Save(outputFile);

TextBlock_HideShow_MouseOverOut_out.pdf (3.2 KB)
TooltipChrome.png (10.9 KB)

Thank, you.
But when i trying to open links i getting this message

image.png (3.5 KB)

I figured out why its can’t work on some files. The issue was that i tried to add tool-tips to files that marked in pdf-viewer (SECURED). It’s was possible to add text-fields, but hide action not worked for them.

@Victor_Boschuk

It is good to know that you have figured out the issue reason and things are working at your end now. Please keep using our API and in case you face any issue, please feel free to let us know.