Reputation: 16684
I got a large PDF which is embedded in a homepage and should have interactive links with custom javascript commands in it.
This document should be maintained by some students and it is not possible for them to go through all sites and set the javascript commands after a change in the original word file. So i want them to set hyperlinks in word where an action should occur.
After that i want to remove the hyperlink with iTextSharp and set a javascript action instead.
I can communicate with HTML, find the hyperlinks correctly and can change their url like in this post
Also tried to add this to the annotation action:
string destination = AnnotationAction.GetAsString(PdfName.URI).ToString();
AnnotationAction.Put(PdfName.JAVASCRIPT, new PdfString("this.hostContainer.postMessage(\"" + destination + "\");"));
But it is always calling the original hyperlink before it is executing the javascript. So how can i remove the hyperlink without losing the formation of the text within the document?
Upvotes: 1
Views: 955
Reputation: 55457
You just need to change the value of /S
to /JAVASCRIPT
and set your JavaScript command as the value of /JS
. Instead of changing things, however, I'm just going to blow away the original action and create a new one, just to make sure there isn't any baggage.
The first three line below are from my original answer that you linked to, after that I've killed off the URL changing code and substituted it with the JavaScript code. Since you understood my other post this should make sense hopefully, too.
//Make sure this annotation has an ACTION
if (AnnotationDictionary.Get(PdfName.A) == null)
continue;
//Remove our old action entry just so nothing weird hangs around
AnnotationDictionary.Remove(PdfName.A);
//Create a new action entry
var a = new PdfDictionary(PdfName.A);
//Set it as an action
a.Put(PdfName.TYPE, PdfName.ACTION);
//Set it as JavaScript
a.Put(PdfName.S, PdfName.JAVASCRIPT);
//Set the JavaScript
a.Put(PdfName.JS, new PdfString(@"app.alert('Hello World');"));
//Add it back to the annotation
AnnotationDictionary.Put(PdfName.A, a);
Upvotes: 3