static void Main(string[] args) { //Creates new Word document instance for Word processing using (WordDocument document = new WordDocument()) { //Opens the input Word document Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Table.docx")); document.Open(docStream, FormatType.Docx); docStream.Dispose(); //Opens the source Word document containing table style definition WordDocument srcDocument = new WordDocument(); docStream = File.OpenRead(Path.GetFullPath(@"../../../TableStyles.docx")); srcDocument.Open(docStream, FormatType.Docx); docStream.Dispose(); //Gets the table style (CustomStyle) from the styles collection WTableStyle srcTableStyle = srcDocument.Styles.FindByName("CustomStyle", StyleType.TableStyle) as WTableStyle; //Creates a cloned copy of table style WTableStyle clonedTableStyle = srcTableStyle.Clone() as WTableStyle; //Adds the cloned copy of source table style to the destination document document.Styles.Add(clonedTableStyle); //Releases the resources of source Word document instance srcDocument.Dispose(); //Gets table to apply style WTable table = (WTable)document.LastSection.Tables[0]; //Applies the custom table style to the table table.ApplyStyle("CustomStyle"); //Saves the file in the given path docStream = File.Create(Path.GetFullPath(@"Result.docx")); document.Save(docStream, FormatType.Docx); docStream.Dispose(); } }
static void Main(string[] args) { //Creates new Word document instance for Word processing WordDocument document = new WordDocument(); //Opens the input Word document Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Table.docx")); document.Open(docStream, FormatType.Docx); docStream.Dispose(); //Adds a new custom table style WTableStyle tableStyle = document.AddTableStyle("CustomStyle") as WTableStyle; //Applies formatting for whole table tableStyle.TableProperties.RowStripe = 1; tableStyle.TableProperties.ColumnStripe = 1; tableStyle.TableProperties.Paddings.Top = 0; tableStyle.TableProperties.Paddings.Bottom = 0; tableStyle.TableProperties.Paddings.Left = 5.4f; tableStyle.TableProperties.Paddings.Right = 5.4f; //Applies conditional formatting for first row ConditionalFormattingStyle firstRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstRow); firstRowStyle.CharacterFormat.Bold = true; firstRowStyle.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(255, 255, 255, 255); firstRowStyle.CellProperties.BackColor = Syncfusion.Drawing.Color.Blue; //Applies conditional formatting for first column ConditionalFormattingStyle firstColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstColumn); firstColumnStyle.CharacterFormat.Bold = true; //Applies conditional formatting for odd row ConditionalFormattingStyle oddRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddRowBanding); oddRowBandingStyle.CellProperties.BackColor = Syncfusion.Drawing.Color.WhiteSmoke; //Gets table to apply style WTable table = (WTable)document.LastSection.Tables[0]; //Applies the custom table style to the table table.ApplyStyle("CustomStyle"); //Saves the file in the given path docStream = File.Create(Path.GetFullPath(@"Result.docx")); document.Save(docStream, FormatType.Docx); docStream.Dispose(); //Releases the resources of Word document instance document.Dispose(); }
static void Main(string[] args) { //Creates new Word document instance for Word processing using (WordDocument document = new WordDocument()) { //Opens the input Word document Stream docStream = File.OpenRead(Path.GetFullPath(@"../../../Table.docx")); document.Open(docStream, FormatType.Docx); docStream.Dispose(); //Gets the table style (Medium Shading 1 Accent 1) from the styles collection WTableStyle tableStyle = document.Styles.FindByName("Medium Shading 1 Accent 1", StyleType.TableStyle) as WTableStyle; //Gets the conditional formatting style of the first row (table headers) from the table style ConditionalFormattingStyle firstRowStyle = tableStyle.ConditionalFormattingStyles[ConditionalFormattingType.FirstRow]; if (firstRowStyle != null) { //Modifies the background color for first row (table headers) firstRowStyle.CellProperties.BackColor = Syncfusion.Drawing.Color.FromArgb(255, 31, 56, 100); } //Saves the file in the given path docStream = File.Create(Path.GetFullPath(@"Result.docx")); document.Save(docStream, FormatType.Docx); docStream.Dispose(); } }
public ActionResult TableStyles(string Group1, string Group2) { if (Group1 == null) { return(View()); } string dataPath = ResolveApplicationDataPath("TemplateTableStyle.doc", "App_Data\\DocIO"); WordDocument document = new WordDocument(dataPath); string dataBase = ResolveApplicationDataPath("Northwind.mdb", "App_Data"); // Get Data from the Database. OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataBase); conn.Open(); DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Suppliers", conn); adapter.Fill(ds); ds.Tables[0].TableName = "Suppliers"; adapter.Dispose(); conn.Close(); // Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(ds.Tables[0]); #region Built-in table styles if (Group1 == "Built-in") { //Get table to apply style. WTable table = (WTable)document.LastSection.Tables[0]; //Apply built-in table style to the table. table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent5); } #endregion Built-in table styles #region Custom Style else { #region Custom table styles //Get table to apply style WTable table = (WTable)document.LastSection.Tables[0]; //Apply custom table style to the table #region Apply Table style WTableStyle tableStyle = document.AddTableStyle("Tablestyle") as WTableStyle; System.Drawing.Color borderColor = System.Drawing.Color.WhiteSmoke; System.Drawing.Color firstRowBackColor = System.Drawing.Color.Blue; System.Drawing.Color backColor = System.Drawing.Color.WhiteSmoke; ConditionalFormattingStyle firstRowStyle, lastRowStyle, firstColumnStyle, lastColumnStyle, oddColumnBandingStyle, oddRowBandingStyle, evenRowBandingStyle; #region Table Properties tableStyle.TableProperties.RowStripe = 1; tableStyle.TableProperties.ColumnStripe = 1; tableStyle.TableProperties.LeftIndent = 0; tableStyle.TableProperties.Paddings.Top = 0; tableStyle.TableProperties.Paddings.Bottom = 0; tableStyle.TableProperties.Paddings.Left = 5.4f; tableStyle.TableProperties.Paddings.Right = 5.4f; tableStyle.TableProperties.Borders.Top.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Top.LineWidth = 1f; tableStyle.TableProperties.Borders.Top.Color = System.Drawing.Color.AliceBlue; tableStyle.TableProperties.Borders.Top.Space = 0; tableStyle.TableProperties.Borders.Bottom.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Bottom.LineWidth = 1f; tableStyle.TableProperties.Borders.Bottom.Color = borderColor; tableStyle.TableProperties.Borders.Bottom.Space = 0; tableStyle.TableProperties.Borders.Left.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Left.LineWidth = 1f; tableStyle.TableProperties.Borders.Left.Color = borderColor; tableStyle.TableProperties.Borders.Left.Space = 0; tableStyle.TableProperties.Borders.Right.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Right.LineWidth = 1f; tableStyle.TableProperties.Borders.Right.Color = borderColor; tableStyle.TableProperties.Borders.Right.Space = 0; tableStyle.TableProperties.Borders.Horizontal.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Horizontal.LineWidth = 1f; tableStyle.TableProperties.Borders.Horizontal.Color = borderColor; tableStyle.TableProperties.Borders.Horizontal.Space = 0; #endregion #region Conditional Formatting Properties #region First Row Conditional Formatting Style firstRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstRow); #region Character format firstRowStyle.CharacterFormat.Bold = true; firstRowStyle.CharacterFormat.BoldBidi = true; firstRowStyle.CharacterFormat.TextColor = System.Drawing.Color.FromArgb(255, 255, 255, 255); #endregion #region Table Cell Properties firstRowStyle.CellProperties.Borders.Top.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Top.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Top.Color = borderColor; firstRowStyle.CellProperties.Borders.Top.Space = 0; firstRowStyle.CellProperties.Borders.Bottom.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Bottom.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Bottom.Color = borderColor; firstRowStyle.CellProperties.Borders.Bottom.Space = 0; firstRowStyle.CellProperties.Borders.Left.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Left.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Left.Color = borderColor; firstRowStyle.CellProperties.Borders.Left.Space = 0; firstRowStyle.CellProperties.Borders.Right.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Right.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Right.Color = borderColor; firstRowStyle.CellProperties.Borders.Right.Space = 0; firstRowStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; firstRowStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; firstRowStyle.CellProperties.BackColor = firstRowBackColor; firstRowStyle.CellProperties.ForeColor = System.Drawing.Color.FromArgb(0, 255, 255, 255); firstRowStyle.CellProperties.TextureStyle = TextureStyle.TextureNone; #endregion #endregion #region Last Row Conditional Formatting Style lastRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.LastRow); #region Character format lastRowStyle.CharacterFormat.Bold = true; lastRowStyle.CharacterFormat.BoldBidi = true; #endregion #region Table Cell Properties lastRowStyle.CellProperties.Borders.Top.BorderType = BorderStyle.Double; lastRowStyle.CellProperties.Borders.Top.LineWidth = .75f; lastRowStyle.CellProperties.Borders.Top.Color = borderColor; lastRowStyle.CellProperties.Borders.Top.Space = 0; lastRowStyle.CellProperties.Borders.Bottom.BorderType = BorderStyle.Single; lastRowStyle.CellProperties.Borders.Bottom.LineWidth = 1f; lastRowStyle.CellProperties.Borders.Bottom.Color = borderColor; lastRowStyle.CellProperties.Borders.Bottom.Space = 0; lastRowStyle.CellProperties.Borders.Left.BorderType = BorderStyle.Single; lastRowStyle.CellProperties.Borders.Left.LineWidth = 1f; lastRowStyle.CellProperties.Borders.Left.Color = borderColor; lastRowStyle.CellProperties.Borders.Left.Space = 0; lastRowStyle.CellProperties.Borders.Right.BorderType = BorderStyle.Single; lastRowStyle.CellProperties.Borders.Right.LineWidth = 1f; lastRowStyle.CellProperties.Borders.Right.Color = borderColor; lastRowStyle.CellProperties.Borders.Right.Space = 0; lastRowStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; lastRowStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; #endregion #endregion #region First Column Conditional Formatting Style firstColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstColumn); #region Character format firstColumnStyle.CharacterFormat.Bold = true; firstColumnStyle.CharacterFormat.BoldBidi = true; #endregion #endregion #region Last Column Conditional Formatting Style lastColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.LastColumn); #region Character format lastColumnStyle.CharacterFormat.Bold = true; lastColumnStyle.CharacterFormat.BoldBidi = true; #endregion #endregion #region Odd Column Banding Conditional Formatting Style oddColumnBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddColumnBanding); #region Table Cell Properties oddColumnBandingStyle.CellProperties.BackColor = backColor; oddColumnBandingStyle.CellProperties.ForeColor = System.Drawing.Color.FromArgb(0, 255, 255, 255); oddColumnBandingStyle.CellProperties.TextureStyle = TextureStyle.TextureNone; #endregion #endregion #region Odd Row Banding Conditional Formatting Style oddRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddRowBanding); #region Table Cell Properties oddRowBandingStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; oddRowBandingStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; oddRowBandingStyle.CellProperties.BackColor = backColor; oddRowBandingStyle.CellProperties.ForeColor = System.Drawing.Color.FromArgb(0, 255, 255, 255); oddRowBandingStyle.CellProperties.TextureStyle = TextureStyle.TextureNone; #endregion #endregion #region Even Row Banding Conditional Formatting Style evenRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.EvenRowBanding); #region Table Cell Properties evenRowBandingStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; evenRowBandingStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; #endregion #endregion #endregion #endregion table.ApplyStyle("Tablestyle"); #endregion } #endregion Custom Style #region Document save option //Save as .docx format if (Group2 == "WordDocx") { return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } // Save as WordML(.xml) format else if (Group2 == "WordML") { return(document.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .pdf format else if (Group2 == "Pdf") { DocToPDFConverter converter = new DocToPDFConverter(); PdfDocument pdfDoc = converter.ConvertToPDF(document); return(pdfDoc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } #endregion Document save option return(View()); }
public ActionResult TableStyles(string Group1, string Group2) { if (Group1 == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; string dataPath = string.Empty; dataPath = basePath + @"/DocIO/TemplateTableStyle.doc"; WordDocument document = new WordDocument(); FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); document.Open(fileStream, FormatType.Doc); fileStream.Dispose(); fileStream = null; //Create MailMergeDataTable MailMergeDataTable mailMergeDataTable = GetMailMergeDataTable(); //Execute Mail Merge with groups. document.MailMerge.ExecuteGroup(mailMergeDataTable); #region Built-in Style if (Group1 == "Built-in") { //Get table to apply style. WTable table = (WTable)document.LastSection.Tables[0]; //Apply built-in table style to the table. table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent5); } #endregion Built-in Style #region Custom Style else { #region Custom table styles //Get table to apply style WTable table = (WTable)document.LastSection.Tables[0]; #region Apply Table style WTableStyle tableStyle = document.AddTableStyle("Tablestyle") as WTableStyle; Color borderColor = Color.WhiteSmoke; Color firstRowBackColor = Color.Blue; Color backColor = Color.WhiteSmoke; ConditionalFormattingStyle firstRowStyle, lastRowStyle, firstColumnStyle, lastColumnStyle, oddColumnBandingStyle, oddRowBandingStyle, evenRowBandingStyle; #region Table Properties tableStyle.TableProperties.RowStripe = 1; tableStyle.TableProperties.ColumnStripe = 1; tableStyle.TableProperties.LeftIndent = 0; tableStyle.TableProperties.Paddings.Top = 0; tableStyle.TableProperties.Paddings.Bottom = 0; tableStyle.TableProperties.Paddings.Left = 5.4f; tableStyle.TableProperties.Paddings.Right = 5.4f; tableStyle.TableProperties.Borders.Top.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Top.LineWidth = 1f; tableStyle.TableProperties.Borders.Top.Color = Color.AliceBlue; tableStyle.TableProperties.Borders.Top.Space = 0; tableStyle.TableProperties.Borders.Bottom.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Bottom.LineWidth = 1f; tableStyle.TableProperties.Borders.Bottom.Color = borderColor; tableStyle.TableProperties.Borders.Bottom.Space = 0; tableStyle.TableProperties.Borders.Left.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Left.LineWidth = 1f; tableStyle.TableProperties.Borders.Left.Color = borderColor; tableStyle.TableProperties.Borders.Left.Space = 0; tableStyle.TableProperties.Borders.Right.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Right.LineWidth = 1f; tableStyle.TableProperties.Borders.Right.Color = borderColor; tableStyle.TableProperties.Borders.Right.Space = 0; tableStyle.TableProperties.Borders.Horizontal.BorderType = BorderStyle.Single; tableStyle.TableProperties.Borders.Horizontal.LineWidth = 1f; tableStyle.TableProperties.Borders.Horizontal.Color = borderColor; tableStyle.TableProperties.Borders.Horizontal.Space = 0; #endregion #region Conditional Formatting Properties #region First Row Conditional Formatting Style firstRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstRow); #region Character format firstRowStyle.CharacterFormat.Bold = true; firstRowStyle.CharacterFormat.BoldBidi = true; firstRowStyle.CharacterFormat.TextColor = Color.FromArgb(255, 255, 255, 255); #endregion #region Table Cell Properties firstRowStyle.CellProperties.Borders.Top.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Top.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Top.Color = borderColor; firstRowStyle.CellProperties.Borders.Top.Space = 0; firstRowStyle.CellProperties.Borders.Bottom.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Bottom.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Bottom.Color = borderColor; firstRowStyle.CellProperties.Borders.Bottom.Space = 0; firstRowStyle.CellProperties.Borders.Left.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Left.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Left.Color = borderColor; firstRowStyle.CellProperties.Borders.Left.Space = 0; firstRowStyle.CellProperties.Borders.Right.BorderType = BorderStyle.Single; firstRowStyle.CellProperties.Borders.Right.LineWidth = 1f; firstRowStyle.CellProperties.Borders.Right.Color = borderColor; firstRowStyle.CellProperties.Borders.Right.Space = 0; firstRowStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; firstRowStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; firstRowStyle.CellProperties.BackColor = firstRowBackColor; firstRowStyle.CellProperties.ForeColor = Color.FromArgb(0, 255, 255, 255); firstRowStyle.CellProperties.TextureStyle = TextureStyle.TextureNone; #endregion #endregion #region Last Row Conditional Formatting Style lastRowStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.LastRow); #region Character format lastRowStyle.CharacterFormat.Bold = true; lastRowStyle.CharacterFormat.BoldBidi = true; #endregion #region Table Cell Properties lastRowStyle.CellProperties.Borders.Top.BorderType = BorderStyle.Double; lastRowStyle.CellProperties.Borders.Top.LineWidth = .75f; lastRowStyle.CellProperties.Borders.Top.Color = borderColor; lastRowStyle.CellProperties.Borders.Top.Space = 0; lastRowStyle.CellProperties.Borders.Bottom.BorderType = BorderStyle.Single; lastRowStyle.CellProperties.Borders.Bottom.LineWidth = 1f; lastRowStyle.CellProperties.Borders.Bottom.Color = borderColor; lastRowStyle.CellProperties.Borders.Bottom.Space = 0; lastRowStyle.CellProperties.Borders.Left.BorderType = BorderStyle.Single; lastRowStyle.CellProperties.Borders.Left.LineWidth = 1f; lastRowStyle.CellProperties.Borders.Left.Color = borderColor; lastRowStyle.CellProperties.Borders.Left.Space = 0; lastRowStyle.CellProperties.Borders.Right.BorderType = BorderStyle.Single; lastRowStyle.CellProperties.Borders.Right.LineWidth = 1f; lastRowStyle.CellProperties.Borders.Right.Color = borderColor; lastRowStyle.CellProperties.Borders.Right.Space = 0; lastRowStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; lastRowStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; #endregion #endregion #region First Column Conditional Formatting Style firstColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.FirstColumn); #region Character format firstColumnStyle.CharacterFormat.Bold = true; firstColumnStyle.CharacterFormat.BoldBidi = true; #endregion #endregion #region Last Column Conditional Formatting Style lastColumnStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.LastColumn); #region Character format lastColumnStyle.CharacterFormat.Bold = true; lastColumnStyle.CharacterFormat.BoldBidi = true; #endregion #endregion #region Odd Column Banding Conditional Formatting Style oddColumnBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddColumnBanding); #region Table Cell Properties oddColumnBandingStyle.CellProperties.BackColor = backColor; oddColumnBandingStyle.CellProperties.ForeColor = Color.FromArgb(0, 255, 255, 255); oddColumnBandingStyle.CellProperties.TextureStyle = TextureStyle.TextureNone; #endregion #endregion #region Odd Row Banding Conditional Formatting Style oddRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.OddRowBanding); #region Table Cell Properties oddRowBandingStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; oddRowBandingStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; oddRowBandingStyle.CellProperties.BackColor = backColor; oddRowBandingStyle.CellProperties.ForeColor = Color.FromArgb(0, 255, 255, 255); oddRowBandingStyle.CellProperties.TextureStyle = TextureStyle.TextureNone; #endregion #endregion #region Even Row Banding Conditional Formatting Style evenRowBandingStyle = tableStyle.ConditionalFormattingStyles.Add(ConditionalFormattingType.EvenRowBanding); #region Table Cell Properties evenRowBandingStyle.CellProperties.Borders.Horizontal.BorderType = BorderStyle.Cleared; evenRowBandingStyle.CellProperties.Borders.Vertical.BorderType = BorderStyle.Cleared; #endregion #endregion #endregion #endregion table.ApplyStyle("Tablestyle"); #endregion } #endregion Custom Style #endregion FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .xml format if (Group2 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); document.Save(ms, type); document.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }