Hi,
Hi
Thanks for your request. I cannot reproduce the problem on my side. I tested with dummy data and as I can see IF, NEXT, NEXTIF fields used in your template works as expected.
Could you please provide us a code that will allow us to reproduce the problem? We will check the issue and provide you more information.
Best regards,
Hi Alexey,
Hi Rajesh,
Hi awais,
Hi
Thanks for your request. Here is the same code in Java:
//Open document
Document doc = new Document("C:\\Temp\\in.doc");
//Create DocuentBuilder. It will help us to modify fields
DocumentBuilder builder = new DocumentBuilder(doc);
//Get collection of FieldStart nodes
NodeCollection<FieldStart> starts = doc.getChildNodes(NodeType.FIELD_START, true);
//loop through all field starts and search for FILLIN fields
for (FieldStart start : starts)
{
if (start.getFieldType() == FieldType.FIELD_FILL_IN)
{
//Every field in MS Word document consists of FieldStart, FieldSeparator, FieldEnd nodes
//And Runs that represent field code and field value (displayed text)
//If you need to change value of fillin field, you should change field code
//And field value
Node fieldSeparator;
Node fieldEnd;
//We should get field code and field value
String fieldCode = "";
String fieldValue = "";
Node currentNode = start.getNextSibling();
//Get Field code
while (currentNode.getNodeType() != NodeType.FIELD_SEPARATOR)
{
if (currentNode.getNodeType() == NodeType.RUN)
fieldCode += ((Run)currentNode).getText();
currentNode = currentNode.getNextSibling();
currentNode.getPreviousSibling().remove();
}
fieldSeparator = currentNode;
currentNode = currentNode.getNextSibling();
//Get field value
while (currentNode.getNodeType() != NodeType.FIELD_END)
{
if (currentNode.getNodeType() == NodeType.RUN)
fieldValue += ((Run)currentNode).getText();
currentNode = currentNode.getNextSibling();
currentNode.getPreviousSibling().remove();
}
fieldEnd = currentNode;
//We should get question and answer from fillin field code
Pattern regex = Pattern.compile("\\s*FILLIN\\s+\"([^\"]+)\"\\s+\\\\d\\s+\"([^\"]+)\"");
Matcher matcher = regex.matcher(fieldCode);
matcher.find();
System.out.println(fieldCode);
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
//Change field code and field value
if (matcher.group(1).equals("What is your name?"))
{
fieldCode = fieldCode.replace(matcher.group(2), "Alexey");
fieldValue = "Alexey";
}
//Insert new field code and field value
builder.moveTo(fieldSeparator);
builder.write(fieldCode);
builder.moveTo(fieldEnd);
builder.write(fieldValue);
}
}
//save output document
doc.save("C:\\Temp\\out.doc");
Hope this helps.
Best regards,
Hi,
Hi
Rajesh,