Hello.
I'm currently working to modify PDFsharp so that I can use it to output documents with mouse-over tooltips on specific areas. Adding text annotations is not an option, because I don't want the note icon and the user needs to be able to mouse-over a large area of a page for these tooltips.
I've partially accomplished this, by adding support for FreeText annotations, widget annotations, and trigger events along with some changes to allow the AcroForms code in 1.2 to work.
I've reached the point where I have a FreeText annotation on the page, and am using a Widget Annotation PushButton over some text for support for the /X and /E additional action types. But the /Hide action won't work on my FreeText annotation, it doesn't hide. I've also tried attaching JavaScript events for the Enter and Exit and hiding the same annotation via JavaScript, which does not work either. I know these events are firing because I can display an app.alert(''); box just fine with them.
Here is my code that tries to accomplish what I've explained above:
Code:
// Create a new PDF document
PdfDocument document = new PdfDocument();
XFont font = new XFont("Verdana", 14);
// Create a page
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString("The first line of text, with mouse events.", font, XBrushes.Black, 30, 50, XStringFormats.Default);
gfx.DrawString("There is no second line of text.", font, XBrushes.Black, 30, 140, XStringFormats.Default);
// Create a PDF text annotation
PdfFreeTextAnnotation textAnnot = new PdfFreeTextAnnotation(document);
textAnnot.Contents = "This is the contents of the annotation.\nLineTwo";
XRect rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(250, 40)));
textAnnot.Rectangle = new PdfRectangle(rect);
textAnnot.Border.Width = 5;
textAnnot.Border.Style = PdfAnnotationBorderStyles.Beveled;
textAnnot.TextAlign = 1;
page.Annotations.Add(textAnnot);
//Create another annotation, just for kicks
PdfWidgetAnnotation mouseAnot = new PdfWidgetAnnotation(document);
mouseAnot.Contents = "Mouse over widget test!";
rect = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 40), new XSize(280, 20)));
mouseAnot.Elements.SetString("/T", "TestButton"); //Field name needed for this to be recognized...?
mouseAnot.Rectangle = new PdfRectangle(rect);
mouseAnot.Elements.SetName("/FT", "/Btn"); //Set this up as a form button.
mouseAnot.TriggerEvents.MouseUp.ActionName = "/JavaScript";
mouseAnot.TriggerEvents.MouseUp.Elements.SetString("/JS", "app.alert('Mouse events are working...');");
mouseAnot.TriggerEvents.Enter.ActionName = "/Hide";
mouseAnot.TriggerEvents.Enter.Elements.SetReference("/T", textAnnot);
mouseAnot.TriggerEvents.Enter.Elements.SetBoolean("/H", false);
mouseAnot.TriggerEvents.Exit.ActionName = "/Hide";
mouseAnot.TriggerEvents.Exit.Elements.SetReference("/T", textAnnot);
mouseAnot.TriggerEvents.Exit.Elements.SetBoolean("/H", true);
page.Annotations.Add(mouseAnot);
mouseAnot.AcroFormFlags = PdfAcroFieldFlags.Pushbutton;
PdfPushButtonField butt = (PdfPushButtonField)document.AcroForm.Fields.CreateAcroField(mouseAnot);
// Save the document...
string filename = "Annotations.pdf";
document.Save(filename);
Process.Start(filename);
This code will output a PDF document where you can click on the first line of text for an alert dialog. But mousing over the line then exiting the button area does not hide the Free Text annotation below that line like it should.
Does anyone understand why this is not working? It seems like the hide event can't find the reference to my text annotation for some reason, but I don't know why. Here is a copy of PDFSharp 1.2 with all of the modifications I've made that will allow the code above to compile:
http://www.sappharad.com/downloads/PDFS ... hanges.zip
The code above can be found in the C# GDI+ Annotations example, I've simply replaced that example.
Any feedback that could point me in the right direction would be appreciated.