Skip to main content
Participant
June 12, 2025
Answered

Unable to save and rename a PDF form

  • June 12, 2025
  • 1 reply
  • 361 views

I´m trying to rename and save a fillable PDF to a different location without succes. I´m using the following javascript and getting a warning and anerror message as attached. Any suggestions?

 

Thank you all in advance

 

try {
// Diagnostic checks
var diagnostics = [];
diagnostics.push("Acrobat Version: " + app.viewerVersion);
diagnostics.push("JavaScript Enabled: " + app.fs.isJavaScriptEnabled);
diagnostics.push("Document Path: " + (this.path ? this.path : "Unknown"));
diagnostics.push("Is Privileged Context: " + (app.trustedFunction ? "Yes" : "No"));

console.println("Diagnostics:\n" + diagnostics.join("\n"));
app.alert("Diagnostics:\n" + diagnostics.join("\n"), 3);

// Check if document has security restrictions (simplified)
if (this.security && this.security.documentSecurity) {
throw new Error("Document security settings may prevent file operations.");
}

// Trusted function for Save As
var trustedSaveAs = app.trustedFunction(function(doc) {
app.beginPriv();
var newPath = null;
try {
newPath = doc.saveAs({
cConvID: "com.adobe.acrobat.pdf", // Specify PDF format
cPath: doc.path, // Current file path as default
bPromptToOverwrite: true // Warn if overwriting
});
} catch (e) {
throw new Error("Save As failed: " + e);
}
app.endPriv();
return newPath;
});

// Trusted function for renaming
var trustedRenameFile = app.trustedFunction(function(doc, newName) {
app.beginPriv();
try {
doc.saveAs({
cPath: doc.path.replace(/[^\/]+$/, newName),
cConvID: "com.adobe.acrobat.pdf",
bPromptToOverwrite: true
});
} catch (e) {
throw new Error("Rename failed: " + e);
}
app.endPriv();
});

// Main execution
var newPath = trustedSaveAs(this);

if (newPath) {
// Get original filename and append _Completed
var originalPath = this.path;
var fileName = originalPath.substring(originalPath.lastIndexOf("/") + 1);
var newFileName = fileName.replace(/\.pdf$/i, "_Completed.pdf");

// Rename original file
trustedRenameFile(this, newFileName);
app.alert("File saved to new location and original file renamed to " + newFileName, 3);
} else {
app.alert("Save operation cancelled.", 3);
}
} catch (e) {
app.alert("Error during save or rename operation: " + e, 0);
}

Correct answer Bernd Alheit

Remove the cConvID. You use a wrong value for it.

1 reply

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
June 12, 2025

Remove the cConvID. You use a wrong value for it.