Hello, I am creating a report where the content is created in HTML and then the insertHtml method is used and it is being saved as a docx. Part of the html are checkboxes. In HTML they look like this:
<input _ngcontent-dtp-c596="" type="checkbox" disabled="" checked="" title="on">
<input _ngcontent-knj-c501="" type="checkbox" disabled="" title="on" class="ng-star-inserted">
When this HTML is imported into the word doc it is rendering not as a checkbox but as text that looks like this: “{ FORMCHECKBOX }”
Is there a way to avoid this? I think it started after we upgraded from an older version. (current version is 25.5)
To add to this, we do not want it to be an editable form field, we want it to just be a checkbox visual checked or unchecked. Before this displayed fine.
Thanks.
@martincoty77
To ensure checkboxes in your HTML are rendered correctly in a Word document when using the insertHtml
method, you may need to take a different approach because standard HTML checkboxes often get converted to form field placeholders in Word, such as “{ FORMCHECKBOX }”.
One effective way to handle this is to utilize the Mail Merge feature of Aspose.Words to insert checkboxes directly into your document. Instead of relying on HTML input elements, you can replace the HTML checkboxes with appropriate merge fields in your Word document. Here’s how you can do it:
-
Prepare your Word template: In your Word document, create merge fields for the checkboxes you need. For instance, you can create a merge field named CheckboxField
.
-
Use the IFieldMergingCallback
interface: Implement this interface to handle the merging of fields. Specifically, for boolean values, you can insert a checkbox form field in place of the merge field.
Here’s a code example that demonstrates how to do this:
Document doc = new Document("Template.docx");
doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[] {"CheckboxField"}, new Object[] {true}); // true or false based on your logic
doc.save("Report.docx");
private static class HandleMergeField implements IFieldMergingCallback {
private DocumentBuilder mBuilder;
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (mBuilder == null) {
mBuilder = new DocumentBuilder(e.getDocument());
}
if (e.getFieldValue() instanceof Boolean) {
mBuilder.moveToMergeField(e.getFieldName());
String checkBoxName = e.getFieldName() + e.getRecordIndex();
mBuilder.insertCheckBox(checkBoxName, (Boolean) e.getFieldValue(), 0);
}
}
public void imageFieldMerging(ImageFieldMergingArgs args) {
// Handle image fields if necessary
}
}
In this implementation:
- When the merge field for the checkbox is encountered, the code checks if the value is a boolean. If it is, it inserts a checkbox at the position of the merge field.
- Modify the
execute
method parameters based on your actual data.
This method allows you to have full control over how checkboxes are rendered in the final Word document, ensuring they appear as intended rather than as placeholder text.
For further reading on inserting checkboxes and other form fields during a mail merge, you can refer to the official documentation on this topic here.
Sources:
[1]: Insert Checkboxes, Text Input or Images during Mail Merge
@martincoty77 Unfortunately, there is no way to import input controls as simple text, you can import them either as form fields or as content controls. Please see HtmlLoadOptions.PreferredControlType property.
However, you can easily replace form field checkboxes with the appropriate Windings symbols:
Document doc = new Document(@"C:\Temp\in.html");
DocumentBuilder builder = new DocumentBuilder(doc);
List<FormField> checkBoxes = doc.Range.FormFields.Where(ff => ff.Type == FieldType.FieldFormCheckBox).ToList();
foreach (FormField ff in checkBoxes)
{
Node current = ff;
while (current.NodeType != NodeType.FieldStart && current != null)
current = current.PreviousSibling;
if (current != null)
{
Field f = ((FieldStart)current).GetField();
builder.MoveToField(f, true);
builder.Font.Name = "Wingdings";
builder.Write(ff.Checked ? "\u00FE" : "\u00A8");
f.Remove();
}
}
doc.Save(@"C:\Temp\out.docx");
Alexey,
Thank You, the HtmlLoadOptions.PreferredControlType worked perfectly from the link you posted.
Quick question. For the scenario where you would replace the input fields with wingdings, is this the approach you would take if you had only certain sections that were html? For the reports I am currently doing, the entire content of the form is Html, except the header and the footer.
Thank You,
Coty
@martincoty77 The code replaces all form field checkboxes with the appropriate Windings symbols. There is no difference from where the form field comes, from HTML or from Word document, in the Aspose.Words DOM these form fields are the same.