Copy link to clipboard
Copied
Hey!
I have an issue with a JavaScript console. I'm a total beginner, never programmed anything but I have .pdf file with open source book but I want to change the font in all 276 pages without having to click each field manually.
I was reading forums but every script I found was returning "undefined" or "syntax" error.
If you have resolution (even semi-automatic not necesarilly using JavaScript) I would be really grateful.
Copy link to clipboard
Copied
A script can't access the properties of static text. It can only read the word itself and its coordinates on the page. Nothing else. What you're trying to do should not be attempted in a PDF file. Export the file to another format (or better yet, go back to the original file used to create the PDF), make the changes there, and then create a new PDF file.
Copy link to clipboard
Copied
For starters, "undefined" is not an error. It's the opposite, actually. It means the code finished without errors, and without returning any values. A syntax error is an error, on the other hand, so if you get that, post the exact error message (and code!) here for further help.
The basic code to do it is something like this:
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f==null) continue;
if (f.type=="text") f.textFont = "Arial";
}
However, the font name is not always that predictable. You should set one field to have the desired font and then run this command (from the JS Console) to get the correct name of that font (let's say the field is called Text1):
this.getField("Text1").textFont;
Then use the output from that command in your code.
Copy link to clipboard
Copied
I tried one from another post I saw on these forums:
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f.type == "text") {
f.textFont = "Calibri";
f.textSize = 12;
}
}
I think I am missing something though. I know that I have to have ALL lines of code selected when pressing CTRL+Enter. This code is not even changing the size of the font it just prints the number "12" in the console. Nothing else is happening.
Copy link to clipboard
Copied
Yes, that is fine. It should change all text fields in the file. Is that not happening for you? It works fine for me.
Copy link to clipboard
Copied
Uh. I have no idea. Some other sources (I've even tried troubleshooting with ChatGPT) recommended being in Prepare Form tool for this operation. It doesn't work whether I am in Edit PDF nor the Prepare Form.
There are a couple of images in this PDF as well as page numbering and some annotations (but I believe these are set as ordinary Text fields) maybe they are interfering with the script?
Copy link to clipboard
Copied
- You don't need to be in Prepare Form mode to run it.
- Having other types of fields in the file should not be an issue.
Copy link to clipboard
Copied
As you can see in the attachment I run the code and the Font remained as Times New Roman. Is there something I've been missing?
Copy link to clipboard
Copied
Are you trying to change static text fonts or form field fonts?
Copy link to clipboard
Copied
I will be brutally honest with you - what's the difference? I have a file with what appears to be a lot of boxes looking like text fields - if I put another text field by using the option "Add Text" on the tool bar, the resulting field looks exactly the same as ones already in the document.
What I'm trying to do is simply change the font of a whole book (so around 280 pages) into a different one without having to click on every single text box manually and then select font from the Format right-hand tool bar (there are probably more than a 1000 of them because paragraphs are formatted as separate text fields). I also can't seem to select multiple text fields across different pages so the CTRL+A method someone else recommended doesn't seem to work.
Are you trying to change static text fonts or form field fonts?
By PDF Automation Station
Copy link to clipboard
Copied
With a script you can't change the font of static text.
Copy link to clipboard
Copied
A script can't access the properties of static text. It can only read the word itself and its coordinates on the page. Nothing else. What you're trying to do should not be attempted in a PDF file. Export the file to another format (or better yet, go back to the original file used to create the PDF), make the changes there, and then create a new PDF file.

