Ejemplo n.º 1
0
        public ActionResult TableStyles(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            string       dataPath = ResolveApplicationDataPath("TemplateTableStyle.doc", "Data\\DocIO");
            WordDocument document = new WordDocument(dataPath);

            string dataBase = ResolveApplicationDataPath("Northwind.mdb", "Data");

            // Get Data from the Database.
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
            string          connectionstring = "Data Source = " + ResolveApplicationDataPath("Northwind.mdb", "Data");
            OleDbConnection conn             = new OleDbConnection();

            conn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Password=\"\";User ID=Admin;" + connectionstring;
            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
            //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

            #region Document save option
            //Save as .docx format
            if (Group1 == "WordDocx")
            {
                return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            // Save as WordML(.xml) format
            else if (Group1 == "WordML")
            {
                return(document.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            //Save as .pdf format
            else if (Group1 == "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());
        }
Ejemplo n.º 2
0
 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();
     }
 }
Ejemplo n.º 3
0
        private async void StileOpzioniTabellaWordAsync()
        {
            //Crea una istanza della classe WordDocument
            WordDocument document = new WordDocument();
            //Aggiunge una sezione ad un documento word
            IWSection sectionFirst = document.AddSection();
            //Aggiunge una tabella ad un documento word
            IWTable tableFirst = sectionFirst.AddTable();

            //Dimensiona la tabella
            tableFirst.ResetCells(3, 2);
            //Salva il documento su memory stream
            MemoryStream stream = new MemoryStream();
            await document.SaveAsync(stream, FormatType.Docx);

            document.Open(stream, FormatType.Docx);

            WSection section = document.Sections[0];

            WTable table = section.Tables[0] as WTable;

            //Applica alla tabella lo stile built-in "LightShading"
            table.ApplyStyle(BuiltinTableStyle.LightShading);

            //Abilita una formattazione speciale per le banded columns della tabella
            table.ApplyStyleForBandedColumns = true;

            //Abilita una formattazione speciale per le banded rows of the table
            table.ApplyStyleForBandedRows = true;

            //Disabilita la formattazione speciale per la prima colonna della tabella
            table.ApplyStyleForFirstColumn = false;

            //Abilita una formattazione speciale per la riga di testata della tabella
            table.ApplyStyleForHeaderRow = true;

            //Abilita una formattazione speciale per l'ultima colonna della tabella
            table.ApplyStyleForLastColumn = true;

            //Disabilita la formattazione speciale per l'ultima riga della tabella
            table.ApplyStyleForLastRow = false;

            //Salva e chiudi l'istanza del documento
            //Salva il documento su memory stream
            MemoryStream memoryStream = new MemoryStream();
            await document.SaveAsync(memoryStream, FormatType.Docx);

            //Libera le risorse impegnate dall'istanza WordDocument
            document.Close();

            //Salva lo stream come file di documento Word nella macchina locale
            StorageFile stFile = await Save(memoryStream, "TableStyle.docx");

            DefaultLaunch("TableStyle.docx");
        }
Ejemplo n.º 4
0
        public ActionResult TableStyles(string Group1)
        {
            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 table styles
            //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

            FormatType type        = FormatType.Docx;
            string     filename    = "Sample.docx";
            string     contenttype = "application/vnd.ms-word.document.12";
            #region Document SaveOption
            //Save as .xml format
            if (Group1 == "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));
        }
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
 static void Main(string[] args)
 {
     //Creates new Word document instance for Word processing
     using (WordDocument document = new WordDocument())
     {
         //Adds a section to the Word document
         IWSection section = document.AddSection();
         //Sets the page margin
         section.PageSetup.Margins.All = 72;
         //Adds a paragrah to the section
         IWParagraph paragraph = section.AddParagraph();
         paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
         paragraph.ParagraphFormat.AfterSpacing        = 20;
         IWTextRange textRange = paragraph.AppendText("Suppliers");
         textRange.CharacterFormat.FontSize  = 14;
         textRange.CharacterFormat.Bold      = true;
         textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(255, 50, 62, 79);
         //Modifies the font size as 10 for default paragraph style
         WParagraphStyle style = document.Styles.FindByName("Normal") as WParagraphStyle;
         style.CharacterFormat.FontSize = 10;
         //Adds a table to the section
         WTable table = section.AddTable() as WTable;
         table.ResetCells(1, 6);
         table[0, 0].Width = 52f;
         table[0, 0].AddParagraph().AppendText("Supplier ID");
         table[0, 1].Width = 128f;
         table[0, 1].AddParagraph().AppendText("Company Name");
         table[0, 2].Width = 70f;
         table[0, 2].AddParagraph().AppendText("Contact Name");
         table[0, 3].Width = 92f;
         table[0, 3].AddParagraph().AppendText("Address");
         table[0, 4].Width = 66.5f;
         table[0, 4].AddParagraph().AppendText("City");
         table[0, 5].Width = 56f;
         table[0, 5].AddParagraph().AppendText("Country");
         //Imports data to the table.
         ImportDataToTable(table);
         //Applies the built-in table style (Medium Shading 1 Accent 1) to the table
         table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent1);
         //Saves the file in the given path
         Stream docStream = File.Create(Path.GetFullPath(@"Result.docx"));
         document.Save(docStream, FormatType.Docx);
         docStream.Dispose();
     }
 }
Ejemplo n.º 7
0
        private async void StileTabellaWordAsync()
        {
            //Crea una istanza della classe WordDocument
            WordDocument document = new WordDocument();

            //Apre un documento word esistente nella istanza DocIO
            //document.Open("Table.docx", FormatType.Docx);
            StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile   storageFile;

            try
            {
                storageFile = await local.GetFileAsync("Table.docx");
            }
            catch (Exception)
            {
                return;
            }
            var streamFile = await storageFile.OpenStreamForReadAsync();

            document.Open(streamFile, FormatType.Docx);

            WSection section = document.Sections[0];

            WTable table = section.Tables[0] as WTable;

            //Applica alla tabella lo stile built-in "LightShading"
            table.ApplyStyle(BuiltinTableStyle.LightShading);

            //Salva e chiudi l'istanza del documento
            //Salva il documento su memory stream
            MemoryStream stream = new MemoryStream();
            await document.SaveAsync(stream, FormatType.Docx);

            //Libera le risorse impegnate dall'istanza WordDocument
            document.Close();

            //Salva lo stream come file di documento Word nella macchina locale
            StorageFile stFile = await Save(stream, "TableStyle.docx");

            DefaultLaunch("TableStyle.docx");
        }
 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(@"../../../Template.docx"));
         document.Open(docStream, FormatType.Docx);
         docStream.Dispose();
         //Creates a new table
         WTable table = new WTable(document);
         table.ResetCells(1, 6);
         table[0, 0].Width = 52f;
         table[0, 0].AddParagraph().AppendText("Supplier ID");
         table[0, 1].Width = 128f;
         table[0, 1].AddParagraph().AppendText("Company Name");
         table[0, 2].Width = 70f;
         table[0, 2].AddParagraph().AppendText("Contact Name");
         table[0, 3].Width = 92f;
         table[0, 3].AddParagraph().AppendText("Address");
         table[0, 4].Width = 66.5f;
         table[0, 4].AddParagraph().AppendText("City");
         table[0, 5].Width = 56f;
         table[0, 5].AddParagraph().AppendText("Country");
         //Imports data to the table
         ImportDataToTable(table);
         //Applies the built-in table style (Medium Shading 1 Accent 1) to the table
         table.ApplyStyle(BuiltinTableStyle.MediumShading1Accent1);
         //Replaces the table placeholder text with a new table
         TextBodyPart bodyPart = new TextBodyPart(document);
         bodyPart.BodyItems.Add(table);
         document.Replace("[Suppliers table]", bodyPart, true, true, true);
         //Saves the resultant file in the given path
         docStream = File.Create(Path.GetFullPath(@"Result.docx"));
         document.Save(docStream, FormatType.Docx);
         docStream.Dispose();
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates word document with built - in table styles
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Open the template document.
                WordDocument document = new WordDocument(fileName + "TemplateTableStyle.doc");

                //Create MailMergeDataTable
                MailMergeDataTable mailMergeDataTable = GetMailMergeDataTable();

                // Execute Mail Merge with groups.
                document.MailMerge.ExecuteGroup(mailMergeDataTable);

                #region Built-in table styles
                //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

                # region Save Document

                //Save as docx format
                if (wordDocxRadioBtn.Checked)
                {
                    //Saving the document as .docx
                    document.Save("Sample.docx", FormatType.Docx);
                    //Message box confirmation to view the created document.
                    if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        try
                        {
                            //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer]
#if NETCORE
                            System.Diagnostics.Process process = new System.Diagnostics.Process();
                            process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.docx")
                            {
                                UseShellExecute = true
                            };
                            process.Start();
#else
                            System.Diagnostics.Process.Start("Sample.docx");
#endif
                            //Exit
                            this.Close();
                        }
                        catch (Win32Exception ex)
                        {
                            MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system");
                            Console.WriteLine(ex.ToString());
                        }
                    }
                }
                //Save as pdf format
                else if (pdfRadioBtn.Checked)
                {
                    DocToPDFConverter converter = new DocToPDFConverter();
                    //Convert word document into PDF document
                    PdfDocument pdfDoc = converter.ConvertToPDF(document);
                    //Save the pdf file
                    pdfDoc.Save("Sample.pdf");
                    //Message box confirmation to view the created document.
                    if (MessageBoxAdv.Show("Do you want to view the generated PDF?", " Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        try
                        {
#if NETCORE
                            System.Diagnostics.Process process = new System.Diagnostics.Process();
                            process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf")
                            {
                                UseShellExecute = true
                            };
                            process.Start();
#else
                            System.Diagnostics.Process.Start("Sample.pdf");
#endif
                            //Exit
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBoxAdv.Show("PDF Viewer is not installed in this system");
                            Console.WriteLine(ex.ToString());
                        }
                    }
                }
                else
                {
                    // Exit
                    this.Close();
                }
                # endregion
            }
