Copy link to clipboard
Copied
I have a multi-page pdf template and I need to insert QR codes that encode the version of the document and the page. The version will be in a form field, so I was looking to use Javascript to encode these QR codes. However I can't get the code to pick the page number. Can you help me please?
Here is the code for the QR code field:
/* Customize: */
function bMemberOf(strName, aStrNames)
{
for (var nMembIdx in aStrNames)
{
if (strName == aStrNames[nMembIdx])
return true;
}
return false;
}
function strTabDelimited(oParam)
{
var bNeedTab = false;
var strNames = "";
var strValues = "";
for (var i = 0; i < oParam.oDoc.numFields; ++i)
{
var strFieldName = oParam.oDoc.getNthFieldName(i);
if ((null == oParam.aFields || bMemberOf(strFieldName, oParam.aFields))
&& (null == oParam.strXclField || strFieldName != oParam.strXclField)
&& (oParam.oDoc.getField(strFieldName).type != "button"))
{
if (bNeedTab)
{
if (oParam.bFieldNames)
strNames += "\t";
strValues += "\t";
}
if (oParam.bFieldNames)
strNames += strFieldName;
strValues += oParam.oDoc.getField(strFieldName).valueAsString;
bNeedTab = true;
}
}
if (oParam.bFieldNames)
return strNames + "\n" + strValues;
else
return "Document version " + strValues + " Page " currentPage;
}
try
{
if ( app.viewerVersion >= ADBE.PMD_Need_Version )
event.value = strTabDelimited({oDoc: this, aFields: ["Version"], bFieldNames: false});
else event.value = " ";
}
catch (e)
{
event.value = " ";
}
Copy link to clipboard
Copied
Check the JavaScript console for errors (ctrl-j).
Copy link to clipboard
Copied
There are errors in the code. So yes, checking the consoles is always the first thing to do when developing code for Acrobat.
First, the "+" is missing from in front of the "currentPage" variable, which is also undefined. So that's two errors.
Is this script a custom calculation for the barcode field? Do you manually place barcode fields on each page? If this is the case, then there is a much more efficient way to do this.
Copy link to clipboard
Copied
Yes, I'm doing the script for the barcode field. I was hoping that duplicating this field on every page would do the trick. However if there is a better way to do this I'm intersted in knowing how.
Copy link to clipboard
Copied
EDIT:
I have managed to make the code work , but the issue now is that when I duplicate it in all pages the page number is always the same.
/* Customize: */
function bMemberOf(strName, aStrNames)
{
for (var nMembIdx in aStrNames)
{
if (strName == aStrNames[nMembIdx])
return true;
}
return false;
}
function strTabDelimited(oParam)
{
var bNeedTab = false;
var strNames = "";
var strValues = "";
for (var i = 0; i < oParam.oDoc.numFields; ++i)
{
var strFieldName = oParam.oDoc.getNthFieldName(i);
if ((null == oParam.aFields || bMemberOf(strFieldName, oParam.aFields))
&& (null == oParam.strXclField || strFieldName != oParam.strXclField)
&& (oParam.oDoc.getField(strFieldName).type != "button"))
{
if (bNeedTab)
{
if (oParam.bFieldNames)
strNames += "\t";
strValues += "\t";
}
if (oParam.bFieldNames)
strNames += strFieldName;
strValues += oParam.oDoc.getField(strFieldName).value;
bNeedTab = true;
}
}
if (oParam.bFieldNames)
return strNames + "\n" + strValues;
else
return "Doc version " + strValues + " page " + this.pageNum+1;
}
try
{
if ( app.viewerVersion >= ADBE.PMD_Need_Version )
event.value = strTabDelimited({oDoc: this, aFields: ["Version"], bFieldNames: false});
else event.value = " ";
}
catch (e)
{
event.value = " ";
}
Copy link to clipboard
Copied
Fields with the same name will get always the same value.
Copy link to clipboard
Copied
You're very close. The currentPage variable is undefined in your script.
The property you're looking for is this.pageNum, which gives the 0-based index of the page the field is currently on. You can access this directly inside your function.
Try modifying your function's return statement to this:
return "Document version " + strValues + " Page " + (this.pageNum + 1);
Using + 1 will give you a 1-based page number (e.g., "Page 1" instead of "Page 0"), which is usually more intuitive for a QR code. That should solve it.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now