Copy Content between word files

Hello,

I want to copy content from one document and paste into another one I am sharing the sample file with you.

I don’t know how to achieve this.

It’ll be a great help if I get to know how to achieve this.
Documents.zip (27.9 KB)

@Niteen_Jadhav

Could you please share more details on this scenario?

Sample code and which API you are using for this purpose?

I am not using any API’s I need a suggestion what should I use

@Niteen_Jadhav

Our sister company Aspose offers this feature. Please take a look at Aspose.Words for .NET document builder.

Hello @atir.tahir I have created a api using Aspose.words, I am using multiple functionality of Groupdocs & Aspose in the api, following are the functionality I am using,

  1. GroupDocs Redaction

  2. Aspose.Words

Here I have am getting a candidate resume in a Web-Api in JSON format and I am creating a Resume with the request. The resume will be in a template which I am storing as a Sections (word file stored in a hard drive).

So, I have 11 Sections and few lists and sub-lists.
Sections.zip (2.7 MB)

So, here I am using Groupdocs Redection to Replace Candidate Details with Actual data which I am getting in JSON format and creating a new file with FileName + “_redaction”.

after this I am using Aspose.Words to Add the section in newly created word file. and at the end creating a whole new file with all the data.

I am sharing the original File (1.0 MB) and File Created Using Aspose.Word (747.6 KB) there are many differences in both the files

I am also sharing JSON File (1.2 KB), Api created by me (2.4 KB) and
Entities (580 Bytes) for your reference.

NOTE: The major problem I am facing is the design of images in my templates are changing and blank pages are getting created even after I am forcefully deleting blank pages.
Sections.zip (2.70 MB)
Candidate Dossier Template.docx (1.04 MB)
Mahesh Kanchan.docx (748 KB)
JsonFormat.zip (1.22 KB)
Api.zip (2.44 KB)
Entity.zip (580 Bytes)

@Niteen_Jadhav Incorrect alignment in a document occurs because of the first blank page and line breaks between content. To preserve the formatting, you can use the first “Section” document as the starting position for insertion or set some bool parameter that will help you avoid inserting a page break the first time.

bool firstInsert = true;

if (!firstInsert)
{
    builder.MoveToDocumentEnd();
    if (pageType == "NewPage")
        builder.InsertBreak(BreakType.PageBreak);
}

Also, I would recommend you don’t add LineBreaks after each document insertion. Instead, you can consider to add this LineBreaks only after a main documents.

Your documents have different section settings with the first blank document, so if you continue to use the blank document as the start document, you need to change the section settings to match the inserted documents. You can use the following code:

doc.FirstSection.PageSetup.PaperSize = docToInsert.FirstSection.PageSetup.PaperSize;
doc.FirstSection.PageSetup.PageWidth = docToInsert.FirstSection.PageSetup.PageWidth;
doc.FirstSection.PageSetup.PageHeight = docToInsert.FirstSection.PageSetup.PageHeight;

To be sure that all content will be inserted correctly, you should also check that paragraphs in the documents have correct line spacing.

Here is my full code for testing:

List<string> docFiles = Directory.GetFiles(MyDir + "Sections\\", "*.*").ToList();

var doc = new Document();
var builder = new DocumentBuilder(doc);

bool firstInsert = true;
foreach (string file in docFiles)
{
    string pageType;
    if (Path.GetFileName(file).Contains("-"))
        pageType = "";
    else
        pageType = "NewPage";

    builder.MoveToDocumentEnd();
    if (!firstInsert)
        if (pageType == "NewPage")
            builder.InsertBreak(BreakType.PageBreak);

    var docToInsert = new Document(file);

    doc.FirstSection.PageSetup.PaperSize = docToInsert.FirstSection.PageSetup.PaperSize;
    doc.FirstSection.PageSetup.PageWidth = docToInsert.FirstSection.PageSetup.PageWidth;
    doc.FirstSection.PageSetup.PageHeight = docToInsert.FirstSection.PageSetup.PageHeight;

    var importFormatOptions = new ImportFormatOptions
    {
        KeepSourceNumbering = true,
    };
    builder.InsertDocument(docToInsert, ImportFormatMode.KeepSourceFormatting, importFormatOptions);

    firstInsert = false;
}

doc.Save("output.docx");

And the output file:
output.docx (798.4 KB)

@Niteen_Jadhav You might also consider using Introduction to LINQ Reporting Engine in C#|Aspose.Words for .NET. You can create a template and fill it with data in json format.