We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Drag and drop merge fields from java



Hi there,

I was wondering if Aspose could be use to do some drag and drop over an open document. I Would like to drag a merge field from java, and drop it over word on a given document.
It seems this could be done only if the drag context contains some word content, not only text, and I guess Aspose manage this content as needed.

Best regards,
rv

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Actually MergeField consists of several nodes (FieldStart, FieldSeparator, FieldEnd and several Runs). If you need to move MergeField you should move all nodes between FieldStart and FieldEnd nodes (including FieldStart and FieldEnd of course). Here is code example how you can achieve this.

//Open document

Document doc = new Document("in.doc");

//Get collection of field starts

NodeCollection starts = doc.getChildNodes(NodeType.FIELD_START, true);

//For exampel we woold like to move the first mergefield in document

FieldStart mergefieldStart = null;

//Search for first mergefield start

for (int i = 0; i < starts.getCount(); i++)

{

FieldStart currentStart = (FieldStart)starts.get(i);

if (currentStart.getFieldType() == FieldType.FIELD_MERGE_FIELD)

{

mergefieldStart = currentStart;

break;

}

}

if (mergefieldStart != null)

{

//We should copy all nodes between FieldStart and FieldEnd

ArrayList nodesList = new ArrayList();

//Write all nodes between start and end into ArrayList

Node currentNode = mergefieldStart;

while (currentNode.getNodeType() != NodeType.FIELD_END)

{

nodesList.add(currentNode);

currentNode = currentNode.getNextSibling();

}

nodesList.add(currentNode);

//Now we would like to move our mergefield to the end of document

for (int nodeIndex = 0; nodeIndex < nodesList.size(); nodeIndex++)

{

doc.getLastSection().getBody().getLastParagraph().appendChild((Node)nodesList.get(nodeIndex));

}

}

//Save document

doc.save("out.doc");

Hope this helps.

Best regards.