0

I need to find a way to add a quite long string in a quite small space in a PDF document.

I am using iTextSharp. I have already tried adding comment annotations (balloons) with PdfAnnotation.CreateText() and I didn't like the way they looked/worked. It made the page too heavy (I had many comments per page) and their behavior was odd in many ways (thank Adobe for that).

Now I was thinking of adding some simple tooltips on 'chunks' in the page or popping-up messageboxes with javascript (like illustrated here : http://www.codehacker.com/ITEXTSHARP/chap15.aspx#). To my great disappointment however, it seems that Acrobat (?) doesn't support Unicode characters in those situations. E.g. I do this:

var javascript = new PdfAnnotation(
    w, 200f, 550f, 300f, 650f,
    PdfAction.JavaScript("app.alert('" + "Αρνάκι άσπρο και παχύ!" + "');\r", w));
chunk.SetAnnotation(javascript);

...and, in the best case, a messagebox with gibberish pops up when the user clicks on the chunk.

Is there any setting for making Unicode acceptable for the code above or another way to do what I want?


EDIT:

I have now seen this: https://stackoverflow.com/a/163065/964053 and I've tried modifying my code like that:

var javascript = new PdfAnnotation(
    w, 200f, 550f, 300f, 650f,
    PdfAction.JavaScript((char)0xFEFF + "app.alert('" + "Αρνάκι άσπρο και παχύ!" + "');\r", w));
chunk.SetAnnotation(javascript);

But nothing seems to change...


EDIT2 : Using octal representation e.g. (\141) doesn't seem to help either...


EDIT3 : This seems to work nice until you double clink on it, but I need to make the tooltip size itself based on the contents size:

var lToolTip = PdfFormField.CreatePopup(
    w, new Rectangle(tc.Left, tc.Bottom, tc.Right, tc.Top), val, true);
chunk.SetAnnotation(lToolTip);

The rectangle provided doesn't seem to be used in any way... Any ideas? I don't know what PdfFormField.CreatePopup() is supposed to create, but I see a small mark on my page that displays a popup when you hover the mouse over it.

Community
  • 1
  • 1
NoOne
  • 3,851
  • 1
  • 40
  • 47

1 Answers1

1

I'm kind of lost in your edits, it's not clear what works for you and what doesn't, but regarding the unicode problem in JavaScript: are you aware that there are two versions of the javaScript() method?

See javaScript(java.lang.String, com.itextpdf.text.pdf.PdfWriter, boolean)

If you add the boolean value true, the JavaScript string should be interpreted as Unicode. If this doesn't solve your problem, I'll delete this answer, and if you clarify your question (cutting away the irrelevant parts), I'll do another attempt.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I didn't even see the other overload! Thanks! Now I have another small problem: when the text in the alert function contains "\r\n", the messagebox doesn't appear at all. I've tried removing the "\n" or the "\r" only, but it doesn't work. It only works when I remove the whole "\r\n". :( – NoOne May 25 '13 at 12:54
  • Ahh! Using val = val.Replace("\r\n", "\\r\\n") on the string fixes everything! Makes sense now... Thanks a lot Bruno. :) – NoOne May 25 '13 at 13:06
  • By the way, do you know any way to put tooltips (like those used in all Windows apps, not like the comments of Acrobat) on chunks? I have seen acrobat display tooltips when you hover the mouse over a comment or a link, but I don't want to use the first, and the second can only display urls in the tooltip. – NoOne May 25 '13 at 13:22
  • 1
    Do you mean Popup annotations as demonstrated in this PDF: http://examples.itextpdf.com/results/part2/chapter07/movie_posters_2.pdf ? The code can be found here: http://itextpdf.com/examples/iia.php?id=152 (See http://tinyurl.com/iiacsCH07 for the C# version) The only other alternative I know of (similar to what happens with URLs), is the TU entry in a field dictionary. However, this involves introducing a form field... – Bruno Lowagie May 25 '13 at 14:10
  • This uses the comment annotations of Acrobat which I try to avoid, but perhaps there is a way to prevent them from opening the resizable window somehow. Otherwise, I will look into the TU entry that you've mentioned. Thanks again! :) – NoOne May 25 '13 at 14:51
  • 1
    It uses the Popup annotation. You can more or less shape the way those are represented, but that demands plenty of work. Another option that comes to mind, is "optional content" (OCG in ISO-language). You could add content in invisible layers and make the layer visible when somebody hovers over a specific area. – Bruno Lowagie May 25 '13 at 16:11
  • Thanks! I'll now have to experiment with all of these! :) – NoOne May 25 '13 at 17:56