This is the number-one way to isolate a problem. Pepper the code with trace statements and then watch them print in the console window as the code runs. This line of code, when run, will print the words “this is a trace statement” in the console window. This is a trace statement, and placing lines like this one in various locations in your script will allow you to find the exact piece of problem code.
A trace statement can print out a simple string so you know the code ran to at least that place, or it can print out a value being calculated by the script. Either way, this is the most powerful tool in your debug toolset. Our job is to narrow the problem to the smallest piece of code possible.
There are several different techniques that can be used separately or combined. To see an example of this in action, I wrote an article that walks through a debug session on a single piece of code that contains several common errors called Why doesn’t my script work? But here, I’ll discuss each technique separately.
Technique 1: Look in the console window. Always do this first when something goes wrong. If Acrobat is throwing an unhandled exception, it will appear here. The exception message may provide an exact code location and information that makes the bug obvious. If the preferences are set up correctly see the articles on the console window , all exceptions will be displayed. Be careful though–handled exceptions can flood the console window with meaningless errors. Only display all exceptions when you are looking for something specific.
Technique 2: Examine intermediate steps. This is the most general-purpose technique and can be used in many different ways. The basic idea is to place trace statements at key locations within the code so you can see the operation of the code in real time.
If a value is being calculated, then use the trace statements to display the intermediate values. If conditional statements or loops are used in the code, then place the trace statements to indicate which path the code takes.
You can even write special conditional debug code that displays traces only under certain conditions. These trace messages will give you an inside look at how the code is actually working and usually allow you to quickly find the location of a bug. The code can be run line by line or in groups.
The conditions on loops and “ifs” can be tested separately from the contents, and test conditions can be varied quickly and easily. This is especially important for developing a complex algorithm, or a piece of code such as a regular expression.
Technique: 4: Isolate code segments on a test document. If the script depends on Acrobat or form events, then it cannot be isolated in the console. This technique isolates the code from events, scripts or other bugs in the main working PDF or script that may interfere with debug.
It focuses your effort on the problem-at-hand and removes any misleading distractions. Technique: 5: Block out code segments. Some errors can cause problems that can’t be easily isolated by the techniques already discussed. A bug might close a document, lock up Acrobat, flood the console window with endless messages, do nothing at all, or even cause a crash.
In these cases, the problem can be isolated by commenting out code so it is not executed. Start by commenting out code from the bottom of the script a few lines at a time. Place a trace statement above the start of the comment so there is an indication the last line of active code was reached.
Repeat the process of moving the top of the commented section and re-running the code to isolate the line on which the really bad error occurs. Figure 1. Blocking out code is an effective method for finding a fatal error. For nested blocks of code, it is necessary to use a combination of line and block comments to both maintain structure and remove potential problems. Find Required Fields Have a large form and need to figure out which fields are already marked as Required? This tool finds and lists all required fields in a popup menu.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information. Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies.
It is mandatory to procure user consent prior to running these cookies on your website. We keep making the best PDF solution better. Create, edit, sign and send PDF files with ease.
Create professional-looking PDFs from inside your Microsoft Office applications, and in other applications, too. Go from camera to PDF. Snap a photo of a paper document with your mobile device and share it with others as a PDF file. Print to PDF. Save web pages as PDFs to archive content, review pages offline, or print them more reliably.
Not the answer you’re looking for?