Free Support Forum - aspose.com

Content not inserted in destination paragraph

Hello,

when inserting content from a document to a mergefield in a paragraph, the content is added outside the mergefield??

I added the code here under.The original doc,the output doc and the doc to be inserted in attachment.

tmpDoc.save("c:/temp/tmp.doc");

DocumentBuilder docBuilder = new DocumentBuilder(doc);

docBuilder.moveToMergeField(strFieldName);

insertDocumentContent(docBuilder.getCurrentParagraph(), tmpDoc);

tmpDoc = null;

doc.save("c:/temp/processed.doc");

------------------------------

public StringBuffer insertDocumentContent(Node insertAfterNode, Document srcDoc) throws Exception

{

StringBuffer txtContent = new StringBuffer();

// We need to make sure that the specified node is either pargraph or

// table.

if (!((insertAfterNode.getNodeType() == NodeType.PARAGRAPH) ||

(insertAfterNode.getNodeType() == NodeType.TABLE)))

throw new IllegalArgumentException("The destination node should be either paragraph or table.");

// We will be inserting into the parent of the destination paragraph.

CompositeNode dstStory = insertAfterNode.getParentNode();

// This object will be translating styles and lists during the import.

NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.USE_DESTINATION_STYLES);

// Loop through all sections in the source document.

Sections sections = srcDoc.getSections();

int nbSections = sections.getCount();

for (int i = 0; i < nbSections; i++)

{

// Loop through all block level nodes (paragraphs and tables) in the

// body of the section.

Section section = sections.get(i);

Body sectionBody = section.getBody();

NodeCollection childNodes = sectionBody.getChildNodes();

int nbChildNodes = childNodes.getCount();

for (int j = 0; j < nbChildNodes; j++)

{

// Do not insert node if it is a last empty paragarph in the

// section.

Node srcNode = childNodes.get(j);

if (srcNode.getNodeType() == NodeType.PARAGRAPH) {

Paragraph para = (Paragraph) srcNode;

if ((para != null) && para.isEndOfSection() && !para.hasChildNodes())

break;

}

// This creates a clone of the node, suitable for insertion into

// the destination document.

Node newNode = importer.importNode(srcNode, true);

// Insert new node after the reference node.

dstStory.insertAfter(newNode, insertAfterNode);

txtContent.append(newNode.getText());

insertAfterNode = newNode;

}

}

return txtContent;

}

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

Thanks for your request. This occurs because your method inserts document after paragraph. I think you can try using the following method.

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

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

InsertDocumentAtMergeField("test", doc, tmpDoc);

tmpDoc = null;

doc.save("processed.doc");

public static void InsertDocumentAtMergeField(String mergeFieldName, Document dstDoc, Document srcDoc) throws Exception

{

//Create DocumentBuilder

DocumentBuilder builder = new DocumentBuilder(dstDoc);

//Move cursor to bookmark and insert paragraph break

builder.moveToMergeField(mergeFieldName);

builder.writeln();

//Content of srcdoc will be inserted after this node

Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling();

//Content of first paragraph of srcDoc will be apended to this parafraph

Paragraph insertAfterParagraph = (Paragraph)insertAfterNode;

//Content of last paragraph of srcDoc will be apended to this parafraph

Paragraph insertBeforeParagraph = builder.getCurrentParagraph();

//We will be inserting into the parent of the destination paragraph.

CompositeNode dstStory = insertAfterNode.getParentNode();

//Remove empty paragraphs from the end of document

while (!srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes())

{

srcDoc.getLastSection().getBody().getLastParagraph().remove();

}

//Loop through all sections in the source document.

int sectCount = srcDoc.getSections().getCount();

for(int sectIndex=0; sectIndex<sectCount; sectIndex++)

{

Section srcSection = srcDoc.getSections().get(sectIndex);

//Loop through all block level nodes (paragraphs and tables) in the body of the section.

int nodeCount = srcSection.getBody().getChildNodes().getCount();

for(int nodeIndex=0; nodeIndex<nodeCount; nodeIndex++)

{

Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);

//Do not insert node if it is a last empty paragarph in the section.

Paragraph para = (Paragraph)srcNode ;

if ((para != null) && para.isEndOfSection() && (!para.hasChildNodes()))

{

break;

}

//If current paragraph is first paragraph of srcDoc

//then appent its content to insertAfterParagraph

if (para.equals(srcDoc.getFirstSection().getBody().getFirstParagraph()))

{

int firstParaChildCount = para.getChildNodes().getCount();

for(int childIndex = 0; childIndex<firstParaChildCount; childIndex++)

{

Node node = para.getChildNodes().get(childIndex);

Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

insertAfterParagraph.appendChild(dstNode);

}

//If subdocument contains only one paragraph

//then copy content of insertBeforeParagraph to insertAfterParagraph

//and remove insertBeforeParagraph

if (srcDoc.getFirstSection().getBody().getFirstParagraph().equals(srcDoc.getLastSection().getBody().getLastParagraph()))

{

while (insertBeforeParagraph.hasChildNodes())

{

insertAfterParagraph.appendChild(insertBeforeParagraph.getFirstChild());

}

insertBeforeParagraph.remove();

}

}

//If current paragraph is last paragraph of srcDoc

//then appent its content to insertBeforeParagraph

if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()))

