Hi,
I have a template with some start and end mergefields. I have to remove the text in between these and add the text entered by the user in the client. I have attached the template for your reference. I am using paragraph.remove() method when I come across a paragraph between the mergefields that needs to be removed. But somehow after I do this, the aspose processing never goes to the next mergefield and stops processing. Currently, I am using paragraph.removeAllChildren() to remove content(runs) within the start and end mergefields. But this leaves a blank line as it does not remove the paragraph completely.
I think the issue has to do with Paragraph break character. Whenever the paragraph ends with a ControlChar.CR, and I use paragraph.remove(), it does not process the document further.
I have attached the ideal resulting and actual resulting document. Please notice the extra blank lines in the actual resulting document.
I have the following code:
public int visitFieldStart(FieldStart fieldStart) throws Exception {
String fieldCode = getFieldCode(fieldStart.getField());
if (fieldCode.equals("NoteStart")) {
// just move to end mergefield
documentBuilder.moveToMergeField("NoteEnd",false,false);
// get current node
Node end = documentBuilder.getCurrentNode();
removeSequence(fieldStart, end);
// insert the user note
documentBuilder.insertHtml(notesList.get(currentIndex));
currentIndex++;
// move to mergefield and remove it
documentBuilder.moveToMergeField("NoteEnd");
}
return VisitorAction.CONTINUE;
}
public void removeSequence(Node start, Node end) throws Exception {
Node curNode = start;
boolean isFieldEnd = true;
// Traverse through all the nodes from FieldStart till FieldEnd
// of User note start tag to reach the node after the field end
while (curNode != null && isFieldEnd) {
Node nextNode = curNode.nextPreOrder(curNode.getDocument());
if (nextNode.getNodeType() == NodeType.FIELD_END) {
isFieldEnd = false;
}
curNode = nextNode;
}
// Get the next node after the FieldEnd
if (curNode != null) {
curNode = curNode.nextPreOrder(start.getDocument());
}
// Remove nodes till the end node is reached
while (curNode != null && !curNode.equals(end)) {
// Move to next node
Node nextNode = curNode.nextPreOrder(start.getDocument());
// Check whether current contains end node
if (curNode.isComposite()) {
CompositeNode curComposite = (CompositeNode) curNode;
if (!curComposite.getChildNodes().contains(end) &&
!curComposite.getChildNodes().contains(start)) {
nextNode = curNode.getNextSibling();
if (!curNode.getText().equals(ControlChar.CR)) {
// ideally cureNode.remove should work here, but it
// does not go to the next visitFieldStart for the next mergefield
((CompositeNode) curNode).removeAllChildren();
}
}
} else {
curNode.remove();
}
curNode = nextNode;
}
}
/**
* Called by Aspose when it encounters a Field end
*
* @param fieldEnd FieldEnd for a field
* @return VisitorAction.CONTINUE
* @throws Exception
*/
public int visitFieldEnd(FieldEnd fieldEnd) throws Exception
{
String fieldCode = getFieldCode(fieldEnd.getField());
if (fieldCode.equals("NoteStart")) {
// remove the merge field for start tag
documentBuilder.moveToMergeField("NoteStart");
}
return VisitorAction.CONTINUE;
}
please let me know if there is a solution around this issue.
Thanks!!