Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Oct 22, 2014 3:48 pm

Hi,
I am trying to merge two documents, using the c# method below :

public static void InsertAckTemplate(string filename, string template)
{
if (!File.Exists(filename))
{
throw new AsConfException(string.Format("Unable to open file {0}", filename), filename);
}
if (!File.Exists(template))
{
throw new AsConfException(string.Format("Unable to open template file {0}", template), filename);
}

//Get section from template
var tmpDoc = new Spire.Doc.Document();
tmpDoc.LoadFromFile(template, FileFormat.Docx);

var doc = new Spire.Doc.Document();
doc.LoadFromFile(filename, FileFormat.Docx);

doc.AcceptChanges();
if (doc.Sections.Count > 0)
{
Paragraph p = doc.LastSection.AddParagraph();
Break stun = new Break(doc, BreakType.PageBreak);
p.ChildObjects.Insert(0, stun);
foreach (Paragraph item in tmpDoc.Sections[0].Paragraphs)
{
p.ChildObjects.Add(item.Clone());
}
}
else
{
Section sec = doc.AddSection();
sec.AddParagraph().ChildObjects.Insert(0, tmpDoc);
}

SaveDocument(filename, doc);
doc.Close();
tmpDoc.Close();
}

When i try to add each paragraph of the template to the file, i have an error : index is out of the bounds of the table.

I attached the filename and template.

Can you help me please ? thanks

tarikzaid
 
Posts: 29
Joined: Wed Aug 06, 2014 7:16 am

Thu Oct 23, 2014 3:08 am

Hi,
Thanks for your inquiry.
please try the below new method to merge documents.
Code: Select all
    string template = "template.doc";
    string filename = "filename.docx";
    Document document = new Document();
    document.LoadFromFile(template, FileFormat.Doc);
    document.InsertTextFromFile(filename, FileFormat.Docx);
    document.SaveToFile("MergedFile.docx", FileFormat.Docx);
   

If you have any questions, welcome to get it back to us.

Best Regards
Lisa
E-iceblue support team.
User avatar

lisa.chen
 
Posts: 21
Joined: Thu Mar 20, 2014 2:31 am

Thu Oct 23, 2014 12:39 pm

Hi,
The code you give works fine.

Thank you.

tarikzaid
 
Posts: 29
Joined: Wed Aug 06, 2014 7:16 am

Fri Oct 24, 2014 3:09 am

Hi,

Thanks for your feedback.
If you have any problems, welcome to get it back to us.

Best Regards,
Lisa
E-iceblue.
User avatar

lisa.chen
 
Posts: 21
Joined: Thu Mar 20, 2014 2:31 am

Return to Spire.Doc

cron