Joined: 01 Jun 2009
|Posted: Mon Jun 01, 2009 3:38 am Post subject: Mouse Trigger Events to show/hide Annotations
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.
Here is my code that tries to accomplish what I've explained above:
// 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;
//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.Elements.SetString("/JS", "app.alert('Mouse events are working...');");
mouseAnot.TriggerEvents.Enter.ActionName = "/Hide";
mouseAnot.TriggerEvents.Exit.ActionName = "/Hide";
mouseAnot.AcroFormFlags = PdfAcroFieldFlags.Pushbutton;
PdfPushButtonField butt = (PdfPushButtonField)document.AcroForm.Fields.CreateAcroField(mouseAnot);
// Save the document...
string filename = "Annotations.pdf";
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:
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.
Joined: 01 Jun 2009
|Posted: Fri Jun 05, 2009 3:19 am Post subject:
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.