How to fill checkbox in pdfium

129 views
Skip to first unread message

Muppalla Mounika

unread,
Nov 1, 2024, 11:50:24 AM11/1/24
to pdfium

I am trying to enable check using pdfium.

Below is my code to enable checkbox in pdfium acroform.

 int pageCount = FPDF_GetPageCount(doc);
    for (int i = 0; i < pageCount; ++i) {
        FPDF_PAGE page = FPDF_LoadPage(doc, i);
        FORM_OnAfterLoadPage(page, form); // Must call after loading the page
        FORM_DoPageAAction(page, form,FPDFPAGE_AACTION_OPEN); // Trigger any page actions

        int annotCount = FPDFPage_GetAnnotCount(page);
        for (int j = 0; j < annotCount; ++j) {
            FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, j);
            if (FPDFAnnot_GetSubtype(annot) == FPDF_ANNOT_WIDGET) {
                int fieldType = FPDFAnnot_GetFormFieldType(form, annot);
                if (fieldType == FPDF_FORMFIELD_CHECKBOX) {
                    FPDFAnnot_SetStringValue(annot, "V", GetFPDFWideString("Yes"));
                    FPDFAnnot_SetAP(annot, FPDF_ANNOT_APPEARANCEMODE_NORMAL, nullptr);
                }
            }

            FPDFPage_CloseAnnot(annot);
        }
        FORM_OnBeforeClosePage(page, form);
        FPDF_ClosePage(page);
    }

On editing PDF & checking details, I see the dictionary value is expected in format. How to achieve it? Also How to set appearance stream along with set value? 

/V /Off

 /V /Yes

Justin Pierce

unread,
Nov 1, 2024, 12:07:42 PM11/1/24
to Muppalla Mounika, pdfium
You will have better luck with `FORM_OnLButtonDown` to emulate a click


From: pdf...@googlegroups.com <pdf...@googlegroups.com> on behalf of Muppalla Mounika <muppall...@gmail.com>
Sent: Friday, November 1, 2024 8:41:21 PM
To: pdfium <pdf...@googlegroups.com>
Subject: How to fill checkbox in pdfium
 
--
You received this message because you are subscribed to the Google Groups "pdfium" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pdfium+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pdfium/154d456c-cb3c-42bc-aec1-3c04baafe671n%40googlegroups.com.

Saveliy Vasilev

unread,
Mar 18, 2025, 3:44:26 PM3/18/25
to pdfium
It seems that it's not possible with the current public pdfium interface.

According to the manual (pp. 615, https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/pdfreference1.7old.pdf); the way to check/uncheck a checkbox is to set /AS value (in annot dict) to match the dict entry in /AP -> /N; which in turn defines how a checked / unchecked box looks like. Apparently, the /Off value is the default for an unselected checkbox option (in /AP -> /N). Yet, there is no standard on how the checked dict entry should be called: I've seen different names within the same form multiple times. This means that the way to set the checkbox implies the following steps:
- Get the /Off and /<something else> options from the /AP -> /N dict from the annot
- Set the /AS to match <something else>

In fact, this is exactly what is done by the C++ code, `CPDFSDK_Widget::SetCheck(bool bChecked)` is a function (not public) that given a checkbox / radio button widget sets the value to `bChecked`. Indeed, the function calls the following that does the job:

void CPDF_FormControl::CheckControl(bool bChecked) {
  DCHECK(GetType() == CPDF_FormField::kCheckBox ||
         GetType() == CPDF_FormField::kRadioButton);
  ByteString csOldAS = m_pWidgetDict->GetByteStringFor("AS", "Off");
  ByteString csAS = "Off";
  if (bChecked)
    csAS = GetOnStateName();
  if (csOldAS == csAS)
    return;
  m_pWidgetDict->SetNewFor<CPDF_Name>("AS", csAS);
}

All this being said, we don't have any exported function that would set the CPDF_Name for a dict. We can only set CPDF_String values, which wraps the string in parenthesis which signals the PDF that it should be treated as a string and not as dict value.

If time allows, I might open a CL to pdfium so we can have some function as FPDFAnnot_SetChecked function for this case. I managed to get it working but I'm missing the tests and it would require compiling pdfium and not relying on the official binaries.

All the best!

Justin Pierce

unread,
Mar 18, 2025, 11:22:41 PM3/18/25
to Saveliy Vasilev, pdfium
It is possible, albeit a bit cumbersome. You can use the form filler API to toggle the value of a checkbox:

FPDFAnnot_GetRect();

FORM_OnLButtonDown();

FORM_OnLButtonUp();
From: pdf...@googlegroups.com <pdf...@googlegroups.com> on behalf of Saveliy Vasilev <sav...@tukki.ai>
Sent: Wednesday, March 19, 2025 2:43:29 AM
To: pdfium <pdf...@googlegroups.com>
Subject: Re: How to fill checkbox in pdfium
 
Reply all
Reply to author
Forward
0 new messages