Dear support iam facing issues merging two words documents and getting out of memory exception please help me with this.Attached a pwd protected .rar file. pwd is: aspose
Attached the code used as well
https://systrends-my.sharepoint.com/:u:/p/rajesh_sanam/EetFX3vgtXhPt91FyolDtI0BclUnoqnadd59mctWvewgOw?e=fhUsBE
public static void Merge_Multiple_Word_Documents_Into_One_Using_CSharp()
{
Document doc = new Document(@"5FullTariff 0-349.doc");
Document docx = new Document(@"5FullTariff 350-875.doc");
Aspose_Merge_Word_Documents(doc, docx, true);
doc.Save(@"both\out.docx");
}
public static void Aspose_Merge_Word_Documents(Document dstDoc, Document srcDocx, bool useDstHeaders)
{
foreach (Section srcSection in srcDocx)
{
Section dstSection = (Section)dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
if (useDstHeaders)
{
dstSection.HeadersFooters.Clear();
dstSection.HeadersFooters.LinkToPrevious(true);
}
dstDoc.AppendChild(dstSection);
}
}
@rajeshsanam Thank you for reporting the problem to us. I have managed to reproduce the problem. However, I do not think this should be considered as a bug. Your documents are simply too large.
You can reduce Aspose.Words memory usage by using LoadOptions.TempFolder, SaveOptions.TempFolder and SaveOptions.MemoryOptimization options. In this case Aspose.Words is able to merge your documents, but it takes about 8 minutes on my side. Here is code I used:
LoadOptions loadOptions = new LoadOptions();
loadOptions.TempFolder = @"C:\Temp\tmp";
// Ensure that the directory exists and load
Directory.CreateDirectory(loadOptions.TempFolder);
Document doc1 = new Document(@"C:\Temp\5FullTariff 0-349.doc", loadOptions);
Console.WriteLine("First loaded!");
Document doc2 = new Document(@"C:\Temp\5FullTariff 350-875.doc", loadOptions);
Console.WriteLine("Second loaded!");
Aspose_Merge_Word_Documents(doc1, doc2, true);
Console.WriteLine("Documents merged!");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.TempFolder = loadOptions.TempFolder;
saveOptions.MemoryOptimization = true;
doc1.Save(@"C:\Temp\out.docx", saveOptions);
The resulting document is ~27MB DOCX document. MS Word hangs for a while while loading it. It took about 10 minutes to open this document in MS Word on my side and MS Word hangs upon scrolling the document.
It is strongly not recommended to use such large documents since consumer applications might not be able to handle them properly.
Hi ,
Iam able to generate the merged document using temp folder but the existing headers were overwritten, and i need to reorder the page numbers is there any solution for this ?.
Yes you are right the word document is large , so we are converting to pdf and giving it to the customer.
@rajeshsanam Headers/footers are overridden by your Aspose_Merge_Word_Documents
method. You can use built-in Document.AppendDocument
method to merge documents:
Document doc1 = new Document(@"C:\Temp\5FullTariff 0-349.doc", loadOptions);
Document doc2 = new Document(@"C:\Temp\5FullTariff 350-875.doc", loadOptions);
doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
Page numbers in header and footer are updated automatically when document is opened in MS Word. You can update first in the document using Aspose.Words by calling Document.UpdateFields
method. Also, you should note that some section in your documents has PageSetup.RestartPageNumbering option set, if you need page numbering to continue for a whole document, you should reset this property to false
.
Note the fields are updated automatically by default when you convert document to fixed page formats, like PDF using Aspose.Words. I have managed to merge and convert your documents to PDF using the following code:
LoadOptions loadOptions = new LoadOptions();
loadOptions.TempFolder = @"C:\Temp\tmp";
// Ensure that the directory exists and load
Directory.CreateDirectory(loadOptions.TempFolder);
Document doc1 = new Document(@"C:\Temp\5FullTariff 0-349.doc", loadOptions);
Document doc2 = new Document(@"C:\Temp\5FullTariff 350-875.doc", loadOptions);
doc1.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.TempFolder = loadOptions.TempFolder;
saveOptions.MemoryOptimization = true;
doc1.Save(@"C:\Temp\out.pdf", saveOptions);
But it took about 40 minutes on my side. Adobe Acrobat hangs for few minutes while opening it. 8k+ pages document is simply to huge to process normally. So I would suggest you to use smaller documents.