Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Create QR code for each individual page in a document

New Here ,
Oct 17, 2025 Oct 17, 2025

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 = " ";
}
TOPICS
General troubleshooting , JavaScript , PDF forms
154
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 17, 2025 Oct 17, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 17, 2025 Oct 17, 2025

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.  

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2025 Oct 27, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 27, 2025 Oct 27, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2025 Oct 27, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 27, 2025 Oct 27, 2025
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines