Hello experts,
I'm using below code to merge multiple documents. But, after merging, I see some issues in the merged document.
public static void MergeDocuments(List<string> documentPaths, string outputFile) { if (System.IO.File.Exists(outputFile)) System.IO.File.Delete(outputFile); string firstDoc = documentPaths.First(); System.IO.File.Copy(firstDoc, outputFile); documentPaths.RemoveAt(0); using (WordprocessingDocument myDoc = WordprocessingDocument.Open(outputFile, true)) { foreach (var documentPath in documentPaths) { string altChunkId = string.Format("AltChunkId{0}", Guid.NewGuid().ToString()); MainDocumentPart mainPart = myDoc.MainDocumentPart; AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart( AlternativeFormatImportPartType.WordprocessingML, altChunkId); using (FileStream fileStream = File.Open(documentPath, FileMode.Open)) chunk.FeedData(fileStream); AltChunk altChunk = new AltChunk(); altChunk.Id = altChunkId; Paragraph para = new Paragraph(new Run((new Break() { Type = BreakValues.Page }))); mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild); mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.LastChild); mainPart.Document.Save(); } } }
The second document I merge has some abnormal line spacing issues in the merged document. It adds extra space after each of the paragraphs. Even the lines in a table also get space after them. If you kindly review the documents, you will notice the issues.
I have uploaded 3 documents in the onedrive account.
Doc 1: https://1drv.ms/w/s!AmifSn8Lhbgbg24v61BIYPjJvTt-
Doc 2: https://1drv.ms/w/s!AmifSn8Lhbgbg2wv61BIYPjJvTt-
Merged: https://1drv.ms/w/s!AmifSn8Lhbgbg20v61BIYPjJvTt-
I have merged "Doc 1.docx" and "Doc 2.docx". The output is merged.docx. You can run below code to merge them.
static void Main() { var list = new List<string>(); list.Add(@"C:\temp\doc 1.docx"); list.Add(@"C:\temp\doc 2.docx"); string output = @"C:\temp\merged.docx"; MergeDocuments(list, output); }
Any help on solving the issues will be highly appreciated.
Thanks in advance.