Free Support Forum - aspose.com

Adding doc between normal text


I’m trying to compose a word document by inserting some fixed text and ,between, text from an external doc file.

I’have tryed your code but the new content from doc file is inserted after all my text and not between where I want it.

Thank you.
Stefano.

Hi

Thanks for your inquiry. I think that you can try using the following code.

public void TestIsertDocumentBetwenText_105975()

{

Document doc = new Document(@"409_105975_ste\in1.doc");

Regex regex = new Regex("here");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction_105975), true);

doc.Save(@"409_105975_ste\out.doc");

}

ReplaceAction ReplaceAction_105975(object sender, ReplaceEvaluatorArgs e)

{

Run run = (Run)e.MatchNode;

Run run1 = new Run(run.Document);

run1.Text = run.Text.Substring(run.Text.IndexOf(e.Match.Value) + e.Match.Value.Length);

run.Text = run.Text.Replace(run1.Text, "");

run.ParentParagraph.InsertAfter(run1, run);

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document);

builder.MoveTo(run1);

builder.InsertBreak(BreakType.ParagraphBreak);

Document srcDoc = new Document(@"409_105975_ste\in2.doc");

InsertDocument(run.ParentParagraph, srcDoc);

return ReplaceAction.Skip;

}

I hope that this will help you.

Best regards.

My aim is to insert text without usinge text references, for example I want to write 2 lines from code with documentbuilder and, after these, open an external doc file and add its contents and ,finally, add a new (or more) line from code.

I want to mix text from code or from file sequentially.

Thank you.

Stefano.

Hi

You can try using the following code.

Document doc = new Document();

Document srcDoc = new Document(@"409_105975_ste\in2.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Some text before document");

InsertDocument(builder.CurrentParagraph, srcDoc);

builder.MoveToDocumentEnd();//I think that you missed this line

builder.Write("Some text after document");

doc.Save(@"409_105975_ste\out.doc");

InsertDocument method you can find here.

http://www.aspose.com/Products/Aspose.Words/Api/InsertOneDocumentintoAnotheratAnyLocation.html

Best regards.

Yes, I have missed the line ! Now it works !

Thank you.
Stefano.