Skip to main content
Participant
Oct 17, 2025 2:45 PM UTC
Question

Create QR code for each individual page in a document

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 = " ";
}

6 replies

Participant
Oct 27, 2025 11:19 AM UTC

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.

Translate
snavarAuthor
Participant
Oct 27, 2025 9:57 AM UTC

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 = " ";
}
Translate
Bernd Alheit
Community Expert
Community Expert
Oct 27, 2025 10:46 AM UTC

Fields with the same name will get always the same value.

Translate
Thom Parker
Community Expert
Community Expert
Oct 17, 2025 5:46 PM UTC

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.  

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
snavarAuthor
Participant
Oct 27, 2025 9:54 AM UTC

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.

Translate
Bernd Alheit
Community Expert
Community Expert
Oct 17, 2025 3:03 PM UTC

Check the JavaScript console for errors (ctrl-j).

Translate