Hi,
thanks for your answer. I will test it as soon as possible and will you inform. At the moment I have founed an other way to find special nodes with a certain text. Then I have splitted the document into parts. Afterward, when I want to merge all parts back to one document I always have a new section after every new document. Could you pleae tell where the problem is? Thanks a lot!
I make use of following code for mergin:
public Document mergeDocuments(List splittedDocuments){
Document res = null;
try {
res = createDoc();
} catch (Exception e) {
throw new DocumentMergerException("new document couldn’t been created",e);
}
for (Document splittedDocument : splittedDocuments) {
try {
appendAllNodes(res,splittedDocument.getChildNodes());
} catch (Exception e) {
throw new DocumentMergerException("couldn’t merge documents",e);
}
}
return res;
}
private Document createDoc() throws Exception {
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();
// This truly makes the document empty. No sections (not possible in
// Microsoft Word).
doc.removeAllChildren();
return doc;
}
private Document appendAllNodes(Document doc, NodeCollection nodes)
throws Exception {
for (Node node : nodes) {
appendNode(doc, node);
}
return doc;
}
private Document appendNode(Document doc, Node node) throws Exception {
Node importNode = doc.importNode(node, true);
doc.appendChild(importNode);
return doc;
}
-------------------------------------------------
And this is the code to create the splitted documents through the start end nodes:
public Document createDocumentByStartEndNode(Node startNode, Node endNode, boolean isInclusive) throws Exception {
ArrayList extractContent = extractContent(startNode, endNode, true);
Document generatedDocument = generateSplittedDocument(getDocument(), extractContent);
return generatedDocument;
}
public static Document generateSplittedDocument(Document srcDoc, ArrayList nodes) throws Exception {
// Create a blank document.
Document dstDoc = new Document();
// Remove the first paragraph from the empty document.
dstDoc.getFirstSection().getBody().removeAllChildren();
// Import each node from the list into the new document. Keep the
// original formatting of the node.
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
for (Node node : (Iterable) nodes) {
Node importNode = importer.importNode(node, true);
dstDoc.getFirstSection().getBody().appendChild(importNode);
}
// Return the generated document.
return dstDoc;
}
public static ArrayList extractContent(Node startNode, Node endNode, boolean isInclusive) {
// First check that the nodes passed to this method are valid for use.
verifyParameterNodes(startNode, endNode);
// Create a list to store the extracted nodes.
ArrayList nodes = new ArrayList();
// Keep a record of the original nodes passed to this method so we can
// split marker nodes if needed.
Node originalStartNode = startNode;
Node originalEndNode = endNode;
// Extract content based on block level nodes (paragraphs and tables).
// Traverse through parent nodes to find them.
// We will split the content of first and last nodes depending if the
// marker nodes are inline
while (startNode.getParentNode().getNodeType() != NodeType.BODY)
startNode = startNode.getParentNode();
while (endNode.getParentNode().getNodeType() != NodeType.BODY)
endNode = endNode.getParentNode();
boolean isExtracting = true;
boolean isStartingNode = true;
boolean isEndingNode;
// The current node we are extracting from the document.
Node currNode = startNode;
// Begin extracting content. Process all block level nodes and
// specifically split the first and last nodes when needed so paragraph
// formatting is retained.
// Method is little more complex than a regular extractor as we need to
// factor in extracting using inline nodes, fields, bookmarks etc as to
// make it really useful.
while (isExtracting) {
// Clone the current node and its children to obtain a copy.
CompositeNode cloneNode = (CompositeNode) currNode.deepClone(true);
isEndingNode = currNode.equals(endNode);
if (isStartingNode || isEndingNode) {
// We need to process each marker separately so pass it off to a
// separate method instead.
if (isStartingNode) {
processMarker(cloneNode, nodes, originalStartNode, isInclusive, isStartingNode, isEndingNode);
isStartingNode = false;
}
// Conditional needs to be separate as the block level start and
// end markers maybe the same node.
if (isEndingNode) {
processMarker(cloneNode, nodes, originalEndNode, isInclusive, isStartingNode, isEndingNode);
isExtracting = false;
}
} else
// Node is not a start or end marker, simply add the copy to the
// list.
nodes.add(cloneNode);
// Move to the next node and extract it. If next node is null that
// means the rest of the content is found in a different section.
if (currNode.getNextSibling() == null && isExtracting) {
// Move to the next section.
Section nextSection = (Section) currNode.getAncestor(NodeType.SECTION).getNextSibling();
currNode = nextSection.getBody().getFirstChild();
} else {
// Move to the next node in the body.
currNode = currNode.getNextSibling();
}
}
// Return the nodes between the node markers.
return nodes;
}
Best regards
Amin