PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Mouse Trigger Events to show/hide Annotations
https://forum.pdfsharp.net/viewtopic.php?f=2&t=745
Page 1 of 1

Author:  Sappharad [ Mon Jun 01, 2009 3:38 am ]
Post subject:  Mouse Trigger Events to show/hide Annotations

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.

Author:  Sappharad [ Fri Jun 05, 2009 3:19 am ]
Post subject: 

UPDATE!
I had someone generate a working document in Adobe Acrobat and worked backwards from there.

It turns out that using a Read Only AcroForms Text Field in place of a Free Text Annotation works fine. I'm still missing something though, because my output via PDFsharp works in Acrobat Reader but not in Foxit Reader or Mac OS X Preview. The text field is there, but the show/hide action has no effect in the other two viewers. Yet the Acrobat generated document works fine in all 3 of those viewers. So far the only difference I see is that the Acrobat generated file uses a lot more Indirect references for some reason when the spec doesn't require them.

I'll keep researching this trying to figure out a solution to my problem, but still any help would be appreciated. This must be something simple that I'm missing.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/