Copiar vínculo al Portapapeles
Copiado
I created a JavaScript code which imports files into the attachment of a pdf form, then searches for the written pdf filenames within the pdf form and links the texts to the imported files. I employ Adobe Acrobat X (10.1.16)
The code (which is run as folder-level JS) is shown here:
var impDatObj = app.trustedFunction( function(odoc,filenme,filepth) {
app.beginPriv();
odoc.importDataObject(filenme,filepth);
app.endPriv();
});var attachpath="/mysrv/subdir/";
impDatObj(this, "abcdef_g.pdf", attachpath+"abcdef_g.pdf");
impDatObj(this, "987654_3.pdf", attachpath+"987654_3.pdf");var numPgs = this.numPages;
for (var jj=0; jj<numPgs; jj++) {
var numWords = this.getPageNumWords(jj);
for (var i=0; i<numWords; i++) {
var ckWord = this.getPageNthWord(jj, i, false);
if (ckWord.indexOf("pdf")>=0) {
ckWord = ckWord.substring(0, 3);
mm=i;
var spc=true;
do {
mm=mm-1;
ckWord=this.getPageNthWord(jj, mm, false)+ckWord;
if (mm>0) spc=(this.getPageNthWord(jj, mm-1, false).indexOf(" ")==-1); else if (mm==0) spc=true; else spc=false;
} while (spc);
}
if (ckWord=="abcdef_g.pdf") {
var m = (new Matrix2D).fromRotated(this,jj);
var mInv = m.invert();
var q = this.getPageNthWordQuads(jj, i);
var r = mInv.transform(q);
r = r.toString();
r = r.split(",");
var p = this.getPageNthWordQuads(jj, mm);
var s = mInv.transform(p);
s = s.toString();
s = s.split(",");
var l = addLink(jj, [s[4], r[5], r[2], s[3]]);
l.setAction("exportDataObject({cName: \"abcdef_g.pdf\", nLaunch: 2});");
}
}
}var numPgs = this.numPages;
for (var jj=0; jj<numPgs; jj++) {
var numWords = this.getPageNumWords(jj);
for (var i=0; i<numWords; i++) {
var ckWord = this.getPageNthWord(jj, i, false);
if (ckWord.indexOf("pdf")>=0) {
ckWord = ckWord.substring(0, 3);
mm=i;
var spc=true;
do {
mm=mm-1;
ckWord=this.getPageNthWord(jj, mm, false)+ckWord;
if (mm>0) spc=(this.getPageNthWord(jj, mm-1, false).indexOf(" ")==-1); else if (mm==0) spc=true; else spc=false;
} while (spc);
}
if (ckWord=="987654_3.pdf") {
var m = (new Matrix2D).fromRotated(this,jj);
var mInv = m.invert();
var q = this.getPageNthWordQuads(jj, i);
var r = mInv.transform(q);
r = r.toString();
r = r.split(",");
var p = this.getPageNthWordQuads(jj, mm);
var s = mInv.transform(p);
s = s.toString();
s = s.split(",");
var l = addLink(jj, [s[4], r[5], r[2], s[3]]);
l.setAction("exportDataObject({cName: \"987654_3.pdf\", nLaunch: 2});");
}
}
}
An error arises at the command: l.setAction("exportDataObject({cName: \"987654_3.pdf\", nLaunch: 2});");
Both documents are correclty uploaded into the attachment of the pdf form, the first text position is correctly linked.
Who can help?
I checked the whole loop in the JS debugger... The issue is not related to setAction, but to the for-loop. I assume that searching for a text (which will be linked) can be implemented in a more clever way.
Thanks anyhow!
Copiar vínculo al Portapapeles
Copiado
What is the full text of the error message? Also, are you getting it when you run this code, or when you click one of the links you're creating?
Copiar vínculo al Portapapeles
Copiado
I only get the error message when I create the links. There is no more text than "GeneralError: Operation failed. Root.(null):Number:Batch undefined: Exce script terminated.
BTW: The code creates both links (and both work!), but subsequent commands are interrupted and the error message points to the setAction command.
Copiar vínculo al Portapapeles
Copiado
You use the whole code in a folder-level JavaScript file?
Copiar vínculo al Portapapeles
Copiado
Yes, exactly. Actually I created a Sequence File (SEQU) with this content and further commands to add checkboxes to a stactic PDF file to make it a PDF form with attachments.
Copiar vínculo al Portapapeles
Copiado
A folder-level JavaScript file and a sequence file is not the same.
Code in folder-level JavaScript files will be executed at startup of Adobe Acrobat.
Copiar vínculo al Portapapeles
Copiado
So it is not a folder-level JavaScript but a script which is run as action (sequence).
Anyhow: How can I prevent the error when running the batch sequence? What could be the reason for this error?
Copiar vínculo al Portapapeles
Copiado
Did you get the error with any PDF file?
Copiar vínculo al Portapapeles
Copiado
This is the issue: The error occurs in about 80% of the cases, so not always.
BTW: Is it important to have a delay between import of the PDF file and the linking to the text?
If yes: How to implement the delay?
Copiar vínculo al Portapapeles
Copiado
What happens when you remove the import of the PDF files?
Copiar vínculo al Portapapeles
Copiado
The rest of the program runs always smoothly if I remove the PDF import and the linking to the PDF file.
Copiar vínculo al Portapapeles
Copiado
Remove only the PDF import.
Copiar vínculo al Portapapeles
Copiado
Same issue:
GeneralError: Operation failed.
Root.(null):3984:Batch undefined:Exec
script terminated
So the issue is not related to the PDF import.
Clicking the created link triggers (correctly) the error:
TypeError: Invalid argument type.
Doc.exportDataObject:1:Link:Page1:Annot14:Action1
===> Parameter cName.
Copiar vínculo al Portapapeles
Copiado
On how many files do you use the action?
Copiar vínculo al Portapapeles
Copiado
I use the action only on the specific open PDF file.
The complete batch file (which creates checkboxes and adds attachments) is quite large, i.e., processing of the whole file takes about 2 Minutes (file size of the batch as SEQU file is about 150kB).
Is there any type of limitation due to memory resources?
Copiar vínculo al Portapapeles
Copiado
Looks like a problem with setAction.
Copiar vínculo al Portapapeles
Copiado
I checked the whole loop in the JS debugger... The issue is not related to setAction, but to the for-loop. I assume that searching for a text (which will be linked) can be implemented in a more clever way.
Thanks anyhow!
Copiar vínculo al Portapapeles
Copiado
The reason for this may be that the method addLink changes the document.
Copiar vínculo al Portapapeles
Copiado
I think you're right. Your loop is very strange. I don't understand the whole do-while section of it and the use of the "spc" variable. I think you don't need it at all, if I understand correctly what you're trying to do.
And no, adding a link does not change the file in a way that should interfere with this loop, since you're not adding actual text.
Copiar vínculo al Portapapeles
Copiado
The reason for the do-while section is the command getPageNthWord. The command limits words by not only by a whitespace, but also e.g. by an underscore. Since I have to reference the whole filename, I have to find the complete string up to a real space char.
As the program "searches" the file extension, I have to step backwards through the text.
But again: This results from a routine which is not an optimal solution for my purpose. I rather have to search for the filename in the text, find the position in terms of "getPageNthWord". I might therefore analyze the search string to retrieve the number of "sub-words" (see remark above) and directly apply the routing to get the boundary box on the first and last sub-word.
The processing of the two cascaded for-loops (jj, i) takes most of the time (the two minutes are required for a 50 page document with 90% of text per page).
¡Prepárate! En enero llega una experiencia mejorada de Adobe Community.
Más información