Copy And Insert content of word document into another

Hii I want to insert content from one document to a template document. How can I do that

@sehar236 You can achieve this using Documentbuilder.InsertDocument method. Please see our documentation for more information:
Java: https://docs.aspose.com/words/java/insert-and-append-documents/
.NET: https://docs.aspose.com/words/net/insert-and-append-documents/
Python: https://docs.aspose.com/words/python-net/insert-and-append-documents/

i want to add the content inside the layout template. it adds it in the end i don’t want that. please help.

the template has multiple pages and the document where i want to copy content has also multiple pages.

@sehar236 You can use DocumentBuilder.MoveToXX method to move DocumentBuilder’s cursor to the position where it is required to insert the content. Please see our documentation for more information:
https://docs.aspose.com/words/net/navigation-with-cursor/

isn’t there any simpler way?

@sehar236 Could you please attach your template, input documents and expected output here for our reference? This will help us to better understand your requirements.

Could you please elaborate what simpler way you mean? It is required only two lines of code to move cursor into the desired position and insert another document.

@sehar236 Could you please attach your template, input documents and expected output as MS Word documents? It is not possible to test or analyze the scenario using screenshots.

@sehar236 Thank you for additional information. But what is the expected output?

@sehar236 I am afraid, it is not possible with your template. There are full page images on each page. You might noticed that even in MS Word editing such document it a real headache.

so what should I do then. I can only use aspose.

@sehar236 I would suggest to redesign the template. Using full-page images is not the best practice. Also, you should note that MS Word documents are flow by their nature, so there is no “page” concept. Consumer applications reflows document content into pages on the fly. You should keep this in mind when design the template.

@sehar236 I am afraid the design of the template still does not allow to achieve what you need. You can check this in MS Word by copying and pasting content of the source document into your template. MS Word format does not generally support continuous background like in your template. Usually there is first page and other pages headers/footers for repeating background content. But in your case the background content is different on 4 pages. To achieve this it is required to put different background image on each page. This can be achieved using code like this:

Document template = new Document(@"C:\Temp\template.docx");
DocumentBuilder builder = new DocumentBuilder(template);
builder.MoveToDocumentEnd();
builder.InsertDocument(new Document(@"C:\Temp\src.docx"), ImportFormatMode.KeepSourceFormatting);

// Split the document into pages
Document doc = (Document)template.Clone(false);
for (int i = 0; i < template.PageCount; i++)
{
    Document page = template.ExtractPages(i, 1);
    // Get the background image for the page.
    string bgImage = $@"C:\Temp\{i}.emf";
    if (File.Exists(bgImage))
    {
        Shape bgShape = new Shape(page, ShapeType.Image);
        bgShape.ImageData.SetImage(bgImage);
        bgShape.Width = page.FirstSection.PageSetup.PageWidth;
        bgShape.Height = page.FirstSection.PageSetup.PageHeight;
        bgShape.WrapType = WrapType.None;
        bgShape.BehindText = true;
        bgShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        bgShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        bgShape.Left = 0;
        bgShape.Top = 0;
        page.FirstSection.Body.FirstParagraph.AppendChild(bgShape);
    }
    doc.AppendDocument(page, ImportFormatMode.UseDestinationStyles);
}

doc.Save(@"C:\Temp\out.docx");

template.docx (9.2 MB)

docs.zip (338.7 KB)

Thanks for the solution . I am trying that but in my solution it says ‘Document’ does not contain a definition for ‘ExtractPages’ and no accessible extension method ‘ExtractPages’ accepting a first argument of type ‘Document’ . do i need to update to new version or is their any alternative?

@sehar236 Yes, you should update to the latest version of Aspose.Words. I am afraid there is no alternative of this method in old versions. The method is available starting from Aspose.Words 20.10 version.

I have added some images in word document but when i convert to pdf those images are lost. What is the solution?

@sehar236 Could you please attach your input and output documents here for testing? We will check the issue and provide you more information.

@sehar236 The problem is not reproducible using the latest 24.1 version of Aspose.Words. As I can see you are using an old 19.6 version. Please try using the latest version on your side and let us know if the problem still persists.

the problem is I can’t update to new version. Isnt there any way that i fix this issue in this version?

@sehar236 I have tested conversion to PDF using 19.6 version and the problem is still not reproducible on my side. I have used the following code for testing:

Document doc = new Document("C:\\Temp\\in.docx");
doc.Save(@"C:\Temp\out_19.6.pdf");

Here is the output: out_19.6.pdf (1.5 MB)