Copy link to clipboard
Copied
Hi all.
I am trying to debug some code I have for moving/resizing pages. Granted, I'm familiar with coding but not Acrobat Specific API. And I'm also having trouble finding simple documentation about what functions are available (eg every function available for this.*).
Can I have some insight on where I'm going wrong below? It won't run and gives me errors both about functions I'm calling not existing, as well as my calls to try and get a handle to the current document.
// Display initial dialog box
app.alert("The script is running. Another dialog box will show upon completion.", 1);
// Function to move page content left and resize page if necessary
function adjustPage(page) {
// Define margins
var rightMargin = 0.4 * 72; // 0.4 inch in points
var leftMargin = 0.2 * 72; // 0.2 inch in points
// Get page width
var pageWidth = page.getCropBox()[2] - page.getCropBox()[0];
// Iterate through page contents
var contents = page.pageContents;
for (var i = 0; i < contents.length; i++) {
var content = contents[i];
if (content instanceof TextAnnotation) {
// Move text annotations
content.move([-rightMargin, 0]);
} else {
// Move other content
content.move([-rightMargin, 0]);
}
}
// Check if any content is outside right margin
var pageBounds = page.getPageBox();
if (pageBounds[2] > pageWidth - rightMargin) {
// Resize page
var resizeAmount = pageBounds[2] - (pageWidth - rightMargin);
page.setSize(pageWidth - resizeAmount, pageBounds[3]);
}
// Check if any content is outside left margin
pageBounds = page.getPageBox();
if (pageBounds[0] < leftMargin) {
// Resize page
var resizeAmount = leftMargin - pageBounds[0];
page.setSize(pageWidth + resizeAmount, pageBounds[3]);
}
}
// Get the current document
var doc = app.activeDocs[0];
// Set the document to edit mode
if (!doc.isDocumentEdited) {
doc.dirty = true;
}
// Initialize error log
var errorLog = [];
// Iterate through pages
for (var i = 0; i < doc.numPages; i++) {
var page = doc.getPageNth(i);
if ((i + 1) % 2 === 0) { // Check if page is even
adjustPage(page);
}
}
// Save error log to CSV file
var errorLogStr = "Error,Page Number\n";
for (var i = 0; i < errorLog.length; i++) {
errorLogStr += errorLog[i].error + "," + errorLog[i].pageNumber + "\n";
}
var filePath = doc.path.replace(/\.pdf$/, "-Script-Errors.csv");
try {
var file = util.writeFile(filePath, errorLogStr);
if (file != null) {
// Open error log file
app.openDoc(file.path);
} else {
app.alert("Error saving error log file.");
}
} catch (e) {
app.alert("Error saving error log file: " + e);
}
// Display completion dialog box
app.alert("Script completed. Error log saved to: " + filePath, 1);
Copy link to clipboard
Copied
There is a lot of non-sense in this code. You couldn't have written this much by simply making things up. You could have easily found loads of real infomation on writing Acrobat JavaScript with a simple google search. And anyone who was actually familiar with programming would have done some basic testing before they got past the first few lines. So you really aren't trying to do debug are you? This script was created with an AI, wasn't it?
Here's a link to the Acrobat JavaScript Reference. You also find a programming guide here:
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#javascript-apis
And you'll find tutorials and samples here (you'll even find a free example script that does almost exactly what you want):
Copy link to clipboard
Copied
It was generated with ChatGPT. I will review the API you provided and the examples!
However, given my lack of coding experience, is there some direction I can provide as a prompt that will help a LLM to produce code that is correct in this context?
Copy link to clipboard
Copied
NO!
You are thinking you can cheat. To do something that takes real knowledge and effort by faking it, and then getting someone else to do the work for you. Get to work, get out your wallet, or go away.