These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Getting images into popups using an images URI

aklenner
Registered: Oct 20 2011
Posts: 4
Answered

Hi,
 
I am working on PDFs, what I do is the following:
 
I take a PDF extract the text, do a lot of annotating add the annotations as colored layers onto the PDF which leaves me with a PDF that has colored annotated text areas that you can click and a app.popUPMenuEx() is opened where additional Information can be found regarding this annotation. This workflow is basically a java program, so everything is automated, I don't use AdobeAcrobatPro or something.
 
The popUp is done with Javascript. All this works fine but now I have a kind of annotation that needs an image as additional information.
 
So my question is the following: How do I get an image.png file, that is generated while the annotations are made (thus I know its location) into the PDF? Can I directly load images via javascript, using the images URI? Or do I have to first get this image into the pdf document (which is the worst case) I dont want the image to be inside the PDF. I already figured out by reading this forum, that a buttonField is maybe what I am looking for, however therefore my image needs to be an Icon, whatever this is...
 
Anyways, any hint or help is really appreciated.
 
Thanks,
 
Alex
 

My Product Information:
Reader 10.1, Unix/Linux
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Handling images in Acrobat JavaScript is tricky. It's easier if the image is embedded in the PDF. Are you are using iText to create the PDFs? If so then is possible to add your image to the AP branch of the Names tree. But the image will need to be converted into an acceptable format, PNG won't work. Saving images on hidden buttons works even better.

Importing a remote image is difficult, but doable. You can in fact load an image through an FDF file. To see how this works use Acrobat Pro to put an image on a button, then export an FDF file with a script and explicitly specifying the button name, like this.

this.exportAsFDF({aFields:"Button1"});

Open the FDF file in a binary editor and you'll see how the image data is formatted. It follows the standard PDF Image XObject structure.

When this same FDF file is loaded into the PDF it will load the image into the button.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

aklenner
Registered: Oct 20 2011
Posts: 4
Hi Thom,

thank you for your fast answer, I am using a combination of iText and PDFBox. I need PDFBox to extract the positions of the text in the pdf for the mapping of the annotations, iText is used for 'stamping' the new information into it. I think I'll go with the import of the image approach, converting to another format is not critical. Could you provide me with a example of how an image, that has this 4 letter PDF internal id and is part of the PDF's AP branch is added to a javascript popup of any kind? That would be very helpful to me.

Thank you very much.


Alex



thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
If you are using iText then you have total access to the internal structure of the PDF. Just dig down through the Java Classes and you'll be able to find everything you need, including the locations of words on the page.

Images are much to complex to cover in any kind of detail on a forum. But, there are only a 4 options I can think of for displaying an image on a "popup" that can be controlled with a script.

1. Put the image on a button and control the visibility of the button. This approach is nice because you can also control the position of the image on the page.

2. Put the image on an OCG layer and control the visibility of the layer. Not the best option because the layer may be under something else. Cannot control position at runtime.

3. Create a "Stamp" annotation of the image and control the visibility of the stamp. Also good because the position can be controlled.

4. Use a custom dialog, See this article. Popup Widows


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

aklenner
Registered: Oct 20 2011
Posts: 4
Hi Thom,

I have my pictures added on buttons now, and I can easily stamp this buttons into my PDF. What I still not get is the reference system between the iText created buttons and the JavaScript code. I try to give you an example of what I have done so far:

I draw a rectangle into my PDF and I add a JavaScript onto this drawn rectangle. This JavaScript however creates a popup with data that comes from information inside the java software, so I don't need to reference anything inside the PDF, that was easy. Now I created a button, and I want to toggle its visibility. Therefore the JavaScript code somehow needs to address the button that is part of the PDF that was created by iText. I am really brain-slow on this I fear, but I don't see where I can take the reference from....

This is how I create my buttons:

PushbuttonField push = new PushbuttonField(stamper.getWriter(),new Rectangle((float) x,(float) y,(float) x+150,(float) y+150), name );
push.setImage(image);
PdfFormField pushbutton =push.getField();
stamper.addAnnotation(pushbutton,pageNum+1);

These are my rectangles:

contentByte.rectangle((float) x, (float) y, (float) w, (float) h);
if (popup != null) {
PdfAction action = PdfAction.javaScript(fields2JavaScript(popup),writer);
contentByte.setAction(action, (float) x, (float) y, (float) x+ (float) w, (float) y + (float) h);
}

There you can see the part where I create my Javascript (PdfAction.javaScript), this methods looks like this:

private String fields2JavaScript(PopupContent content) {
String ids = "";
String javascript = "";

javascript += "var x = app.popUpMenuEx(";
for (int i = 0; i < content.numOfLinksAndSeparators(); i++) {
javascript += "{cName:'" + content.getKey(i) + "',cReturn:'"
+ content.getLink(i) + "'},";
}

if (ids.length() > 0)
ids += ids.substring(0, ids.length() - 1);
javascript = javascript.substring(0, javascript.length() - 1);
javascript += ");\n " + "if (x != null) {\n" + "app.launchURL(x);\n"
+ "}";
return javascript;
}

Now I guess I can also add a custom JavaScript that toggles the visibility of my formerly created button, but I just don't see how I address it while creating the corresponding JavaScript code. I do have the iText name of the button object but that doesn't work.

Cheers,

Alex
aklenner
Registered: Oct 20 2011
Posts: 4
Ok, seconds after posting I figured it out, its the field name parameter that I can address in the JavaScript code. It's working now, thank you very much for your help.

Cheers,

Alex