{

Node previouseNode = null;

int firstParaChildCount = para.getChildNodes().getCount();

for(int childIndex = 0; childIndex<firstParaChildCount; childIndex++)

{

Node node = para.getChildNodes().get(childIndex);

Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

if (previouseNode == null)

{

insertBeforeParagraph.insertBefore(dstNode, insertBeforeParagraph.getFirstChild());

}

else

{

insertBeforeParagraph.insertAfter(dstNode, previouseNode);

}

previouseNode = dstNode;

}

}

else

{

//This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

//Insert new node after the reference node.

dstStory.insertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

}

}

I hope this could help you.

Best regards.

Hi there,

Problem is solved, Thank you so much!You guys are just great. Thanks a lot for the great support...

Michael

Hello,

first of all thanks again for the great help,.. but it looks like the proposed solution still has a little disfunction:


When inserting the content from the document to the mergefield, the content is added twice, except for the last paragraph. The difference with last time is that the doc to be inserted contains multiple paragraphs.?!Any idea?

Thanks,

Michael

ps: I added the original.doc, ToBeInserted(tmp.doc) and processed.doc in attachment

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

Thanks for your request. Sorry there was my mistake. Please change the following line in your code:

//If current paragraph is last paragraph of srcDoc

//then appent its content to insertBeforeParagraph

if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()))

Use the following instead

//If current paragraph is last paragraph of srcDoc

//then appent its content to insertBeforeParagraph

else if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()))

Best regards.

Hello there,

thanx for the solution...insert works well know...except that the style of the source paragraph is not applied/kept on the first paragraph that is inserted . The second one is OK. I did specify 'KEEP_SOURCE_FORMATTING'. Is there something else I have to do? I putted the original doc, the tempdoc to be inserted and the processed doc in attachment.

Thx,

Michael

Document xMergedBookmarksDoc = new Document(mergedBookmarksDocPath);

// Geef de Bookmarks collection van het document

Bookmarks bookmarks = xMergedBookmarksDoc.getRange().getBookmarks();

int nbBookmarks = bookmarks.getCount();

Document tmpDoc = new Document();

CompositeNode dstNode = tmpDoc.getFirstSection().getBody();

//StringBuffer value = new StringBuffer();

//MST NEW

//ISSUE 1476 : bookmarks worden in omgekeerde, eigenlijk zelf willekeurige volgorde ingevoegd in doc. Heb code zo aangepast dat er in omgekeerde volgorde door de

//lijst van in te voegen bookmarks wordt gegaan daar elke bookmark niet achteraan maar vooraan in de temp doc wordt toegevoegd

