HTML2PDF Radio Buttons Not Generating as Form Fields

Product: PDFTron .NET x64

Product Version: 9.1.0

HTML2PDF Radio Buttons Not Generating as Form Fields

I am using the HTML2PDF module to turn an HTML form into a PDF form. While most form inputs are rendered as expected, I’m not able to succesfully convert radio buttons.

For example, I would expect the markup below to generate a PDF form field of type “e_radio” with the name “Radio_Buttons”, but the resulting PDF document has no field with either that name or type.

The PDF form displays empty radio buttons, but they cannot be selected or toggled.

<div>
    <label>
        <input type="radio" class="lifesuite__radio" name="Radio_Buttons" value="">
        &nbsp;
        <span></span>
    </label>
</div>
<div>
    <label>
        <input type="radio" class="lifesuite__radio" name="Radio_Buttons" value="Dr">
        &nbsp;
        <span>Dr</span>
    </label>
</div>
<div>
    <label>
        <input type="radio" class="lifesuite__radio" name="Radio_Buttons" value="Miss">
        &nbsp;
        <span>Miss</span>
    </label>
</div>
<div>
    <label>
        <input type="radio" class="lifesuite__radio" name="Radio_Buttons" value="Mr">
        &nbsp;
        <span>Mr</span>
    </label>
</div>

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

At this time there is no official support for converting HTML forms to PDF forms. The HTML2PDF module utilizes 3rd party software which may have provided some sort of forms conversion by default in the latest releases.

Is this for a new feature/requirement? If not, what issues do you have with your current solution?
Could you add the Forms programmatically afterwards using our APIs?

Thanks, @Ryan .

I’ve realized that PDFTron is using the wkhtmltopdf dll, and have found that this is an issue that has gone unaddressed for a long time: radio buttons not rendered as input fields in pdf · Issue #1583 · wkhtmltopdf/wkhtmltopdf · GitHub.

I have found a workaround, for anyone who’s curious. You can render radio buttons in the HTML as checkboxes with radio button styling, and the PDF will have the appropriate display:

<div>
    <label>
        <input type="checkbox" name="Radio_Buttons_" value="" style="-webkit-appearance: radio;-moz-appearance: radio;appearance: radio">
        &nbsp;
        <span></span>
    </label>
</div>
<div>
    <label>
        <input type="checkbox" name="Radio_Buttons_Dr" value="Dr" style="-webkit-appearance: radio;-moz-appearance: radio;appearance: radio">
        &nbsp;
        <span>Dr</span>
    </label>
</div>
<div>
    <label>
        <input type="checkbox" name="Radio_Buttons_Miss" value="Miss" style="-webkit-appearance: radio;-moz-appearance: radio;appearance: radio">
        &nbsp;
        <span>Miss</span>
    </label>
</div>

Use this approach with caution, as the resulting fields will not behave like radio buttons, but could, I’m certain, be converted by those a little more PDFTron savvy than myself. In our case however (generating PDF templates that we then fill out programmatically based on controlled input), this is not a problem.

The one thing you could help me with, @Ryan , is getting the appearance of this pseudo-radio button to be correct (that is, to populate with a filled circle).

I have the following code for setting the value and state of these pseudo-radio buttons based on a list of string values:

foreach (string val in strVals) {
    if (!string.IsNullOrWhiteSpace(val)) {
        Field valField = srcPdfDoc.GetField($"{strName}_{val}");
        if (valField != null) {
            valField.SetValue(GetOnState(valField));
            var widget = CheckBoxWidget.Create(srcPdfDoc, valField.GetUpdateRect());
            widget.SetAppearance(CreateCheckmarkAppearance(srcPdfDoc, valField.GetUpdateRect()));
            widget.SetChecked(true);
            srcPdfDoc.GetPage(1).AnnotPushBack(widget);
        }
    }
}

And I have modified the CreateCheckmarkAppearance() function as such:

    Obj CreateCheckmarkAppearance(PDFDoc doc, Rect updateRectangle) {
        // Create a checkmark appearance stream
        ElementBuilder build = new ElementBuilder();
        ElementWriter writer = new ElementWriter();
        writer.Begin(doc);

        // Draw background
        Element e = build.CreateRect(updateRectangle.x1, updateRectangle.y1, updateRectangle.Width(), updateRectangle.Height());
        e.SetPathFill(true); // this path is should be filled

        // Set the path color space and color
        GState gstate = e.GetGState();
        gstate.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
        gstate.SetFillColor(new ColorPt(0, 0, 1)); // blue
        writer.WritePlacedElement(e);

        // Draw checkmark
        writer.WriteElement(build.CreateTextBegin());

        // other options are circle ("l"), diamond ("H"), cross ("\x35")
        // See section D.4 "ZapfDingbats Set and Encoding" in PDF Reference
        //Manual for
            // the complete graphical map for ZapfDingbats font.
            e = build.CreateTextRun("1", Font.Create(doc,
        Font.StandardType1Font.e_zapf_dingbats), 1);
            writer.WriteElement(e);
            writer.WriteElement(build.CreateTextEnd());

        Obj stm = writer.End();

        // Set the bounding box
        stm.PutRect("BBox", updateRectangle.x1, updateRectangle.y1, updateRectangle.x2, updateRectangle.y2);
        stm.PutName("Subtype", "Form");

        return stm;
    }

However, I still get the default checked appearance. What am I doing wrong here?