Free Support Forum - aspose.com

Importing files to bookmark locations

Thanks for the quick response on prior question. I have one more issue that I am running into with combining files together. I have a case where I want to import a file into a specified bookmark location. I am using the code below but what happens is that the imported file is being placed on seperate page and I get an additional blank page on the end. What am I doing wrong?

protected void InsertFile(string FileLocation, string bookmarkName)

{

// Get current working document

Document doc = (Document)(WordDocList[CurrentIndexWordDocAndWordApp]);

// Insert the target file into the desired document

InsertDocument(doc.Range.Bookmarks[bookmarkName].

BookmarkStart, new Document(FileLocation));

}

private void InsertDocument(Node node, Document doc)

{

int index;

Document dstDoc = node.Document;

Section insertedSection;

if(node.GetAncestor(typeof(Cell))!=null ||

node.GetAncestor(typeof(Shape))!=null)

{

while(node.NodeType != NodeType.Paragraph)

{

node = node.ParentNode;

}

// Insertion point is tracked to Cell or Shape level:

// - insert appended document on node by node basis.

index = node.ParentNode.ChildNodes.IndexOf(node);

foreach(Section section in doc.Sections)

{

foreach(Node sourceNode in section.Body.ChildNodes)

{

// Only Paragraph or Table nodes can be

// inserted into Cell or Shape

if(sourceNode is Paragraph || sourceNode is Table)

{

// Do not insert node if it is a last

// empty paragarph in the section.

if(sourceNode is Paragraph &&

sourceNode == section.Body.LastChild &&

!sourceNode.HasInnerText)

{

break;

}

Node insertedNode = dstDoc.ImportNode(sourceNode, true,

ImportFormatMode.KeepSourceFormatting);

node.ParentNode.ChildNodes.Insert(index++, insertedNode);

}

}

}

}

else

{

// Insertion point is tracked to Section.Body level:

// - insert appended document on section by section basis.

Section dstSection;

Body body = (Body)node.GetAncestor(typeof(Body));

if(body.LastChild!=node)

{

DocumentBuilder builder = new DocumentBuilder(dstDoc);

builder.MoveTo(node);

builder.InsertBreak(BreakType.SectionBreakContinuous);

dstSection = builder.CurrentParagraph.ParentSection;

}

else

{

dstSection = (Section)node.GetAncestor(typeof(Section));

}

index = dstDoc.Sections.IndexOf(dstSection);

int index0 = index;

foreach(Section section in doc.Sections)

{

insertedSection = (Section)dstDoc.ImportNode(section,

true, ImportFormatMode.KeepSourceFormatting);

// Uncomment this line if you want to get rid of

// headers/footers in the inserted section

//insertedSection.ClearHeadersFooters();

dstDoc.Sections.Insert(index++, insertedSection);

}

dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;

}

}

Hi,

Not at all Smile [:)] Please try a modified version of document insertion code published here:

http://www.aspose.com/Community/forums/thread/56629.aspx

In general, we are planning to improve the insertion of nodes and documents so that you wouldn't have such problems in the future.

Tried the code referenced in the post and I am getting different problem. While the file merge is occurring it is not inserting the file at the bookmark location. Instead the file is always being imported at the beginning of the file.

Below is the code. This is the last outstanding issue with Aspose integration. If this works I will recommend purchasing your product to replace our use of word automation.

protected void Aspose_InsertFile(string FileLocation, string bookmarkName)

{

// Get current working document

Document doc = (Document)(WordDocList[CurrentIndexWordDocAndWordApp]);

// Insert the target file into the desired document

Aspose_InsertDocument(doc.Range.Bookmarks[bookmarkName].

BookmarkStart, new Document(FileLocation));

}

///

/// This method is taken from ASPOSE for inserting documents at bookmark

///

///

///

private void Aspose_InsertDocument(Node node, Document doc)

{

Document dstDoc = node.Document;

Section insertedSection;

int index = node.ParentNode.ChildNodes.IndexOf(node);

foreach (Section section in doc.Sections)

{

insertedSection = (Section)dstDoc.ImportNode(section, true,

ImportFormatMode.KeepSourceFormatting);

foreach (Node insertedNode in insertedSection.Body.ChildNodes)

{

// Only Paragraph or Table nodes can be inserted into Cell or Shape

if (insertedNode is Paragraph || insertedNode is Table)

{

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

if (insertedNode is Paragraph &&

insertedNode == section.Body.LastChild &&

insertedNode.GetText().Equals(string.Empty))

break;

node.ParentNode.ParentNode.ChildNodes.

Insert(++index, insertedNode.Clone(true));

}

}

}

}

I have made some improvements to InsertDocument code. It now seems to work correctly for many differenent cases. The only limitation is that it does not break the paragraph if the bookmark marks some part of the text inside it. Instead the document is inserted immediately after this paragraph.

Please test it and let me know if it does the job now:

///

/// Inserts content of the external document after the specified node.

///

/// Node in the destination document where the external document content should be inserted.

/// Document to insert.

public void InsertDocument(Node node, Document doc)

{

CompositeNode parentNode = node.ParentNode;

while (true)

{

if (parentNode == null)

throw new Exception("Document cannot be inserted after the specified node.");

if (parentNode is Story || parentNode is Cell || parentNode is Shape)

break;

node = parentNode;

parentNode = node.ParentNode;

}

int index = node.ParentNode.ChildNodes.IndexOf(node);

Document dstDoc = node.Document;

Section insertedSection;

foreach (Section section in doc.Sections)

{

insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);

foreach (Node insertedNode in insertedSection.Body.ChildNodes)

{

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

if (insertedNode is Paragraph && insertedNode == section.Body.LastChild && insertedNode.ToTxt().Equals(string.Empty))

break;

parentNode.ChildNodes.Insert(++index, insertedNode.Clone(true));

}

}

}

Best regards,

Thanks miklovan did the trick. If testing going ok (nothing terrible comes up) we will buying a license of Aspose.Word.

Zeid