for (int x = 0; x < arlBookmarks.size() ; x++){

int omgekeerdePos = arlBookmarks.size() - x -1;

String bookmarkName = (String)arlBookmarks.get(omgekeerdePos);

for (int i = 0; i < nbBookmarks; i++) {

Bookmark bookmark = bookmarks.get(i);

String sBookmarkName = bookmark.getName();

if(bookmarkName.equalsIgnoreCase(sBookmarkName)){

NodeImporter importer = new NodeImporter(xMergedBookmarksDoc, tmpDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

appendBookmarkContent(importer, bookmark, dstNode);

try{

tmpDoc.getRange().getBookmarks().remove(sBookmarkName);

}catch(Exception e){

System.out.println("!!!!!!!!!!!!! sBookmarkName = " + sBookmarkName + " kon NIET worden verwijderd");

}

}

}

}

InsertDocumentAtMergeField(strFieldName, doc, tmpDoc);

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

Thanks for your inquiry. This occurs because first and last paragraph are not fully imported into the destination document. The code import first and last paragraph children only, but a not whole paragraph, that’s why first paragraph format is lost.

Best regards.

Hello, thanx for the help,but i don't really know what to change. Can you help some more?

Thx,

Michael

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

Thanks for your inquiry. Please try using the following code:

public static void InsertDocumentAtMergeField(String mergeFieldName, Document dstDoc, Document srcDoc) throws Exception

{

//Create DocumentBuilder

DocumentBuilder builder = new DocumentBuilder(dstDoc);

//Move cursor to bookmark and insert paragraph break

builder.moveToMergeField(mergeFieldName);

builder.writeln();

//Content of srcdoc will be inserted after this node

Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling();

//Content of first paragraph of srcDoc will be apended to this parafraph

Paragraph insertAfterParagraph = (Paragraph)insertAfterNode;

//Content of last paragraph of srcDoc will be apended to this parafraph

Paragraph insertBeforeParagraph = builder.getCurrentParagraph();

//We will be inserting into the parent of the destination paragraph.

CompositeNode dstStory = insertAfterNode.getParentNode();

//Remove empty paragraphs from the end of document

while (!srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes())

{

srcDoc.getLastSection().getBody().getLastParagraph().remove();

}

//Loop through all sections in the source document.

int sectCount = srcDoc.getSections().getCount();

for(int sectIndex=0; sectIndex<sectCount; sectIndex++)

{

Section srcSection = srcDoc.getSections().get(sectIndex);

//Loop through all block level nodes (paragraphs and tables) in the body of the section.

int nodeCount = srcSection.getBody().getChildNodes().getCount();

for(int nodeIndex=0; nodeIndex<nodeCount; nodeIndex++)

{

Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);

//Do not insert node if it is a last empty paragarph in the section.

Paragraph para = (Paragraph)srcNode ;

if ((para != null) && para.isEndOfSection() && (!para.hasChildNodes()))

{

break;

}

//If current paragraph is first paragraph of srcDoc

//then appent its content to insertAfterParagraph

if (para.equals(srcDoc.getFirstSection().getBody().getFirstParagraph()) && insertAfterParagraph.hasChildNodes())

{

int firstParaChildCount = para.getChildNodes().getCount();

for(int childIndex = 0; childIndex<firstParaChildCount; childIndex++)

{

Node node = para.getChildNodes().get(childIndex);

Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

insertAfterParagraph.appendChild(dstNode);

}

//If subdocument contains only one paragraph

//then copy content of insertBeforeParagraph to insertAfterParagraph

//and remove insertBeforeParagraph

if (srcDoc.getFirstSection().getBody().getFirstParagraph().equals(srcDoc.getLastSection().getBody().getLastParagraph()))

{

while (insertBeforeParagraph.hasChildNodes())

{

insertAfterParagraph.appendChild(insertBeforeParagraph.getFirstChild());

}

insertBeforeParagraph.remove();

}

}

//If current paragraph is last paragraph of srcDoc

//then appent its content to insertBeforeParagraph

else if (para.equals(srcDoc.getLastSection().getBody().getLastParagraph()) && insertBeforeParagraph.hasChildNodes())

{

Node previouseNode = null;

int firstParaChildCount = para.getChildNodes().getCount();

for(int childIndex = 0; childIndex<firstParaChildCount; childIndex++)

{

Node node = para.getChildNodes().get(childIndex);

Node dstNode = dstDoc.importNode(node, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

if (previouseNode == null)

{

insertBeforeParagraph.insertBefore(dstNode, insertBeforeParagraph.getFirstChild());

}

else

{

insertBeforeParagraph.insertAfter(dstNode, previouseNode);

}

previouseNode = dstNode;

}

}

else

{

//This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);

//Insert new node after the reference node.

dstStory.insertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

if(!insertAfterParagraph.hasChildNodes())

insertAfterParagraph.remove();

if(!insertBeforeParagraph.hasChildNodes())

insertBeforeParagraph.remove();

}

}

Hope this helps.

Best regards,

Thx a lot,

works well know...have a geat weekend,

Michael