Hi,
I want to replace all checkboxes in a document with StructuredDocumentTag checkboxes. Unfortunately these do not appear or do not appear at the desired position. How can I do this? The checkboxes can also be in the middle of a paragraph.
First attemp, new checkboxes are not displayed:
FormFieldCollection formFields = WordDoc.Range.FormFields;
foreach (FormField field in formFields)
{
if (field.Type == (FieldType)TypeToConvert)
{
if (field.Type == FieldType.FieldFormCheckBox)
{
StructuredDocumentTag SdtCheckBox = new StructuredDocumentTag(WordDoc, SdtType.Checkbox, MarkupLevel.Inline)
{
Checked = field.Checked,
Tag = !string.IsNullOrWhiteSpace(field.Name) ? field.Name : “”
};
SdtCheckBox.ContentsFont.Size = field.Font.Size;
SdtCheckBox.ContentsFont.Bold = field.Font.Bold;
field.ParentNode.InsertAfter(SdtCheckBox, field);
field.Remove();
}
}
}
Second try, builder inserts on wrong place:
FormFieldCollection formFields = WordDoc.Range.FormFields;
foreach (FormField field in formFields)
{
if (field.Type == (FieldType)TypeToConvert)
{
if (field.Type == FieldType.FieldFormCheckBox)
{
StructuredDocumentTag SdtCheckBox = new StructuredDocumentTag(WordDoc, SdtType.Checkbox, MarkupLevel.Inline)
{
Checked = field.Checked,
Tag = !string.IsNullOrWhiteSpace(field.Name) ? field.Name : “”
};
SdtCheckBox.ContentsFont.Size = field.Font.Size;
SdtCheckBox.ContentsFont.Bold = field.Font.Bold;
builder.MoveTo(field.ParentNode);
builder.InsertNode(SdtCheckBox);
field.Remove();
retVal = retVal == -1 ? 1 : retVal++;
}
}
}
@MarkusPlesoft
Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.
Sure, created a test document. Test.dotx is the template I edit, Test.docx is the result.
Test.zip (18.8 KB)
@MarkusPlesoft
Please use the following code example to convert FormField to content control of type checkbox. Hope this helps you.
Document doc = new Document(MyDir + "Test.dotx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (FieldStart fieldStart in doc.GetChildNodes(NodeType.FieldStart, true))
{
if (fieldStart.GetField().Type == FieldType.FieldFormCheckBox)
{
builder.MoveTo(fieldStart);
StructuredDocumentTag structuredDocumentTag = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
builder.InsertNode(structuredDocumentTag);
fieldStart.GetField().Remove();
}
}
doc.Save(MyDir + "19.9.docx");
Hi tahir.manzoor,
thank you for your suggestion. With this version I don’t have access to the properties of the previous checkbox. Therefore I have modified the code a bit:
FormFieldCollection formFields = WordDoc.Range.FormFields;
foreach (FormField field in formFields)
{
if (field.Type == FieldType.FieldFormCheckBox)
{
StructuredDocumentTag SdtCheckBox = new StructuredDocumentTag(WordDoc, SdtType.Checkbox, MarkupLevel.Inline)
{
Checked = field.Checked,
Tag = !string.IsNullOrWhiteSpace(field.Name) ? field.Name : ""
};
SdtCheckBox.ContentsFont.Size = field.Font.Size;
SdtCheckBox.ContentsFont.Bold = field.Font.Bold;
Node startNode = field;
while (startNode != null && startNode.NodeType != NodeType.FieldStart)
{
startNode = startNode.PreviousSibling;
}
if (startNode.NodeType == NodeType.FieldStart)
{
builder.MoveTo(startNode);
field.Remove();
builder.InsertNode(SdtCheckBox);
}
}
}
@MarkusPlesoft
It is nice to hear from you that you have fixed your issue. Please let us know if you face any issue while using Aspose.Words.