Difficulties about inserting document content into other documents

1.docx (99.5 KB)

I want to copy the contents of document 1.docx (see attached) to another document, but there will be formatting changes (especially the formatting of the table is completely misplaced), the code I used is as follows:

Document doc = new Document("C:\\Users\\zhang\\Desktop\\f\\" + "1.docx");

Document doc2 = new Document();

DocumentBuilder b = new DocumentBuilder(doc2);

ImportFormatOptions i = new ImportFormatOptions();
i.IgnoreTextBoxes = false;
i.KeepSourceNumbering = true;
i.SmartStyleBehavior = true;

doc2.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting, i);

doc2.UpdateFields();
doc2.UpdatePageLayout();
doc2.UpdateTableLayout();
doc2.UpdateListLabels();
doc2.UpdateThumbnail();
doc2.UpdateWordCount();

doc2.Save("C:\\Users\\zhang\\Desktop\\f\\" + "2.docx");

Can the contents of 1.docx be copied or inserted into another document that is already open, the format must be identical ?

@marlon First of all you are using Document.UpdateTableLayout method in your code. This method is deprecated and will be removed in one of our future versions. Calling this method might affcet table layout unpredictably. So it is not recommended to use this method unless you are sure it will not break anything in your documents.
Second, the destination document in your code is created from scratch, that is why destination and source documents uses different set of compatibility options. To avoid this problem, clone the source document instead of creating it from scratch. The following code produces proper output:

Document doc = new Document("C:\\Temp\\1.docx");

Document doc2 = (Document)doc.Clone(false);
doc2.EnsureMinimum();

ImportFormatOptions i = new ImportFormatOptions();
i.IgnoreTextBoxes = false;
i.KeepSourceNumbering = true;
i.SmartStyleBehavior = true;

doc2.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting, i);

doc2.Save("C:\\Temp\\2.docx");

Thank you very much, I think the key sentence is ,

(Document)doc.Clone(false)

However, if I want to insert another document into an already open document and keep the source formatting, Tried using method CopyStylesFromTemplate to copy the format, but it doesn’t seem to work.I can’t find any other way to do it at the moment, such as the following code:

Document doc = new Document("C:\\Users\\zhang\\Desktop\\f\\" + "1.docx");
Document doc2 = new Document("C:\\Users\\zhang\\Desktop\\f\\" + "2.dotx");  //2.dotx is a template document, Already exists and is opened, need to insert 1.docx content

doc2.CopyStylesFromTemplate(doc);//Already open the document, can not be clone1.docx format, I can not find other ways, only through this method

doc2.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);

// doc2.Save("C:\\Users\\zhang\\Desktop\\f\\" + "3.docx"); // if save

@marlon Could you please attach your destination document? We will check the issue and provide you more information.

As I have mentioned, your documents might be created using different version of MS Word and as a result have different set of compatibility options. When you concatenate the documents compatibility options of the main document are applied and the content is reflowed appropriately. This might lead to difference in layout of the appended documents. Unfortunately, there is no way to keep different compatibility options for different parts of the document.

2.zip (9.6 KB)

Sorry, since I don’t have permission to upload the .dotx template document, I’ve zipped it up a bit, see attached 2.zip.

@marlon The problem with your document occurs because in the destination document character spacing control is set to doNotCompress, but in the source document it is set to compressPunctuation. If merge document document using the following code the table o the second page looks fine:

Document src = new Document(@"C:\Temp\1.docx");
Document dst = new Document(@"C:\Temp\2.dotx");

dst.JustificationMode = JustificationMode.Compress;

dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);

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

I tested it and the formatting changes are more serious, the formatting of out.docx is very different from the formatting of 1.docx, for example, the table on the left of the image below (screenshot from out.docx) has completely changed.

image.png (14.2 KB)

@marlon Unfortunately, I cannot reproduce the problem on my side using the latest 23.8 version of Aspose.Words. Here is the output document produced on my side: out.docx (86.5 KB)