Free Support Forum - aspose.com

Copy section break?

Hello,

I am copying the some contents from other reference document. The same is attached.

I want to copy contents from [!INSERTTHIS!] to [!/INSERTTHIS!].

However there is section break in the document. I want to copy that also, as it lies between the tags.

Please help. I dont want to copy complete sections.

I tried to search on your forum, but could not worked!

Thank you.

Hi

Thanks for your interest in Aspose products. I created a code example for you.

Paragraph startTag = null;

Paragraph endTag = null;

public void TestCopyContent_102684()

{

Document doc = new Document(@"358_102684_isrc\in.doc");

Regex regexStart = new Regex("\\[!INSERTTHIS!\\]");

Regex regexEnd = new Regex("\\[!/INSERTTHIS!\\]");

//Get paragraph containing start tag

doc.Range.Replace(regexStart, new ReplaceEvaluator(ReplaceActionStart_102684), true);

//Get paragraph containing end tag

doc.Range.Replace(regexEnd, new ReplaceEvaluator(ReplaceActionEnd_102684), true);

//remove content before start tag

while (startTag.ParentSection.PreviousSibling != null)

{

startTag.ParentSection.PreviousSibling.Remove();

}

while (startTag.PreviousSibling != null)

{

startTag.PreviousSibling.Remove();

}

//remove content after end tag

while (endTag.ParentSection.NextSibling != null)

{

endTag.ParentSection.NextSibling.Remove();

}

while (endTag.NextSibling != null)

{

endTag.NextSibling.Remove();

}

//remove start and end tags

startTag.Remove();

endTag.Remove();

//save doc containing required content

doc.Save(@"358_102684_isrc\out.doc");

}

//Get paragraph containing start tag

ReplaceAction ReplaceActionStart_102684(object sender, ReplaceEvaluatorArgs e)

{

startTag = (e.MatchNode as Run).ParentParagraph;

return ReplaceAction.Skip;

}

//Get paragraph containing end tag

ReplaceAction ReplaceActionEnd_102684(object sender, ReplaceEvaluatorArgs e)

{

endTag = (e.MatchNode as Run).ParentParagraph;

return ReplaceAction.Skip;

}

I hope that this code will help you.

Best regards.

Hi Thanks for reply. I tried your example with InsertDocument function to copy document. However I am not able to copy section breaks. Ca you please tell me how to do?

Currently it is copying only paragraphs inside the section nodes.

Hi

Thanks for your request. I modified the InsertDocument Method. See the following code.

public void InsertDocument_102684(Node insertAfterNode, Document srcDoc)

{

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

if (!((insertAfterNode.NodeType == NodeType.Paragraph) || (insertAfterNode.NodeType == NodeType.Table)))

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

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

CompositeNode dstStory = insertAfterNode.ParentNode;

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

NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);

Document dstDoc = dstStory.Document;

Section dstSection = (dstStory as Body).ParentSection;

DocumentBuilder builder = new DocumentBuilder(dstDoc);

int index = dstDoc.Sections.IndexOf((dstStory as Body).ParentSection);

if (srcDoc.Sections.Count > 1)

{

builder.MoveToSection(index);

builder.MoveToParagraph(dstSection.Body.Paragraphs.IndexOf(insertAfterNode), -1);

builder.InsertBreak(BreakType.SectionBreakContinuous);

}

// Loop through all sections in the source document.

foreach (Section srcSection in srcDoc.Sections)

{

int i = 0;

if (srcSection.Equals(srcDoc.FirstSection))

{

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

foreach (Node srcNode in srcSection.Body)

{

i++;

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

// 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);

insertAfterNode = newNode;

}

}

else

{

Node newNode = importer.ImportNode(srcSection, true);

dstDoc.Sections.Insert(index+1, newNode);

}

}

}

I hope that this code will help you.

Best regards.

Thanks, it worked !

One more question. Please have a look to attached document to main thread.

I want to copy part of document from [!InsertThis!] to [!/InsertThis!] 2 times. However I am finding difficulty to copy same section in same document. Can you please guide us?

Hi

Thanks for your inquiry. I think that you can use Clone function to solve this problem. For example see the following code.

doc1.AppendChild(sect);

doc1.AppendChild(sect.Clone(true));

I hope that this will help you.

Best regards.