Insert content from another document

Hi,
I have one document consider that as mainDocument, i want to insert the content from document2 to main document, starting from where my first document’s content end

I am using below code but it is not giving me the document which i am inserting

Document mainDocument = templates.Item1;
Document documentToInsert = templates.Item2;
DocumentBuilder documentBuilder = new DocumentBuilder(mainDocument);
for (int i = 0; i <= 5; i++)
{
    documentBuilder.MoveToDocumentEnd();
    documentBuilder.InsertDocument(documentToInsert, ImportFormatMode.KeepSourceFormatting);
    string def = documentBuilder.CurrentParagraph.GetText();
    /*
    //mainDocument.Sections.Add(documentToInsert);

    var lastSection = mainDocument.LastChild;
    var node = documentToInsert.FirstChild;
    //mainDocument.InsertAfter(node, lastSection);
    mainDocument.Append(node);
    mainDocument.Document.Append(node);
    mainDocument.LastSection.AppendChild(node);
    //mainDocument.ParentNode.*/
}

mainDocument.Save("C:\\Document.docx");
MemoryStream ms = new MemoryStream();
mainDocument.Save(ms, SaveFormat.Docx);
var reportBuffer = ms.ToArray();
var base64String = Convert.ToBase64String(reportBuffer);

I am only getting the content which is mainDocument, documentToInsert’s content is not there in final Downloaded document

@Amrinder_Singh

To insert content from one document into another using Aspose.Words for .NET, you can utilize the DocumentBuilder class along with the InsertDocument method. It seems that your current implementation is attempting to insert the document multiple times in a loop, which may not be necessary. Instead, you can simply move to the end of the main document and insert the content from the second document once.

Here’s a refined version of your code that demonstrates how to achieve this:

// Load the main document and the document to insert
Document mainDocument = templates.Item1;
Document documentToInsert = templates.Item2;

// Create a DocumentBuilder for the main document
DocumentBuilder documentBuilder = new DocumentBuilder(mainDocument);

// Move the cursor to the end of the main document
documentBuilder.MoveToDocumentEnd();

// Optionally, insert a page break before the inserted document
documentBuilder.InsertBreak(BreakType.PageBreak);

// Insert the document while keeping its source formatting
documentBuilder.InsertDocument(documentToInsert, ImportFormatMode.KeepSourceFormatting);

// Save the modified main document
mainDocument.Save("C:\\Document.docx");

Key Points:

  • Move to Document End: The MoveToDocumentEnd method positions the cursor at the end of the main document, ensuring that the inserted content appears at the correct location.
  • Insert Break: Adding a page break before the inserted document can help maintain a clean separation between the two documents.
  • InsertDocument Method: This method allows you to insert the entire content of the second document into the main document while preserving its formatting.

This approach should resolve the issue you are facing with the insertion of the second document. If you need to insert the document multiple times or at different locations, you can adjust the logic accordingly.

For more detailed information, you can refer to the official documentation on inserting documents using Aspose.Words for .NET here.

Sources:

[1]: Insert and Append Documents in C#

@Amrinder_Singh In your case you should use either Document.AppenDocument method. Or use Merger.Merge method to concatenate documents.

Using Document.Append I am able to insert the other document into my destnation document, but i have one more issue as Document.Append() will insert into new Page, i want to start insert whatever data is there in my sourcedoucment, insert that into destination starting from the next line where it ended the last time

@Amrinder_Singh You can set section start of the first section in the second document to continuous:

Document doc1 = new Document("doc1.docx");
Document doc2 = new Document("doc2.docx");
// Set section start of the first section of the second document.
doc2.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
// Append document.
doc1.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);
// Save the output
doc1.Save(@"C:\Temp\out.docx");

With that what is happening, the sourcedocument which i am inserting that is working fine it start from next line only but the second time , when the first time we insert source to destination, it is starting from next page only not from the next line of destination document

@Amrinder_Singh Could you please attach your input, output and expected output documents here for our reference? We will check the issue and provide you more information.

Destinantion Document.zip (21.6 KB)

Above i attached the required scenarios for me

@Amrinder_Singh Thank you for additional information. There is an explicit page break at the end of the destination document.

So it is expected that there will be page break between the destination document content and the appended content.

ok thanks, i will check that

1 Like