Leonardo Neninger
Leonardo Neninger

Reputation: 586

PDFTron WebViewer: Set the Comb flag to a text field that appears multiple times

I'm loading a PDF document and I need to be sure that each field whose name has a certain pattern gets converted to a COMB field. Happens that a field can be duplicated, meaning that is possible to find another field with the same.

For each field...

const fieldChanged = await PDFNet.runWithCleanup(() => combFieldAdjustmentCallBack(pdfField, configuredMaxLen), this.configurationService.webViewerPDFTronKey);

combFieldAdjustmentCallBack implementation

      const combFieldAdjustmentCallBack = async (pdfField: Core.Annotations.Forms.Field, 
         configuredMaxLen: number) => {
            let fieldChanged: boolean;
            const itr = await pdfDoc.getFieldIterator(pdfField.name);
            for (; await itr.hasNext(); await itr.next()) {

                const docField = await itr.current();

                // if the field has not set the flag comb; set it and mark the field as changed
                const comb = await docField.getFlag(PDFNet.Field.Flag.e_comb);
                if (!comb) {
                    await docField.setFlag(PDFNet.Field.Flag.e_multiline, false);
                    await docField.setFlag(PDFNet.Field.Flag.e_password, false);
                    await docField.setFlag(PDFNet.Field.Flag.e_no_spellcheck, true);
                    await docField.setFlag(PDFNet.Field.Flag.e_no_scroll, true);
                    await docField.setFlag(PDFNet.Field.Flag.e_rich_text, false);

                    await docField.setFlag(PDFNet.Field.Flag.e_comb, true);

                    const comb = await docField.getFlag(PDFNet.Field.Flag.e_comb);

                    //  Always "Field XYZ is a comb field: false"
                    console.log(`Field ${pdfField.name} is a comb field: ${comb}`);

                    fieldChanged = true;
                }

                // Set max length
                const currentMaxLength = await docField.getMaxLen();
                if (configuredMaxLen != currentMaxLength) {
                    await docField.setMaxLen(configuredMaxLen);
                    fieldChanged = true;
                }

                if (fieldChanged) {
                    docField.refreshAppearance();
                }

                return fieldChanged ?? false;
            }
        };

This logic works when the field happens only once, but doesn't work when the field repeats across the PDF. Please, note Always "Field XYZ is a comb field: false

What I'm missing

Upvotes: 0

Views: 118

Answers (2)

Leonardo Neninger
Leonardo Neninger

Reputation: 586

The code was correct from the beginning.

After some tests we observed the follow behaviors:

  1. One one field in the PDF fail to set the COMB flag.
  2. The other matching fields in the PDF were successfully converted to COMB at some point previous to the discovery of this problem.
  3. The matching fields in other PDF files where successfully converted to COMB.

Conclusion

So base on our tests we concluded that something was wrong with that particular field in that particular file. Creating another version of the file resolved the problem.

Upvotes: 0

TylerG
TylerG

Reputation: 1

It looks like you are trying to redefine a const variable, therefore if it is set to false it will always be false.

Best regards Tyler

Upvotes: 0

Related Questions