Skip to main content
Known Participant
March 13, 2020
Answered

Javascript coding within a PDF document

  • March 13, 2020
  • 1 reply
  • 3713 views

Good morning,
I've been searching everywhere for a specific set of codes that I can't seem to find. Here's in a nutshell the situation since I don't even know if this is possible:
I have a PDF form that I created with tons of buttons on it for extra information on each field. In other words, when pressed it shows instructions related to that specific field. I have to create that form in a bilingual way and therefore the instructions have to also be bilingual. I though the easiest way to do this without crowding the text box display would be the following:
1- when the information bubble is press, the button would trigger a display that would ask to press English or French.
2- if English is pressed then the English text would display and if French is pressed then the French text only would display.
3- after displaying the text, it would have the usual OK button to close the information text and return to the form.
I managed to create button tool to display the entire text in English and French all at once, but then realized it it way to long and in some cases you can't even see the OK button to get out of it, that's why I thought separating the text would work better, but I have no clue how to create the display to chose FRENCH or ENGLISH selection. Or even better, to display by default the English one with an option at the bottom that would say "Press here for French" "Press OK to return to the form".

    This topic has been closed for replies.
    Correct answer Thom Parker

    You have several options for displaying text, an alert, a hidden form field, or, you could put the lanuage text directly into the menu, so you don't have to handle the return value. This is the easiest for both you and the user, as long as the text entries are short.

     

    app.popUpMenu(["English", "England is a great country"], ["Français", "La France est un grand pays"]);

    1 reply

    Thom Parker
    Community Expert
    Community Expert
    March 13, 2020

    It would be good to have a method at the document level for determining language. For example, radio buttons at the top of the first page. 

     

    However if you want the user to make a selection at the time they display the message, then a couple of good options are a popup dialog box or a  popup menu. I like menus. 

    https://www.pdfscripting.com/public/Creating-Popup-Menus.cfm

     

    You may want this popup to only appear the first time, that way it's not a constant annoyance. Use a document level variable to determine whether or not the popup language selection is displayed.

     

     

     

     

     

    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Known Participant
    March 17, 2020

    The coding used worked perfectly.  However, now I'm playing with the form to seek if I can do even better.  You mentioned previously that "It would be good to have a method at the document level for determining language. For example, radio buttons at the top of the first page."  And I'm not disregarding that mention at all, in fact that's what I'm trying to work with right now to rise the form to a different level.  The problem I'm having at this moment is that each text box created on the multitude of fields on the form, are English/French within the same box to make the form functionnally bilingual for the user.  I'd prefer as you mentioned to have them select right from the beginning what language they'd like to use, but is that possible to have one form only and when they select English, then only the English text box would appear and vice versa?  Basically, I don't want to have two seperate forms, and both languages should be dormant in the background and when selected the proper one becomes active?  The problem right now is I can't see any possibility with a radio or any type of buttons, to activate/deactivate text boxes, it allows me to do that only on fields.  Is it even possible to have text boxes hiden or showing according to a selection on the PDF form?

    Known Participant
    March 19, 2020

    By blank I mean the page content, not the form fields.

    You start with a blank document, Add the layers, then add form fields. You should be able to either copy them over, or replace pages on the old form with the layered pages. 

    Or, if you already have a form with the form fields, then make that form blank (keeping the fields), then add the layers.  It doesn't matter if the PDF has form fields or not in order to add the layers, what's imporant is that the pages do not contain any content. The form fields are a different thing and can be added before or after the layers are added. 

    One way to make the pages of the exising Form blank, is to create a PDF with all blank pages and do a page Replacment on the original form with the blank pages. This keeps the fields, but gets rid of the page content. 


    I had to play quite a bit with the script to understand it and get it to work the way I wanted, but I finally got it down to where I wanted.  Thank you for your help with that, I love layers now lol 😉

     

    However, two small things that I now need to manage in view of this huge change.  I use the app.popupmenu at the very oppening of the document to get the user to chose the language, but that doesn't take care of my information bubbles buttons on the form to get only English or only French to come up.  What I've done now is to create two buttons side to side, one in English and one in French.  Is there a way from the javascript being use at the oppening of the document, to give it an indication to hide specific buttons if a certain selection is done (i.e. hide French ones if English is selected)?  That would resolve that situation once and for all.

     

    Second and last thing, the app.popupmenu display a box to select English or French, is there a way to make that menu way bigger?  It sorts of get lost as a tiny thing in the middle of the screen.

     

    Thank you again for your huge help with this.  Here's what I got which works for the entry of the document:

     

    var cRtn = app.popUpMenu("English","Français");
    if(cRtn == "English") {
    var ocgs = this.getOCGs();
    for (var i = 0; i < ocgs.length; i++) {
    if (ocgs[i].name == "English") {
    ocgs[i].state = true;
    }
    if (ocgs[i].name == "French") {
    ocgs[i].state = false;
    }
    }
    app.alert("IMPORTANT INFORMATION \n" + "\This is a test.",3);
    }
    else if (cRtn == "Français") {
    var ocgs = this.getOCGs();
    for (var i = 0; i < ocgs.length; i++) {
    if (ocgs[i].name == "English") {
    ocgs[i].state = false;
    }
    if (ocgs[i].name == "French") {
    ocgs[i].state = true;
    }
    }
    app.alert("INFORMATION IMPORTANTE \n" + "\ Ceci est un test."
    ,3);}

     

    UPDATE: I found a script to show/hide buttons:

    var ContractInfoEnglish = getField("ContractInfoEnglish");
    ContractInfoEnglish.display = display.visible;

    However, I have 45 buttons in double (one in English and one in French for each), I'd have to repeat those lines 90 times twice (one for hidden and one for visible), that would take forever and make for a huge script, there's got to be an easier way to do this to bunch up instead of repeating for each one of them?  BTW, those are regular buttons and not radio or anything else, it's just a click and display thing the information thing.

     

    I thought of a way that may work easier, but I have no ideas of the script to use to do it.  I thought maybe I can create a hidden button on the form, that button will have an action of hidding/displaying the information buttons through the form when the checkmark is put in the box.  However, from the JavaScript I wrote at the beginning of the document, how do I insert a command to get a simulation of someone putting a checkmark in that box so it activate the hide/show function?  I saw a few places where it can put a checkmark in the box but it doesn't do the effect of hide/show after.  Basically I think it needs to have a simulation of a mouse up or something like that?