Merge word document files in asp.net c#
Introduction :
This article show you how to merge or combine two more doc files in Asp.net using C# .
I have created one function to merge multiple word file in c# .it is very easy to use .You just need to pass word file path arrays and save file path . Then i will save merged word document file .
Add following dll reference to your application :
Code for merge word doc files :
Introduction :
This article show you how to merge or combine two more doc files in Asp.net using C# .
I have created one function to merge multiple word file in c# .it is very easy to use .You just need to pass word file path arrays and save file path . Then i will save merged word document file .
Add following dll reference to your application :
Microsoft.Office.Interop.Word
Code for merge word doc files :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
namespace HamidSite
{
public partial class MergerWorddocs : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] DocsFiles = new string[] { @"C:\Merfiles\Doc1.docx", @"C:\Merfiles\Doc2.docx" };
string PathToSave =Server.MapPath("MergesDocs/MergeDoc.docx");
Merge(DocsFiles,PathToSave,true);
}
public void Merge(string[] filesToMerge, string fileSavePath, bool insertPageBreaks)
{
object missing = System.Type.Missing;
object pageBreak = WdBreakType.wdPageBreak;
object outputFile = fileSavePath;
Application wordApplication = new Application();
try
{
Document wordDocument = wordApplication.Documents.Add(ref missing, ref missing , ref missing , ref missing);
Selection selection = wordApplication.Selection;
foreach (string docFile in filesToMerge)
{
selection.InsertFile(docFile , ref missing , ref missing , ref missing , ref missing);
if (insertPageBreaks)
{
selection.InsertBreak(ref pageBreak);
}
}
wordDocument.SaveAs(ref outputFile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wordDocument = null;
wordApplication.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex) { throw ex; }
}
}
}
No comments:
Post a Comment