Ejemplo n.º 10
0
        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());
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates word document with built - in table styles
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Open the template document.
                WordDocument document = new WordDocument(fileName + "TemplateTableStyle.doc");

                // 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
                //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

                # region Save Document

                //Save as docx format
                if (wordDocxRadioBtn.Checked)
                {
                    //Saving the document as .docx
                    document.Save("Sample.docx", FormatType.Docx);
                    //Message box confirmation to view the created document.
                    if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        try
                        {
                            //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer]
                            System.Diagnostics.Process.Start("Sample.docx");
                            //Exit
                            this.Close();
                        }
                        catch (Win32Exception ex)
                        {
                            MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system");
                            Console.WriteLine(ex.ToString());
                        }
                    }
                }
                //Save as pdf format
                else if (pdfRadioBtn.Checked)
                {
                    DocToPDFConverter converter = new DocToPDFConverter();
                    //Convert word document into PDF document
                    PdfDocument pdfDoc = converter.ConvertToPDF(document);
                    //Save the pdf file
                    pdfDoc.Save("Sample.pdf");
                    //Message box confirmation to view the created document.
                    if (MessageBoxAdv.Show("Do you want to view the generated PDF?", " Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        try
                        {
                            System.Diagnostics.Process.Start("Sample.pdf");
                            //Exit
                            this.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBoxAdv.Show("PDF Viewer is not installed in this system");
                            Console.WriteLine(ex.ToString());
                        }
                    }
                }
                else
                {
                    // Exit
                    this.Close();
                }
                # endregion
            }
        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));
        }