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

Free Support Forum - aspose.com

Issue in Inserting a document into another at a bookmark

Hi,

Our requirement is as follows:

I have a Main Word Document (Doc1) inside which there is a bookmark (B1). I need to insert a Child Word Document(ChildDoc) at the bookmark (B1) in main word document (Doc1). I have tried using the logic given in ASPOSE documentation, it works but not per our requirement.

Poblem : Now the issue is the child document can contain anything tables, images, paragraphs, etc.. its a whole document by itself. So after inserting the first document, again if we try to insert a new ChildDocument(ChildDoc2) at the same location(say bookmark B1) or try to insert the same document(ChildDoc) once again data is getting duplicated. I am able to see both the documents which i dont want. It should ideally over write the existing content at the given bookmark and insert the new content.

Requirement : When i try to insert any document or image or anything at a bookmark location in an existing document, it should delete the existing content at the given bookmark location and add the new content there. When i repeat the operation for number of times, the behaviour should be the same.

Please suggest something.

Thanks & Regards,

Suguna

Hi Suguna,

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

Thanks for your request. In this case, you should just clear bookmark before inserting. Please see the code below:

// Open documents.

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

Document srcDoc = new Document("src.doc");

InsertDocumentAtBookmark("Test", dstDoc, srcDoc);

dstDoc.Save("out.doc", SaveFormat.Doc);

public static void InsertDocumentAtBookmark(string bookmarkName, Document dstDoc, Document srcDoc)

{

// Clear bookmark

dstDoc.Range.Bookmarks[bookmarkName].Text = string.Empty;

//Create DocumentBuilder

DocumentBuilder builder = new DocumentBuilder(dstDoc);

//Move cursor to bookmark and insert paragraph break

builder.MoveToBookmark(bookmarkName);

builder.Writeln();

//Content of srcdoc will be inserted after this node

Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;

//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.CurrentParagraph;

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

CompositeNode dstStory = insertAfterNode.ParentNode;

// Loop through all sections in the source document.

foreach (Section srcSection in srcDoc.Sections)

{

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

foreach (Node srcNode in srcSection.Body)

{

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

Paragraph para = srcNode as Paragraph;

if (para != null && para.IsEndOfSection && !para.HasChildNodes)

break;

//If current paragraph is first paragraph of srcDoc

//then append its content to insertAfterParagraph

if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstParagraph))

{

foreach (Node node in para.ChildNodes)

{

Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);

insertAfterParagraph.AppendChild(dstNode);

}

}

//If current paragraph is last paragraph of srcDoc

//then append its content to insertBeforeParagraph

else

{

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

Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

// Insert new node after the reference node.

dstStory.InsertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

}

if (!insertBeforeParagraph.HasChildNodes)

insertBeforeParagraph.Remove();

}

Best regards,

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(10)