Hello,
I am evaluating Aspose Words .Net for a routine we need to implement which would merge multiple word documents into one. I am using sample code as provided on-line and it works well for the most part. I am experiencing difficulties when attempting to merge two attached documents. Document #1 contains an image, and Document #2 contains a fancy word table. When I merge the two, table’s formatting becomes skewed. Can anyone shed any light on why this is happening.
I’ve attached documents, the resulting output document, as well as a sample project which demonstrates the problem.
Here is the code as well for quick review:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aspose.Words;
using System.IO;
namespace IC.Tinker.AsposeWord.DocMerge
{
public partial class fMergeDocuments : Form
{
private static string TEST_FOLDER = @"S:\general\IFRS Phase III\Document Assembly\Test Notes";
private static string OUTPUT_REPORT_NAME = @"Aspose Concatenation";
public fMergeDocuments()
{
InitializeComponent();
}
private void btnMergeDocs_Click(object sender, EventArgs e)
{
// Retrieve files for processing.
string[] files = Directory.GetFiles(TEST_FOLDER, "*.doc*", SearchOption.TopDirectoryOnly); // Retrieve both .doc and .docx files.
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document dstDoc = new Document(files[0]);
// Concatonate files with a heading and page break in between
for (int i = 1; i < files.Length; i++)
AppendDoc(dstDoc, files[i]);
// Save the finished document.
dstDoc.Save(TEST_FOLDER + OUTPUT_REPORT_NAME + ".docx", SaveFormat.Docx);
dstDoc.Save(TEST_FOLDER + OUTPUT_REPORT_NAME + ".doc", SaveFormat.Doc);
System.Diagnostics.Process.Start(TEST_FOLDER + OUTPUT_REPORT_NAME + ".doc");
}
///
/// A useful function that you can use to easily append one document to another.
///
/// The destination document where to append to.
/// The source document.
public void AppendDoc(Document dstDoc, string srcDocName)
{
Document srcDoc = new Document(srcDocName);
// Make the section continuous with the note heading.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
foreach (Section srcSection in srcDoc)
{
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
// // Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.UseDestinationStyles);
// Now the new section node can be appended to the destination document.
dstDoc.AppendChild(dstSection);
}
}
}
}
Thanks,
Irfan