Exemplo n.º 1
0
        /// <summary>
        /// Imports the data from XML file to the table.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.Exception">reader</exception>
        /// <exception cref="XmlException">Unexpected xml tag  + reader.LocalName</exception>
        private static void ImportDataToTable(WTable table)
        {
            FileStream fs     = new FileStream(@"../../../Suppliers.xml", FileMode.Open, FileAccess.Read);
            XmlReader  reader = XmlReader.Create(fs);

            if (reader == null)
            {
                throw new Exception("reader");
            }
            while (reader.NodeType != XmlNodeType.Element)
            {
                reader.Read();
            }
            if (reader.LocalName != "SuppliersList")
            {
                throw new XmlException("Unexpected xml tag " + reader.LocalName);
            }
            reader.Read();
            while (reader.NodeType == XmlNodeType.Whitespace)
            {
                reader.Read();
            }
            while (reader.LocalName != "SuppliersList")
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.LocalName)
                    {
                    case "Suppliers":
                        //Adds new row to the table for importing data from next record.
                        WTableRow tableRow = table.AddRow(true);
                        ImportDataToRow(reader, tableRow);
                        break;
                    }
                }
                else
                {
                    reader.Read();
                    if ((reader.LocalName == "SuppliersList") && reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }
            reader.Dispose();
            fs.Dispose();
        }
Exemplo n.º 2
0
 private void SetRepeatValue()
 {
     if (lstRepeat.Count > 0)
     {
         foreach (IRepeatWord iRepeat in lstRepeat)
         {
             WTableRow r = Section.Tables[iRepeat.ITemp.IndexTable].Rows[iRepeat.ITemp.IndexRow].Clone();
             Section.Tables[iRepeat.ITemp.IndexTable].Rows.Insert(countRowTable[iRepeat.ITemp.IndexTable], r);
             countRowTable[iRepeat.ITemp.IndexTable] = countRowTable[iRepeat.ITemp.IndexTable] + 1;
             string     paragraph = string.Empty;
             WTableCell cell;
             for (int i = 0, n = Section.Tables[iRepeat.ITemp.IndexTable].LastRow.Cells.Count; i < n; i++)
             {
                 cell = Section.Tables[iRepeat.ITemp.IndexTable].LastRow.Cells[i];
                 ReplateTempInCell(cell, iRepeat);
                 //paragraph = ReplateTempInParagraph(cell.LastParagraph.Text, iRepeat);
                 //cell.LastParagraph.Text = paragraph;
             }
         }
     }
 }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // Create a new document.
            WordDocument document = new WordDocument();

            // Adding a new section to the document.
            IWSection section = document.AddSection();

            section.PageSetup.DifferentFirstPage = true;
            IWTextRange textRange;
            IWParagraph paragraph = section.AddParagraph();

            // --------------------------------------------
            // Table in page header
            // --------------------------------------------


            IWParagraph hParagraph = new WParagraph(document);

            hParagraph.AppendText("Header text\r\n").CharacterFormat.FontSize = 14;

            section.HeadersFooters.FirstPageHeader.Paragraphs.Add(hParagraph);


            IWTable hTable = document.LastSection.HeadersFooters.FirstPageHeader.AddTable();

            hTable.ResetCells(2, 2);

            hTable[0, 0].AddParagraph().AppendText("1");
            hTable[0, 1].AddParagraph().AppendText("2");
            hTable[1, 0].AddParagraph().AppendText("3");
            hTable[1, 1].AddParagraph().AppendText("4");


            // --------------------------------------------
            // Tiny table
            // --------------------------------------------

            paragraph = section.AddParagraph();

            paragraph.AppendText("Tiny table\r\n").CharacterFormat.FontSize = 14;
            paragraph = section.AddParagraph();
            WTextBody textBody = section.Body;
            IWTable   table    = textBody.AddTable();

            table.ResetCells(2, 2);

            WTableRow row_0 = table.Rows[0];

            row_0.Cells[0].AddParagraph().AppendText("A");
            row_0.Cells[0].AddParagraph().AppendText("AA");
            row_0.Cells[0].AddParagraph().AppendText("AAA");

            WTableRow row_1 = table.Rows[1];

            row_1.Cells[1].AddParagraph().AppendText("B");
            row_1.Cells[1].AddParagraph().AppendText("BB\r\nBBB");
            row_1.Cells[1].AddParagraph().AppendText("BBB");

            textBody.AddParagraph().AppendText("Text after table...").CharacterFormat.FontSize = 14;

            // --------------------------------------------
            // Table with different formatting
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Table with different formatting\r\n").CharacterFormat.FontSize = 14;
            paragraph = section.AddParagraph();
            textBody  = section.Body;
            table     = textBody.AddTable();
            table.ResetCells(3, 3);

            /* ------- First Row -------- */

            WTableRow row0 = table.Rows[0];

            paragraph = (IWParagraph)row0.Cells[0].AddParagraph();
            textRange = paragraph.AppendText("1");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
            textRange.CharacterFormat.FontName            = "Arial";
            textRange.CharacterFormat.Bold             = true;
            textRange.CharacterFormat.FontSize         = 14f;
            row0.Cells[0].CellFormat.Borders.LineWidth = 2f;
            row0.Cells[0].CellFormat.Borders.Color     = Color.Magenta;

            paragraph = (IWParagraph)row0.Cells[1].AddParagraph();
            textRange = paragraph.AppendText("2");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            textRange.CharacterFormat.Emboss            = true;
            textRange.CharacterFormat.FontSize          = 15f;
            row0.Cells[1].CellFormat.Borders.LineWidth  = 1.3f;
            row0.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DoubleWave;

            paragraph = (IWParagraph)row0.Cells[2].AddParagraph();
            textRange = paragraph.AppendText("3");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
            textRange.CharacterFormat.Engrave             = true;
            textRange.CharacterFormat.FontSize            = 15f;
            row0.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Emboss3D;

            /* ------- Second Row -------- */

            WTableRow row1 = table.Rows[1];

            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            textRange = paragraph.AppendText("4");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            textRange.CharacterFormat.SmallCaps           = true;
            textRange.CharacterFormat.FontName            = "Comic Sans MS";
            textRange.CharacterFormat.FontSize            = 16;
            row1.Cells[0].CellFormat.Borders.LineWidth    = 2f;
            row1.Cells[0].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.DashDotStroker;

            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            textRange = paragraph.AppendText("5");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            textRange.CharacterFormat.FontName            = "Times New Roman";
            textRange.CharacterFormat.Shadow = true;
            textRange.CharacterFormat.TextBackgroundColor = Color.Orange;
            textRange.CharacterFormat.FontSize            = 15f;
            row1.Cells[1].CellFormat.Borders.LineWidth    = 2f;
            row1.Cells[1].CellFormat.Borders.Color        = Color.Brown;

            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            textRange = paragraph.AppendText("6");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            textRange.CharacterFormat.Bold             = true;
            textRange.CharacterFormat.FontSize         = 14f;
            row1.Cells[2].CellFormat.BackColor         = Color.FromArgb(51, 51, 101);
            row1.Cells[2].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;

            /* ------- Third Row -------- */

            WTableRow row2 = table.Rows[2];

            paragraph = (IWParagraph)row2.Cells[0].AddParagraph();
            textRange = paragraph.AppendText("7");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            textRange.CharacterFormat.FontSize            = 13f;
            row2.Cells[0].CellFormat.Borders.LineWidth    = 1.5f;
            row2.Cells[0].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.DashLargeGap;

            paragraph = (IWParagraph)row2.Cells[1].AddParagraph();
            textRange = paragraph.AppendText("8");
            textRange.CharacterFormat.TextColor         = Color.Blue;
            textRange.CharacterFormat.FontSize          = 16f;
            row2.Cells[1].CellFormat.Borders.LineWidth  = 3f;
            row2.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Wave;

            paragraph = (IWParagraph)row2.Cells[2].AddParagraph();
            textRange = paragraph.AppendText("9");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            row2.Cells[2].CellFormat.Borders.LineWidth    = 2f;
            row2.Cells[2].CellFormat.Borders.Color        = Color.Cyan;
            row2.Cells[2].CellFormat.Borders.Shadow       = true;
            row2.Cells[2].CellFormat.Borders.Space        = 20;

            // --------------------------------------------
            // Table Cell Merging.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Table Cell Merging...").CharacterFormat.FontSize = 14;
            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            RowFormat format = new RowFormat();

            format.Paddings.All       = 5;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Dot;
            format.Borders.LineWidth  = 2;

            // Inserting rows to the table.
            table.ResetCells(6, 6, format, 80);

            // Table formatting with cell merging.
            table.Rows[0].Cells[0].CellFormat.HorizontalMerge   = CellMerge.Start;
            table.Rows[0].Cells[1].CellFormat.HorizontalMerge   = CellMerge.Continue;
            table.Rows[0].Cells[0].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            table.Rows[0].Cells[0].CellFormat.BackColor         = Color.FromArgb(218, 230, 246);
            IWParagraph par = table.Rows[0].Cells[0].AddParagraph();

            par.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            par.AppendText("Horizontal Merge").CharacterFormat.Bold = true;


            table.Rows[2].Cells[3].CellFormat.VerticalMerge = CellMerge.Start;
            table.Rows[3].Cells[3].CellFormat.VerticalMerge = CellMerge.Continue;

            table.Rows[2].Cells[3].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            par = table.Rows[2].Cells[3].AddParagraph();
            table.Rows[2].Cells[3].CellFormat.BackColor           = Color.FromArgb(252, 172, 85);
            par.ParagraphFormat.HorizontalAlignment               = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            par.AppendText("Vertical Merge").CharacterFormat.Bold = true;

            #region Table Cell Spacing.
            // --------------------------------------------
            // Table Cell Spacing.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Table Cell spacing...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            format = new RowFormat();

            format.CellSpacing        = 5;
            format.Paddings.All       = 5;
            format.CellSpacing        = 2.5f;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.IsBreakAcrossPages = true;
            table.ResetCells(25, 5, format, 100);
            IWTextRange text;
            table.Rows[0].IsHeader = true;

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Color.FromArgb(203, 211, 226);
            }

            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                    if (i % 2 != 1)
                    {
                        table[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                    }
                    else
                    {
                        table[i, j].CellFormat.BackColor = Color.FromArgb(246, 249, 255);
                    }
                }
            }
            #endregion Table Cell Spacing.

            #region Nested Table
            // --------------------------------------------
            // Nested Table.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.ParagraphFormat.PageBreakBefore = true;
            paragraph.AppendText("Nested Table...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            format = new RowFormat();
            format.Paddings.All       = 5;
            format.CellSpacing        = 2.5f;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            table.ResetCells(5, 3, format, 100);


            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Color.FromArgb(242, 151, 50);
            }
            table[0, 2].Width = 200;
            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    if ((i == 2) && (j == 2))
                    {
                        text = paragraph.AppendText("Nested Table");
                    }

                    else
                    {
                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    }

                    if ((j == 2))
                    {
                        table[i, j].Width = 200;
                    }

                    text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                }
            }

            // Adding a nested Table.
            IWTable nestTable = table[2, 2].AddTable();

            format = new RowFormat();

            format.Borders.BorderType  = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.HorizontalAlignment = RowAlignment.Center;
            nestTable.ResetCells(3, 3, format, 50);

            for (int i = 0; i < nestTable.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = nestTable[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    nestTable[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Color.Black;
                    text.CharacterFormat.Bold      = true;
                }
            }
            #endregion Nested Table

            #region Table with Images

            //Add a new section to the document.
            section = document.AddSection();
            //Add paragraph to the section.
            paragraph = section.AddParagraph();
            //Writing text.
            textRange = paragraph.AppendText("Table with Images");
            textRange.CharacterFormat.FontSize  = 13f;
            textRange.CharacterFormat.TextColor = Color.DarkBlue;
            textRange.CharacterFormat.Bold      = true;

            //Add paragraph to the section.
            section.AddParagraph();
            paragraph = section.AddParagraph();

            text = null;

            //Adding a new Table to the paragraph.
            table = section.Body.AddTable();
            table.ResetCells(1, 3);

            //Adding rows to the table.
            WTableRow row = table.Rows[0];
            //Set heading row height
            row.Height = 25f;
            //set heading values to the Table.
            for (int i = 0; i < 3; i++)
            {
                //Add paragraph for writing Text to the cells.
                paragraph = (IWParagraph)row.Cells[i].AddParagraph();
                //Set Horizontal Alignment as Center.
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                //Writing Row Heading
                switch (i)
                {
                case 0:
                    text = paragraph.AppendText("SNO");
                    row.Cells[i].Width = 50f; break;

                case 1: text = paragraph.AppendText("Drinks"); break;

                case 2: text = paragraph.AppendText("Showcase Image"); row.Cells[i].Width = 200f; break;
                }
                //Set row Heading formatting
                text.CharacterFormat.Bold      = true;
                text.CharacterFormat.FontName  = "Cambria";
                text.CharacterFormat.FontSize  = 11f;
                text.CharacterFormat.TextColor = Color.White;

                //Set row cells formatting
                row.Cells[i].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row.Cells[i].CellFormat.BackColor         = Color.FromArgb(157, 161, 190);

                row.Cells[i].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            }

            int sno = 1;
            //Writing Sno, Product name and Product Images to the Table.

            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Apple Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            Assembly execAssm = typeof(TableFormattingDemo).GetTypeInfo().Assembly;

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();

            Stream imageStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Apple.png");

            paragraph.AppendPicture(imageStream);
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Grape Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();

            imageStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Grape.png");

            paragraph.AppendPicture(imageStream);
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Hot Soup");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();

            imageStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.Soup.png");

            paragraph.AppendPicture(imageStream);
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            #endregion Table with Images
            Save(rdDoc.IsChecked == true, document);
        }
Exemplo n.º 4
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Get Template document and database path.
                string dataPath = Application.StartupPath + @"..\..\..\..\..\..\..\common\Data\DocIO\";
                //Creates an empty Word document instance.
                WordDocument document = new WordDocument();

                //Opens template document.
                document.Open(System.IO.Path.Combine(dataPath, "ContentControlTemplate.docx"));

                IWTextRange textRange;
                //Gets table from the template document.
                IWTable   table = document.LastSection.Tables[0];
                WTableRow row   = table.Rows[1];

                #region Inserting content controls

                #region Calendar content control
                IWParagraph cellPara = row.Cells[0].Paragraphs[0];
                //Accesses the date picker content control.
                IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl);
                textRange = inlineControl.ParagraphItems[0] as WTextRange;
                //Sets today's date to display.
                textRange.Text = DateTime.Now.ToShortDateString();
                textRange.CharacterFormat.FontSize = 14;
                //Protects the content control.
                inlineControl.ContentControlProperties.LockContents = true;
                #endregion

                #region Plain text content controls
                table    = document.LastSection.Tables[1];
                row      = table.Rows[0];
                cellPara = row.Cells[0].LastParagraph;
                //Accesses the plain text content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                //Protects the content control.
                inlineControl.ContentControlProperties.LockContents = true;
                textRange = inlineControl.ParagraphItems[0] as WTextRange;
                //Sets text in plain text content control.
                textRange.Text = "Northwind Analytics";
                textRange.CharacterFormat.FontSize = 14;

                cellPara = row.Cells[1].LastParagraph;
                //Accesses the plain text content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                //Protects the content control.
                inlineControl.ContentControlProperties.LockContents = true;
                textRange = inlineControl.ParagraphItems[0] as WTextRange;
                //Sets text in plain text content control.
                textRange.Text = "Northwind";
                textRange.CharacterFormat.FontSize = 14;

                row      = table.Rows[1];
                cellPara = row.Cells[0].LastParagraph;
                //Accesses the plain text content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                //Protects the content control.
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets text in plain text content control.
                textRange      = inlineControl.ParagraphItems[0] as WTextRange;
                textRange.Text = "10";
                textRange.CharacterFormat.FontSize = 14;


                cellPara = row.Cells[1].LastParagraph;
                //Accesses the plain text content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                //Protects the content control.
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets text in plain text content control.
                textRange      = inlineControl.ParagraphItems[0] as WTextRange;
                textRange.Text = "Nancy Davolio";
                textRange.CharacterFormat.FontSize = 14;
                #endregion

                #region CheckBox Content control
                row      = table.Rows[2];
                cellPara = row.Cells[0].LastParagraph;
                //Inserts checkbox content control.
                inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets checkbox as checked state.
                inlineControl.ContentControlProperties.IsChecked = true;
                textRange = cellPara.AppendText("C#, ");
                textRange.CharacterFormat.FontSize = 14;

                //Inserts checkbox content control.
                inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets checkbox as checked state.
                inlineControl.ContentControlProperties.IsChecked = true;
                textRange = cellPara.AppendText("VB");
                textRange.CharacterFormat.FontSize = 14;
                #endregion


                #region Drop down list content control
                cellPara = row.Cells[1].LastParagraph;
                //Accesses the dropdown list content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets default option to display.
                textRange      = inlineControl.ParagraphItems[0] as WTextRange;
                textRange.Text = "ASP.NET";
                textRange.CharacterFormat.FontSize = 14;
                inlineControl.ParagraphItems.Add(textRange);

                //Adds items to the dropdown list.
                ContentControlListItem item;
                item             = new ContentControlListItem();
                item.DisplayText = "ASP.NET MVC";
                item.Value       = "2";
                inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

                item             = new ContentControlListItem();
                item.DisplayText = "Windows Forms";
                item.Value       = "3";
                inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

                item             = new ContentControlListItem();
                item.DisplayText = "WPF";
                item.Value       = "4";
                inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

                item             = new ContentControlListItem();
                item.DisplayText = "Xamarin";
                item.Value       = "5";
                inlineControl.ContentControlProperties.ContentControlListItems.Add(item);
                #endregion

                #region Calendar content control
                row      = table.Rows[3];
                cellPara = row.Cells[0].LastParagraph;
                //Accesses the date picker content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets default date to display.
                textRange      = inlineControl.ParagraphItems[0] as WTextRange;
                textRange.Text = DateTime.Now.AddDays(-5).ToShortDateString();
                textRange.CharacterFormat.FontSize = 14;

                cellPara = row.Cells[1].LastParagraph;
                //Inserts date picker content control.
                inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
                inlineControl.ContentControlProperties.LockContents = true;
                //Sets default date to display.
                textRange      = inlineControl.ParagraphItems[0] as WTextRange;
                textRange.Text = DateTime.Now.AddDays(10).ToShortDateString();
                textRange.CharacterFormat.FontSize = 14;
                #endregion

                #endregion
                #region Block content control
                //Accesses the block content control.
                BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl);
                //Protects the block content control
                blockContentControl.ContentControlProperties.LockContents = true;
                #endregion

                //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());
                    }
                }

                // Exit
                this.Close();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        public IActionResult FormFillingAndProtection(string Button)
        {
            if (Button == null)
            {
                return(View());
            }
            string basePath     = _hostingEnvironment.WebRootPath;
            string dataPath     = basePath + @"/DocIO/ContentControlTemplate.docx";
            string contenttype1 = "application/vnd.ms-word.document.12";
            // Load Template document stream.
            FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            if (Button == "View Template")
            {
                return(File(fileStream, contenttype1, "ContentControlTemplate.docx"));
            }
            // Creates an empty Word document instance.
            WordDocument document = new WordDocument();

            // Opens template document.
            document.Open(fileStream, FormatType.Docx);
            fileStream.Dispose();
            fileStream = null;
            IWTextRange textRange;
            //Gets table from the template document.
            IWTable   table = document.LastSection.Tables[0];
            WTableRow row   = table.Rows[1];

            #region Inserting content controls

            #region Calendar content control
            IWParagraph cellPara = row.Cells[0].Paragraphs[0];
            //Accesses the date picker content control.
            IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl);
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets today's date to display.
            textRange.Text = DateTime.Now.ToString("d");
            textRange.CharacterFormat.FontSize = 14;
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            #endregion

            #region Plain text content controls
            table    = document.LastSection.Tables[1];
            row      = table.Rows[0];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind Analytics";
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind";
            textRange.CharacterFormat.FontSize = 14;

            row      = table.Rows[1];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "10";
            textRange.CharacterFormat.FontSize = 14;


            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "Nancy Davolio";
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #region CheckBox Content control
            row      = table.Rows[2];
            cellPara = row.Cells[0].LastParagraph;
            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("C#, ");
            textRange.CharacterFormat.FontSize = 14;

            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("VB");
            textRange.CharacterFormat.FontSize = 14;
            #endregion


            #region Drop down list content control
            cellPara = row.Cells[1].LastParagraph;
            //Accesses the dropdown list content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default option to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "ASP.NET";
            textRange.CharacterFormat.FontSize = 14;
            inlineControl.ParagraphItems.Add(textRange);

            //Adds items to the dropdown list.
            ContentControlListItem item;
            item             = new ContentControlListItem();
            item.DisplayText = "ASP.NET MVC";
            item.Value       = "2";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "Windows Forms";
            item.Value       = "3";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "WPF";
            item.Value       = "4";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "Xamarin";
            item.Value       = "5";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);
            #endregion

            #region Calendar content control
            row      = table.Rows[3];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(-5).ToString("d");
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Inserts date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(10).ToString("d");
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #endregion
            #region Block content control
            //Accesses the block content control.
            BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl);
            //Protects the block content control
            blockContentControl.ContentControlProperties.LockContents = true;
            #endregion

            FormatType type        = FormatType.Docx;
            string     filename    = "Sample.docx";
            string     contenttype = "application/vnd.ms-word.document.12";

            MemoryStream ms = new MemoryStream();
            document.Save(ms, type);
            document.Close();
            ms.Position = 0;
            return(File(ms, contenttype, filename));
        }
        /// <summary>
        /// Button click event for FormFillingAndProtection.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event args</param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // Load Template document stream.
            Stream inputStream = execAssm.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.DocIO.DocIO.Assets.ContentControlTemplate.docx");

            // Creates an empty Word document instance.
            WordDocument document = new WordDocument();

            // Opens template document.
            document.Open(inputStream, FormatType.Docx);

            IWTextRange textRange;
            //Gets table from the template document.
            IWTable   table = document.LastSection.Tables[0];
            WTableRow row   = table.Rows[1];

            #region Inserting content controls

            #region Calendar content control
            IWParagraph cellPara = row.Cells[0].Paragraphs[0];
            //Accesses the date picker content control.
            IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl);
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets today's date to display.
            textRange.Text = DateTime.Now.ToString("d");
            textRange.CharacterFormat.FontSize = 14;
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            #endregion

            #region Plain text content controls
            table    = document.LastSection.Tables[1];
            row      = table.Rows[0];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind Analytics";
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind";
            textRange.CharacterFormat.FontSize = 14;

            row      = table.Rows[1];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "10";
            textRange.CharacterFormat.FontSize = 14;


            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "Nancy Davolio";
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #region CheckBox Content control
            row      = table.Rows[2];
            cellPara = row.Cells[0].LastParagraph;
            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("C#, ");
            textRange.CharacterFormat.FontSize = 14;

            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("VB");
            textRange.CharacterFormat.FontSize = 14;
            #endregion


            #region Drop down list content control
            cellPara = row.Cells[1].LastParagraph;
            //Accesses the dropdown list content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default option to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "ASP.NET";
            textRange.CharacterFormat.FontSize = 14;
            inlineControl.ParagraphItems.Add(textRange);

            //Adds items to the dropdown list.
            ContentControlListItem item;
            item             = new ContentControlListItem();
            item.DisplayText = "ASP.NET MVC";
            item.Value       = "2";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "Windows Forms";
            item.Value       = "3";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "WPF";
            item.Value       = "4";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "Xamarin";
            item.Value       = "5";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);
            #endregion

            #region Calendar content control
            row      = table.Rows[3];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(-5).ToString("d");
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Inserts date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(10).ToString("d");
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #endregion
            #region Block content control
            //Accesses the block content control.
            BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl);
            //Protects the block content control
            blockContentControl.ContentControlProperties.LockContents = true;
            #endregion

            Save(document);
        }
        public ActionResult FormatTable(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            // Create a new document.
            WordDocument document = new WordDocument();

            // Adding a new section to the document.
            IWSection section = document.AddSection();

            section.PageSetup.Margins.All        = 50;
            section.PageSetup.DifferentFirstPage = true;
            IWTextRange textRange;
            IWParagraph paragraph = section.AddParagraph();


            #region Table Cell Spacing.
            // --------------------------------------------
            // Table Cell Spacing.
            // --------------------------------------------
            paragraph.AppendText("Table Cell spacing...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            WTextBody textBody = section.Body;

            // Adding a new Table to the textbody.
            IWTable table = textBody.AddTable();
            table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            table.TableFormat.Paddings.All       = 5.4f;
            RowFormat format = new RowFormat();

            format.Paddings.All       = 5;
            format.CellSpacing        = 2;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.IsBreakAcrossPages = true;
            table.ResetCells(25, 5, format, 90);
            IWTextRange text;
            table.Rows[0].IsHeader = true;

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Color.FromArgb(203, 211, 226);
            }

            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                    if (i % 2 != 1)
                    {
                        table[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                    }
                    else
                    {
                        table[i, j].CellFormat.BackColor = Color.FromArgb(246, 249, 255);
                    }
                }
            }
            (table as WTable).AutoFit(AutoFitType.FitToContent);
            #endregion Table Cell Spacing.

            #region Nested Table
            // --------------------------------------------
            // Nested Table.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.ParagraphFormat.PageBreakBefore = true;
            paragraph.AppendText("Nested Table...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            format = new RowFormat();
            format.Paddings.All       = 5;
            format.CellSpacing        = 2.5f;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            table.ResetCells(5, 3, format, 100);

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Color.FromArgb(242, 151, 50);
            }
            table[0, 2].Width = 200;
            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    if ((i == 2) && (j == 2))
                    {
                        text = paragraph.AppendText("Nested Table");
                    }

                    else
                    {
                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    }

                    if ((j == 2))
                    {
                        table[i, j].Width = 200;
                    }

                    text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                }
            }

            // Adding a nested Table.
            IWTable nestTable = table[2, 2].AddTable();

            format = new RowFormat();

            format.Borders.BorderType  = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.HorizontalAlignment = RowAlignment.Center;
            nestTable.ResetCells(3, 3, format, 45);

            for (int i = 0; i < nestTable.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = nestTable[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    nestTable[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Color.Black;
                    text.CharacterFormat.Bold      = true;
                }
            }
            (nestTable as WTable).AutoFit(AutoFitType.FitToContent);
            (table as WTable).AutoFit(AutoFitType.FitToWindow);
            #endregion Nested Table
            #region Table with Images
            string dataPath = ResolveApplicationDataPath("", "Images\\DocIO");

            //Add a new section to the document.
            section = document.AddSection();
            //Add paragraph to the section.
            paragraph = section.AddParagraph();
            //Writing text.
            textRange = paragraph.AppendText("Table with Images");
            textRange.CharacterFormat.FontSize  = 13f;
            textRange.CharacterFormat.TextColor = Color.DarkBlue;
            textRange.CharacterFormat.Bold      = true;

            //Add paragraph to the section.
            section.AddParagraph();
            paragraph = section.AddParagraph();

            text = null;

            //Adding a new Table to the paragraph.
            table = section.Body.AddTable();
            table.ResetCells(1, 3);

            //Adding rows to the table.
            WTableRow row = table.Rows[0];
            //Set heading row height
            row.Height = 25f;
            //set heading values to the Table.
            for (int i = 0; i < 3; i++)
            {
                //Add paragraph for writing Text to the cells.
                paragraph = (IWParagraph)row.Cells[i].AddParagraph();
                //Set Horizontal Alignment as Center.
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                //Writing Row Heading
                switch (i)
                {
                case 0:
                    text = paragraph.AppendText("SNO");
                    row.Cells[i].Width = 50f; break;

                case 1: text = paragraph.AppendText("Drinks"); break;

                case 2: text = paragraph.AppendText("Showcase Image"); row.Cells[i].Width = 200f; break;
                }
                //Set row Heading formatting
                text.CharacterFormat.Bold      = true;
                text.CharacterFormat.FontName  = "Cambria";
                text.CharacterFormat.FontSize  = 11f;
                text.CharacterFormat.TextColor = Color.White;

                //Set row cells formatting
                row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                row.Cells[i].CellFormat.BackColor         = Color.FromArgb(157, 161, 190);

                row.Cells[i].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            }

            int sno = 1;
            //Writing Sno, Product name and Product Images to the Table.

            WTableRow row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Apple Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            paragraph.AppendPicture(Image.FromFile(dataPath + "Apple Juice.png"));
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Grape Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            paragraph.AppendPicture(Image.FromFile(dataPath + "Grape Juice.png"));
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Hot Soup");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            paragraph.AppendPicture(Image.FromFile(dataPath + "Hot Soup.png"));
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            (table as WTable).AutoFit(AutoFitType.FixedColumnWidth);
            #endregion Table with Images

            //Save as .doc format
            if (Group1 == "WordDoc")
            {
                return(document.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            //Save as .docx format
            else 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));
            }
            return(View());
        }
Exemplo n.º 8
0
        private async void CreaTabellaDinamicaWordAsync()
        {
            //Crea una istanza della classe WordDocument
            WordDocument document = new WordDocument();

            IWSection section = document.AddSection();

            section.AddParagraph().AppendText("Price Details");

            section.AddParagraph();

            //Aggiunge una nuova tabella al dovumento Word
            IWTable table = section.AddTable();

            //Aggiunge la prima riga alla tabella
            WTableRow row = table.AddRow();

            //Aggiunge la prima cella nella prima riga
            WTableCell cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("Item");

            //Aggiunge la seconda cella nella prima riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("Price($)");

            //Aggiunge la seconda riga alla tabella
            row = table.AddRow(true, false);

            //Aggiunge la prima cella nella seconda riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("Apple");

            //Aggiunge la seconda cella nella seconda riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("50");

            //Aggiunge la terza riga alla tabella
            row = table.AddRow(true, false);

            //Aggiunge la prima cella nella terza riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("Orange");

            //Aggiunge la seconda cella nella terza riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("30");

            //Aggiunge la quarta riga alla tabella
            row = table.AddRow(true, false);

            //Aggiunge la prima cella nella quarta riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("Banana");

            //Aggiunge la seconda cella nella quarta riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("20");

            //Aggiunge la quinta riga alla tabella
            row = table.AddRow(true, false);

            //Aggiunge la prima cella nella quinta riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("Grapes");

            //Aggiunge la seconda cella nella quinta riga
            cell = row.AddCell();

            //Specifica la larghezza della cella
            cell.Width = 200;

            cell.AddParagraph().AppendText("70");

            ////Saves and closes the document instance
            //document.Save("Table.docx", FormatType.Docx);

            //Salva il documento su memory stream
            MemoryStream stream = new MemoryStream();
            await document.SaveAsync(stream, FormatType.Docx);

            document.Close();

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

            DefaultLaunch("Table.docx");
        }
        public ActionResult FormatTable(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            // Create a new document.
            WordDocument document = new WordDocument();

            // Adding a new section to the document.
            IWSection section = document.AddSection();

            section.PageSetup.DifferentFirstPage = true;
            IWTextRange textRange;
            IWParagraph paragraph = section.AddParagraph();

            // --------------------------------------------
            // Table in page header
            // --------------------------------------------


            IWParagraph hParagraph = new WParagraph(document);

            hParagraph.AppendText("Header text\r\n").CharacterFormat.FontSize = 14;

            section.HeadersFooters.FirstPageHeader.Paragraphs.Add(hParagraph);


            IWTable hTable = document.LastSection.HeadersFooters.FirstPageHeader.AddTable();

            hTable.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            hTable.TableFormat.Paddings.All       = 5.4f;
            hTable.ResetCells(2, 2);

            hTable[0, 0].AddParagraph().AppendText("1");
            hTable[0, 1].AddParagraph().AppendText("2");
            hTable[1, 0].AddParagraph().AppendText("3");
            hTable[1, 1].AddParagraph().AppendText("4");


            // --------------------------------------------
            // Tiny table
            // --------------------------------------------

            paragraph = section.AddParagraph();

            paragraph.AppendText("Tiny table\r\n").CharacterFormat.FontSize = 14;
            paragraph = section.AddParagraph();
            WTextBody textBody = section.Body;
            IWTable   table    = textBody.AddTable();

            table.ResetCells(2, 2);
            table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            table.TableFormat.Paddings.All       = 5.4f;
            WTableRow row_0 = table.Rows[0];

            row_0.Cells[0].AddParagraph().AppendText("A");
            row_0.Cells[0].AddParagraph().AppendText("AA");
            row_0.Cells[0].AddParagraph().AppendText("AAA");

            WTableRow row_1 = table.Rows[1];

            row_1.Cells[1].AddParagraph().AppendText("B");
            row_1.Cells[1].AddParagraph().AppendText("BB\r\nBBB");
            row_1.Cells[1].AddParagraph().AppendText("BBB");

            textBody.AddParagraph().AppendText("Text after table...").CharacterFormat.FontSize = 14;

            // --------------------------------------------
            // Table with different formatting
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Table with different formatting\r\n").CharacterFormat.FontSize = 14;
            paragraph = section.AddParagraph();
            textBody  = section.Body;
            table     = textBody.AddTable();
            table.ResetCells(3, 3);

            /* ------- First Row -------- */

            WTableRow row0 = table.Rows[0];

            paragraph = (IWParagraph)row0.Cells[0].AddParagraph();
            textRange = paragraph.AppendText("1");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
            textRange.CharacterFormat.FontName            = "Arial";
            textRange.CharacterFormat.Bold             = true;
            textRange.CharacterFormat.FontSize         = 14f;
            row0.Cells[0].CellFormat.Borders.LineWidth = 2f;
            row0.Cells[0].CellFormat.Borders.Color     = Color.Magenta;

            paragraph = (IWParagraph)row0.Cells[1].AddParagraph();
            textRange = paragraph.AppendText("2");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            textRange.CharacterFormat.Emboss            = true;
            textRange.CharacterFormat.FontSize          = 15f;
            row0.Cells[1].CellFormat.Borders.LineWidth  = 1.3f;
            row0.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DoubleWave;

            paragraph = (IWParagraph)row0.Cells[2].AddParagraph();
            textRange = paragraph.AppendText("3");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
            textRange.CharacterFormat.Engrave             = true;
            textRange.CharacterFormat.FontSize            = 15f;
            row0.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Emboss3D;

            /* ------- Second Row -------- */

            WTableRow row1 = table.Rows[1];

            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            textRange = paragraph.AppendText("4");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            textRange.CharacterFormat.SmallCaps           = true;
            textRange.CharacterFormat.FontName            = "Comic Sans MS";
            textRange.CharacterFormat.FontSize            = 16;
            row1.Cells[0].CellFormat.Borders.LineWidth    = 2f;
            row1.Cells[0].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.DashDotStroker;

            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            textRange = paragraph.AppendText("5");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            textRange.CharacterFormat.FontName            = "Times New Roman";
            textRange.CharacterFormat.Shadow = true;
            textRange.CharacterFormat.TextBackgroundColor = Color.Orange;
            textRange.CharacterFormat.FontSize            = 15f;
            row1.Cells[1].CellFormat.Borders.LineWidth    = 2f;
            row1.Cells[1].CellFormat.Borders.Color        = Color.Brown;

            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            textRange = paragraph.AppendText("6");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            textRange.CharacterFormat.Bold             = true;
            textRange.CharacterFormat.FontSize         = 14f;
            row1.Cells[2].CellFormat.BackColor         = Color.FromArgb(51, 51, 101);
            row1.Cells[2].CellFormat.VerticalAlignment = VerticalAlignment.Middle;

            /* ------- Third Row -------- */

            WTableRow row2 = table.Rows[2];

            paragraph = (IWParagraph)row2.Cells[0].AddParagraph();
            textRange = paragraph.AppendText("7");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            textRange.CharacterFormat.FontSize            = 13f;
            row2.Cells[0].CellFormat.Borders.LineWidth    = 1.5f;
            row2.Cells[0].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.DashLargeGap;

            paragraph = (IWParagraph)row2.Cells[1].AddParagraph();
            textRange = paragraph.AppendText("8");
            textRange.CharacterFormat.TextColor         = Color.Blue;
            textRange.CharacterFormat.FontSize          = 16f;
            row2.Cells[1].CellFormat.Borders.LineWidth  = 3f;
            row2.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Wave;

            paragraph = (IWParagraph)row2.Cells[2].AddParagraph();
            textRange = paragraph.AppendText("9");
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
            row2.Cells[2].CellFormat.Borders.LineWidth    = 2f;
            row2.Cells[2].CellFormat.Borders.Color        = Color.Cyan;
            row2.Cells[2].CellFormat.Borders.Shadow       = true;
            row2.Cells[2].CellFormat.Borders.Space        = 20;

            // --------------------------------------------
            // Table Cell Merging.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Table Cell Merging...").CharacterFormat.FontSize = 14;
            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            RowFormat format = new RowFormat();

            format.Paddings.All       = 5;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Dot;
            format.Borders.LineWidth  = 2;

            // Inserting rows to the table.
            table.ResetCells(6, 6, format, 80);

            // Table formatting with cell merging.
            table.Rows[0].Cells[0].CellFormat.HorizontalMerge   = CellMerge.Start;
            table.Rows[0].Cells[1].CellFormat.HorizontalMerge   = CellMerge.Continue;
            table.Rows[0].Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            table.Rows[0].Cells[0].CellFormat.BackColor         = Color.FromArgb(218, 230, 246);
            IWParagraph par = table.Rows[0].Cells[0].AddParagraph();

            par.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            par.AppendText("Horizontal Merge").CharacterFormat.Bold = true;


            table.Rows[2].Cells[3].CellFormat.VerticalMerge = CellMerge.Start;
            table.Rows[3].Cells[3].CellFormat.VerticalMerge = CellMerge.Continue;

            table.Rows[2].Cells[3].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            par = table.Rows[2].Cells[3].AddParagraph();
            table.Rows[2].Cells[3].CellFormat.BackColor           = Color.FromArgb(252, 172, 85);
            par.ParagraphFormat.HorizontalAlignment               = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            par.AppendText("Vertical Merge").CharacterFormat.Bold = true;

            #region Table Cell Spacing.
            // --------------------------------------------
            // Table Cell Spacing.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Table Cell spacing...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();
            table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            table.TableFormat.Paddings.All       = 5.4f;
            format = new RowFormat();

            format.Paddings.All       = 5;
            format.CellSpacing        = 2;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.IsBreakAcrossPages = true;
            table.ResetCells(25, 5, format, 90);
            IWTextRange text;
            table.Rows[0].IsHeader = true;

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Color.FromArgb(203, 211, 226);
            }

            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                    if (i % 2 != 1)
                    {
                        table[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                    }
                    else
                    {
                        table[i, j].CellFormat.BackColor = Color.FromArgb(246, 249, 255);
                    }
                }
            }
            #endregion Table Cell Spacing.

            #region Nested Table
            // --------------------------------------------
            // Nested Table.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.ParagraphFormat.PageBreakBefore = true;
            paragraph.AppendText("Nested Table...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            format = new RowFormat();
            format.Paddings.All       = 5;
            format.CellSpacing        = 2.5f;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            table.ResetCells(5, 3, format, 100);

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Color.FromArgb(242, 151, 50);
            }
            table[0, 2].Width = 200;
            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    if ((i == 2) && (j == 2))
                    {
                        text = paragraph.AppendText("Nested Table");
                    }

                    else
                    {
                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    }

                    if ((j == 2))
                    {
                        table[i, j].Width = 200;
                    }

                    text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                }
            }

            // Adding a nested Table.
            IWTable nestTable = table[2, 2].AddTable();

            format = new RowFormat();

            format.Borders.BorderType  = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.HorizontalAlignment = RowAlignment.Center;
            nestTable.ResetCells(3, 3, format, 45);

            for (int i = 0; i < nestTable.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = nestTable[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    nestTable[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Color.Black;
                    text.CharacterFormat.Bold      = true;
                }
            }
            #endregion Nested Table
            #region Table with Images
            string dataPath = ResolveApplicationDataPath("", "Content\\DocIO");

            //Add a new section to the document.
            section = document.AddSection();
            //Add paragraph to the section.
            paragraph = section.AddParagraph();
            //Writing text.
            textRange = paragraph.AppendText("Table with Images");
            textRange.CharacterFormat.FontSize  = 13f;
            textRange.CharacterFormat.TextColor = Color.DarkBlue;
            textRange.CharacterFormat.Bold      = true;

            //Add paragraph to the section.
            section.AddParagraph();
            paragraph = section.AddParagraph();

            text = null;

            //Adding a new Table to the paragraph.
            table = section.Body.AddTable();
            table.ResetCells(1, 3);

            //Adding rows to the table.
            WTableRow row = table.Rows[0];
            //Set heading row height
            row.Height = 25f;
            //set heading values to the Table.
            for (int i = 0; i < 3; i++)
            {
                //Add paragraph for writing Text to the cells.
                paragraph = (IWParagraph)row.Cells[i].AddParagraph();
                //Set Horizontal Alignment as Center.
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                //Writing Row Heading
                switch (i)
                {
                case 0: text           = paragraph.AppendText("SNO");
                    row.Cells[i].Width = 50f; break;

                case 1: text = paragraph.AppendText("Drinks"); break;

                case 2: text = paragraph.AppendText("Showcase Image"); row.Cells[i].Width = 200f; break;
                }
                //Set row Heading formatting
                text.CharacterFormat.Bold      = true;
                text.CharacterFormat.FontName  = "Cambria";
                text.CharacterFormat.FontSize  = 11f;
                text.CharacterFormat.TextColor = Color.White;

                //Set row cells formatting
                row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                row.Cells[i].CellFormat.BackColor         = Color.FromArgb(157, 161, 190);

                row.Cells[i].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            }

            int sno = 1;
            //Writing Sno, Product name and Product Images to the Table.

            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Apple Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            paragraph.AppendPicture(Image.FromFile(dataPath + "Apple Juice.png"));
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Grape Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            paragraph.AppendPicture(Image.FromFile(dataPath + "Grape Juice.png"));
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Hot Soup");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            paragraph.AppendPicture(Image.FromFile(dataPath + "Hot Soup.png"));
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
            sno++;
            #endregion Table with Images

            //Save as .doc format
            if (Group1 == "WordDoc")
            {
                return(document.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            //Save as .docx format
            else 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));
            }
            return(View());
        }
Exemplo n.º 10
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Create a new document.
                WordDocument document = new WordDocument();

                // Adding a new section to the document.
                IWSection section = document.AddSection();
                section.PageSetup.DifferentFirstPage = true;
                IWTextRange textRange;
                IWParagraph paragraph = section.AddParagraph();

                // --------------------------------------------
                // Table in page header
                // --------------------------------------------


                IWParagraph hParagraph = new WParagraph(document);
                hParagraph.AppendText("Header text\r\n").CharacterFormat.FontSize = 14;

                section.HeadersFooters.FirstPageHeader.Paragraphs.Add(hParagraph);


                IWTable hTable = document.LastSection.HeadersFooters.FirstPageHeader.AddTable();

                hTable.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                hTable.TableFormat.Paddings.All       = 5.4f;
                hTable.ResetCells(2, 2);

                hTable[0, 0].AddParagraph().AppendText("1");
                hTable[0, 1].AddParagraph().AppendText("2");
                hTable[1, 0].AddParagraph().AppendText("3");
                hTable[1, 1].AddParagraph().AppendText("4");


                // --------------------------------------------
                // Tiny table
                // --------------------------------------------

                paragraph = section.AddParagraph();

                paragraph.AppendText("Tiny table\r\n").CharacterFormat.FontSize = 14;
                paragraph = section.AddParagraph();
                WTextBody textBody = section.Body;
                IWTable   table    = textBody.AddTable();
                table.ResetCells(2, 2);

                table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                table.TableFormat.Paddings.All       = 5.4f;
                WTableRow row_0 = table.Rows[0];
                row_0.Cells[0].AddParagraph().AppendText("A");
                row_0.Cells[0].AddParagraph().AppendText("AA");
                row_0.Cells[0].AddParagraph().AppendText("AAA");

                WTableRow row_1 = table.Rows[1];
                row_1.Cells[1].AddParagraph().AppendText("B");
                row_1.Cells[1].AddParagraph().AppendText("BB\r\nBBB");
                row_1.Cells[1].AddParagraph().AppendText("BBB");

                textBody.AddParagraph().AppendText("Text after table...").CharacterFormat.FontSize = 14;

                // --------------------------------------------
                // Table with different formatting
                // --------------------------------------------

                section.AddParagraph();
                paragraph = section.AddParagraph();
                paragraph.AppendText("Table with different formatting\r\n").CharacterFormat.FontSize = 14;
                paragraph = section.AddParagraph();
                textBody  = section.Body;
                table     = textBody.AddTable();
                table.ResetCells(3, 3);

                /* ------- First Row -------- */

                WTableRow row0 = table.Rows[0];

                paragraph = (IWParagraph)row0.Cells[0].AddParagraph();
                textRange = paragraph.AppendText("1");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
                textRange.CharacterFormat.FontName            = "Arial";
                textRange.CharacterFormat.Bold             = true;
                textRange.CharacterFormat.FontSize         = 14f;
                row0.Cells[0].CellFormat.Borders.LineWidth = 2f;
                row0.Cells[0].CellFormat.Borders.Color     = Color.Magenta;

                paragraph = (IWParagraph)row0.Cells[1].AddParagraph();
                textRange = paragraph.AppendText("2");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
                textRange.CharacterFormat.Emboss            = true;
                textRange.CharacterFormat.FontSize          = 15f;
                row0.Cells[1].CellFormat.Borders.LineWidth  = 1.3f;
                row0.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DoubleWave;

                paragraph = (IWParagraph)row0.Cells[2].AddParagraph();
                textRange = paragraph.AppendText("3");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Left;
                textRange.CharacterFormat.Engrave             = true;
                textRange.CharacterFormat.FontSize            = 15f;
                row0.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Emboss3D;

                /* ------- Second Row -------- */

                WTableRow row1 = table.Rows[1];

                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                textRange = paragraph.AppendText("4");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                textRange.CharacterFormat.SmallCaps           = true;
                textRange.CharacterFormat.FontName            = "Comic Sans MS";
                textRange.CharacterFormat.FontSize            = 16;
                row1.Cells[0].CellFormat.Borders.LineWidth    = 2f;
                row1.Cells[0].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.DashDotStroker;

                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                textRange = paragraph.AppendText("5");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                textRange.CharacterFormat.FontName            = "Times New Roman";
                textRange.CharacterFormat.Shadow = true;
                textRange.CharacterFormat.TextBackgroundColor = Color.Orange;
                textRange.CharacterFormat.FontSize            = 15f;
                row1.Cells[1].CellFormat.Borders.LineWidth    = 2f;
                row1.Cells[1].CellFormat.Borders.Color        = Color.Brown;

                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                textRange = paragraph.AppendText("6");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                textRange.CharacterFormat.Bold             = true;
                textRange.CharacterFormat.FontSize         = 14f;
                row1.Cells[2].CellFormat.BackColor         = Color.FromArgb(51, 51, 101);
                row1.Cells[2].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;

                /* ------- Third Row -------- */

                WTableRow row2 = table.Rows[2];

                paragraph = (IWParagraph)row2.Cells[0].AddParagraph();
                textRange = paragraph.AppendText("7");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
                textRange.CharacterFormat.FontSize            = 13f;
                row2.Cells[0].CellFormat.Borders.LineWidth    = 1.5f;
                row2.Cells[0].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.DashLargeGap;

                paragraph = (IWParagraph)row2.Cells[1].AddParagraph();
                textRange = paragraph.AppendText("8");
                textRange.CharacterFormat.TextColor         = Color.Blue;
                textRange.CharacterFormat.FontSize          = 16f;
                row2.Cells[1].CellFormat.Borders.LineWidth  = 3f;
                row2.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Wave;

                paragraph = (IWParagraph)row2.Cells[2].AddParagraph();
                textRange = paragraph.AppendText("9");
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Right;
                row2.Cells[2].CellFormat.Borders.LineWidth    = 2f;
                row2.Cells[2].CellFormat.Borders.Color        = Color.Cyan;
                row2.Cells[2].CellFormat.Borders.Shadow       = true;
                row2.Cells[2].CellFormat.Borders.Space        = 20;

                // --------------------------------------------
                // Table Cell Merging.
                // --------------------------------------------

                section.AddParagraph();
                paragraph = section.AddParagraph();
                paragraph.AppendText("Table Cell Merging...").CharacterFormat.FontSize = 14;
                section.AddParagraph();
                paragraph = section.AddParagraph();
                textBody  = section.Body;

                // Adding a new Table to the textbody.
                table = textBody.AddTable();
                table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                table.TableFormat.Paddings.All       = 5.4f;

                RowFormat format = new RowFormat();
                format.Paddings.All       = 5;
                format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Dot;
                format.Borders.LineWidth  = 2;

                // Inserting rows to the table.
                table.ResetCells(6, 6, format, 80);

                // Table formatting with cell merging.
                table.Rows[0].Cells[0].CellFormat.HorizontalMerge   = CellMerge.Start;
                table.Rows[0].Cells[1].CellFormat.HorizontalMerge   = CellMerge.Continue;
                table.Rows[0].Cells[0].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                table.Rows[0].Cells[0].CellFormat.BackColor         = Color.FromArgb(218, 230, 246);
                IWParagraph par = table.Rows[0].Cells[0].AddParagraph();
                par.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                par.AppendText("Horizontal Merge").CharacterFormat.Bold = true;


                table.Rows[2].Cells[3].CellFormat.VerticalMerge = CellMerge.Start;
                table.Rows[3].Cells[3].CellFormat.VerticalMerge = CellMerge.Continue;

                table.Rows[2].Cells[3].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                par = table.Rows[2].Cells[3].AddParagraph();
                table.Rows[2].Cells[3].CellFormat.BackColor           = Color.FromArgb(252, 172, 85);
                par.ParagraphFormat.HorizontalAlignment               = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                par.AppendText("Vertical Merge").CharacterFormat.Bold = true;

                #region Table Cell Spacing.
                // --------------------------------------------
                // Table Cell Spacing.
                // --------------------------------------------

                section.AddParagraph();
                paragraph = section.AddParagraph();
                paragraph.AppendText("Table Cell spacing...").CharacterFormat.FontSize = 14;

                section.AddParagraph();
                paragraph = section.AddParagraph();
                textBody  = section.Body;

                // Adding a new Table to the textbody.
                table = textBody.AddTable();

                format = new RowFormat();

                format.Paddings.All       = 5;
                format.CellSpacing        = 2;
                format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
                format.IsBreakAcrossPages = true;
                table.ResetCells(25, 5, format, 90);
                IWTextRange text;
                table.Rows[0].IsHeader = true;

                for (int i = 0; i < table.Rows[0].Cells.Count; i++)
                {
                    paragraph = table[0, i].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                    text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                    text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                    text.CharacterFormat.Bold        = true;
                    text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                    table[0, i].CellFormat.BackColor = Color.FromArgb(203, 211, 226);
                }

                for (int i = 1; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        paragraph = table[i, j].AddParagraph() as WParagraph;
                        paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                        text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                        text.CharacterFormat.Bold      = true;
                        if (i % 2 != 1)
                        {
                            table[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                        }
                        else
                        {
                            table[i, j].CellFormat.BackColor = Color.FromArgb(246, 249, 255);
                        }
                    }
                }
                #endregion Table Cell Spacing.

                #region Nested Table
                // --------------------------------------------
                // Nested Table.
                // --------------------------------------------

                section.AddParagraph();
                paragraph = section.AddParagraph();
                paragraph.ParagraphFormat.PageBreakBefore = true;
                paragraph.AppendText("Nested Table...").CharacterFormat.FontSize = 14;

                section.AddParagraph();
                paragraph = section.AddParagraph();
                textBody  = section.Body;

                // Adding a new Table to the textbody.
                table = textBody.AddTable();

                format = new RowFormat();
                format.Paddings.All       = 5;
                format.CellSpacing        = 2.5f;
                format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
                table.ResetCells(5, 3, format, 100);


                for (int i = 0; i < table.Rows[0].Cells.Count; i++)
                {
                    paragraph = table[0, i].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                    text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                    text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                    text.CharacterFormat.Bold        = true;
                    text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                    table[0, i].CellFormat.BackColor = Color.FromArgb(242, 151, 50);
                }
                table[0, 2].Width = 200;
                for (int i = 1; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        paragraph = table[i, j].AddParagraph() as WParagraph;
                        paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        if ((i == 2) && (j == 2))
                        {
                            text = paragraph.AppendText("Nested Table");
                        }

                        else
                        {
                            text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                        }

                        if ((j == 2))
                        {
                            table[i, j].Width = 200;
                        }

                        text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                        text.CharacterFormat.Bold      = true;
                    }
                }

                // Adding a nested Table.
                IWTable nestTable = table[2, 2].AddTable();

                format = new RowFormat();

                format.Borders.BorderType  = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
                format.HorizontalAlignment = RowAlignment.Center;
                nestTable.ResetCells(3, 3, format, 45);

                for (int i = 0; i < nestTable.Rows.Count; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        paragraph = nestTable[i, j].AddParagraph() as WParagraph;
                        paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        nestTable[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                        text.CharacterFormat.TextColor = Color.Black;
                        text.CharacterFormat.Bold      = true;
                    }
                }
                #endregion Nested Table

                #region Table with Images
                string dataPath = Application.StartupPath + @"\..\..\..\..\..\..\..\Common\images\DocIO\";

                //Add a new section to the document.
                section = document.AddSection();
                //Add paragraph to the section.
                paragraph = section.AddParagraph();
                //Writing text.
                textRange = paragraph.AppendText("Table with Images");
                textRange.CharacterFormat.FontSize  = 13f;
                textRange.CharacterFormat.TextColor = Color.DarkBlue;
                textRange.CharacterFormat.Bold      = true;

                //Add paragraph to the section.
                section.AddParagraph();
                paragraph = section.AddParagraph();

                text = null;

                //Adding a new Table to the paragraph.
                table = section.Body.AddTable();
                table.ResetCells(1, 3);

                //Adding rows to the table.
                WTableRow row = table.Rows[0];
                //Set heading row height
                row.Height = 25f;
                //set heading values to the Table.
                for (int i = 0; i < 3; i++)
                {
                    //Add paragraph for writing Text to the cells.
                    paragraph = (IWParagraph)row.Cells[i].AddParagraph();
                    //Set Horizontal Alignment as Center.
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                    //Writing Row Heading
                    switch (i)
                    {
                    case 0: text           = paragraph.AppendText("SNO");
                        row.Cells[i].Width = 50f; break;

                    case 1: text = paragraph.AppendText("Drinks"); break;

                    case 2: text = paragraph.AppendText("Showcase Image"); row.Cells[i].Width = 200f; break;
                    }
                    //Set row Heading formatting
                    text.CharacterFormat.Bold      = true;
                    text.CharacterFormat.FontName  = "Cambria";
                    text.CharacterFormat.FontSize  = 11f;
                    text.CharacterFormat.TextColor = Color.White;

                    //Set row cells formatting
                    row.Cells[i].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                    row.Cells[i].CellFormat.BackColor         = Color.FromArgb(157, 161, 190);

                    row.Cells[i].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                }

                int sno = 1;
                //Writing Sno, Product name and Product Images to the Table.

                row1 = table.AddRow(false);

                //Writing SNO to the table with formatting text.
                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(sno.ToString());
                text.CharacterFormat.Bold     = true;
                text.CharacterFormat.FontSize = 10f;
                row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
                //Writing Product Name to the table with Formatting.
                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText("Apple Juice");
                text.CharacterFormat.Bold                   = true;
                text.CharacterFormat.FontSize               = 10f;
                text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
                row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

                //Writing Product Images to the Table.
                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                paragraph.AppendPicture(Image.FromFile(dataPath + "Apple Juice.png"));
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
                sno++;
                row1 = table.AddRow(false);

                //Writing SNO to the table with formatting text.
                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(sno.ToString());
                text.CharacterFormat.Bold     = true;
                text.CharacterFormat.FontSize = 10f;
                row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
                //Writing Product Name to the table with Formatting.
                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText("Grape Juice");
                text.CharacterFormat.Bold                   = true;
                text.CharacterFormat.FontSize               = 10f;
                text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
                row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

                //Writing Product Images to the Table.
                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                paragraph.AppendPicture(Image.FromFile(dataPath + "Grape Juice.png"));
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
                sno++;
                row1 = table.AddRow(false);

                //Writing SNO to the table with formatting text.
                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(sno.ToString());
                text.CharacterFormat.Bold     = true;
                text.CharacterFormat.FontSize = 10f;
                row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
                //Writing Product Name to the table with Formatting.
                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText("Hot Soup");
                text.CharacterFormat.Bold                   = true;
                text.CharacterFormat.FontSize               = 10f;
                text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
                row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

                //Writing Product Images to the Table.
                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                paragraph.AppendPicture(Image.FromFile(dataPath + "Hot Soup.png"));
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
                sno++;
                #endregion Table with Images
                //Save as doc format
                if (wordDocRadioBtn.Checked)
                {
                    //Saving the document to disk.
                    document.Save("Sample.doc");

                    //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)
                    {
                        //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer]
                        System.Diagnostics.Process.Start("Sample.doc");
                        //Exit
                        this.Close();
                    }
                }
                //Save as docx format
                else 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();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
Exemplo n.º 11
0
        public ActionResult Forms(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            #region CreateForm
            // Create a new document.
            WordDocument document = new WordDocument();
            // Adding a new section to the document.
            IWSection section = document.AddSection();
            // Adding a new paragraph to the section.
            IWParagraph paragraph = section.AddParagraph();
            #region Document formatting
            //Set background color.
            document.Background.Gradient.Color1         = Syncfusion.Drawing.Color.FromArgb(232, 232, 232);
            document.Background.Gradient.Color2         = Syncfusion.Drawing.Color.FromArgb(255, 255, 255);
            document.Background.Type                    = BackgroundType.Gradient;
            document.Background.Gradient.ShadingStyle   = GradientShadingStyle.Horizontal;
            document.Background.Gradient.ShadingVariant = GradientShadingVariant.ShadingDown;

            section.PageSetup.Margins.All = 30f;
            section.PageSetup.PageSize    = new Syncfusion.Drawing.SizeF(600, 600f);
            #endregion

            #region Title Section
            IWTable table = section.Body.AddTable();
            table.ResetCells(1, 2);

            WTableRow row = table.Rows[0];
            row.Height = 25f;

            IWParagraph cellPara    = row.Cells[0].AddParagraph();
            string      basePath    = _hostingEnvironment.WebRootPath;
            FileStream  imageStream = new FileStream(basePath + @"/images/DocIO/Image.jpg", FileMode.Open, FileAccess.Read);
            IWPicture   pic         = cellPara.AppendPicture(imageStream);
            pic.Height = 80;
            pic.Width  = 180;

            cellPara = row.Cells[1].AddParagraph();
            row.Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            row.Cells[1].CellFormat.BackColor         = Syncfusion.Drawing.Color.FromArgb(173, 215, 255);
            IWTextRange txt = cellPara.AppendText("Job Application Form");
            cellPara.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            txt.CharacterFormat.Bold     = true;
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 18f;

            row.Cells[0].Width = 200;
            row.Cells[1].Width = 300;
            //row.Cells[1].CellFormat.FitText = true;
            row.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
            #endregion

            section.AddParagraph();

            #region General Information
            table = section.Body.AddTable();
            table.TableFormat.Paddings.All = 5.4f;
            table.ResetCells(2, 1);
            row                = table.Rows[0];
            row.Height         = 20;
            row.Cells[0].Width = 500;
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Thick;
            row.Cells[0].CellFormat.Borders.Color      = Syncfusion.Drawing.Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(198, 227, 255);
            row.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            txt = cellPara.AppendText(" General Information");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.Bold     = true;
            txt.CharacterFormat.FontSize = 11f;

            row                = table.Rows[1];
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].Width = 500;
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
            row.Cells[0].CellFormat.Borders.Color      = Syncfusion.Drawing.Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(222, 239, 255);

            txt = cellPara.AppendText("\n Full Name:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            WTextFormField txtField = cellPara.AppendTextFormField("John");
            txtField.TextRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Birth Date:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("BirthDayField", DateTime.Now.ToString("M/d/yyyy"));
            txtField.StringFormat = "M/d/yyyy";
            txtField.Type         = TextFormFieldType.DateText;
            txtField.TextRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;
            txtField.CharacterFormat.TextColor           = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.CharacterFormat.FontName            = "Arial";
            txtField.CharacterFormat.FontSize            = 11f;

            txt = cellPara.AppendText("\n\n Address:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField      = cellPara.AppendTextFormField("221b Baker Street");
            txtField.Type = TextFormFieldType.RegularText;
            txtField.TextRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Phone:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("(206)555-3412");
            txtField.TextRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Email:\t\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("*****@*****.**");
            txtField.TextRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;
            cellPara.AppendText("\n");
            #endregion

            section.AddParagraph();

            #region Educational Qualification
            table = section.Body.AddTable();
            table.ResetCells(2, 1);
            table.TableFormat.Paddings.All = 5.4f;
            row                = table.Rows[0];
            row.Height         = 20;
            row.Cells[0].Width = 500;
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Thick;
            row.Cells[0].CellFormat.Borders.Color      = Syncfusion.Drawing.Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(198, 227, 255);
            row.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            txt = cellPara.AppendText(" Educational Qualification");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.Bold     = true;
            txt.CharacterFormat.FontSize = 11f;

            row                = table.Rows[1];
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].Width = 500;
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
            row.Cells[0].CellFormat.Borders.Color      = Syncfusion.Drawing.Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(222, 239, 255);

            txt = cellPara.AppendText("\n Type:\t\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            WDropDownFormField dropField = cellPara.AppendDropDownFormField();
            dropField.DropDownItems.Add("Higher");
            dropField.DropDownItems.Add("Vocational");
            dropField.DropDownItems.Add("Universal");
            dropField.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            dropField.CharacterFormat.FontName  = "Arial";
            dropField.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Institution:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("Michigan University");
            txtField.TextRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.CharacterFormat.FontSize            = 11f;

            txt = cellPara.AppendText("\n\n Programming Languages:");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txt = cellPara.AppendText("\n\n\t C#:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 9f;
            dropField = cellPara.AppendDropDownFormField();
            dropField.DropDownItems.Add("Perfect");
            dropField.DropDownItems.Add("Good");
            dropField.DropDownItems.Add("Excellent");
            dropField.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            dropField.CharacterFormat.FontName  = "Arial";
            dropField.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n\t VB:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 9f;
            dropField = cellPara.AppendDropDownFormField();
            dropField.DropDownItems.Add("Perfect");
            dropField.DropDownItems.Add("Good");
            dropField.DropDownItems.Add("Excellent");
            dropField.CharacterFormat.TextColor = Syncfusion.Drawing.Color.MidnightBlue;
            dropField.CharacterFormat.FontName  = "Arial";
            dropField.CharacterFormat.FontSize  = 11f;
            #endregion
            //Protect document
            document.ProtectionType = ProtectionType.AllowOnlyFormFields;
            MemoryStream st = new MemoryStream();
            document.Save(st, FormatType.Doc);
            st.Seek(0, SeekOrigin.Begin);
            #endregion CreateForm

            #region FillForm
            // Create a new document.
            WordDocument       document1 = new WordDocument(st, FormatType.Doc);
            IWSection          sec       = document1.LastSection;
            WTextFormField     textFF;
            WDropDownFormField dropFF;

            //Access the text field
            textFF = sec.Body.FormFields[0] as WTextFormField;

            //Fill value for the textfield
            textFF.TextRange.Text = "John";

            //Access the form field with feild name
            textFF = sec.Body.FormFields["BirthDayField"] as WTextFormField;
            textFF.TextRange.Text = "5.13.1980";

            textFF = sec.Body.FormFields[2] as WTextFormField;
            textFF.TextRange.Text = "221b Baker Street";

            textFF = sec.Body.FormFields[3] as WTextFormField;
            textFF.TextRange.Text = "(206)555-3412";

            textFF = sec.Body.FormFields[4] as WTextFormField;
            textFF.TextRange.Text = "*****@*****.**";

            dropFF = sec.Body.FormFields[5] as WDropDownFormField;

            //Set the value
            dropFF.DropDownSelectedIndex = 1;

            textFF = sec.Body.FormFields[6] as WTextFormField;
            textFF.TextRange.Text = "Michigan University";

            dropFF = sec.Body.FormFields[7] as WDropDownFormField;
            dropFF.DropDownSelectedIndex = 1;

            dropFF = sec.Body.FormFields[8] as WDropDownFormField;
            dropFF.DropDownSelectedIndex = 2;

            //Allow only to fill the form.
            document1.ProtectionType = ProtectionType.AllowOnlyFormFields;
            #endregion FillForm

            FormatType type        = FormatType.Docx;
            string     filename    = "Sample.docx";
            string     contenttype = "application/vnd.ms-word.document.12";
            #region Document SaveOption
            //Save as .doc format
            if (Group1 == "WordDoc")
            {
                type        = FormatType.Doc;
                filename    = "Sample.doc";
                contenttype = "application/msword";
            }
            //Save as .xml format
            else 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));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Imports the data to the table row.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">reader</exception>
        /// <exception cref="XmlException">Unexpected xml tag  + reader.LocalName</exception>
        private static void ImportDataToRow(XmlReader reader, WTableRow tableRow)
        {
            if (reader == null)
            {
                throw new Exception("reader");
            }
            while (reader.NodeType != XmlNodeType.Element)
            {
                reader.Read();
            }
            if (reader.LocalName != "Suppliers")
            {
                throw new XmlException("Unexpected xml tag " + reader.LocalName);
            }
            reader.Read();
            while (reader.NodeType == XmlNodeType.Whitespace)
            {
                reader.Read();
            }
            while (reader.LocalName != "Suppliers")
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.LocalName)
                    {
                    case "SupplierID":
                        tableRow.Cells[0].AddParagraph().AppendText(reader.ReadElementContentAsString());
                        break;

                    case "CompanyName":
                        tableRow.Cells[1].AddParagraph().AppendText(reader.ReadElementContentAsString());
                        break;

                    case "ContactName":
                        tableRow.Cells[2].AddParagraph().AppendText(reader.ReadElementContentAsString());
                        break;

                    case "Address":
                        tableRow.Cells[3].AddParagraph().AppendText(reader.ReadElementContentAsString());
                        break;

                    case "City":
                        tableRow.Cells[4].AddParagraph().AppendText(reader.ReadElementContentAsString());
                        break;

                    case "Country":
                        tableRow.Cells[5].AddParagraph().AppendText(reader.ReadElementContentAsString());
                        break;

                    default:
                        reader.Skip();
                        break;
                    }
                }
                else
                {
                    reader.Read();
                    if ((reader.LocalName == "Suppliers") && reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                }
            }
        }
Exemplo n.º 13
0
        private async void FormattaTabellaWord2Async()
        {
            //Creates an instance of WordDocument class
            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;

            //Accede all'istanza della prima riga della tabella
            WTableRow row = table.Rows[0];

            //Specifica l'altezza della riga
            row.Height = 20;

            //Specifica il tipo di atezza riga
            row.HeightType = TableRowHeightType.AtLeast;

            //Accede all'istanza della prima cella della riga
            WTableCell cell = row.Cells[0];

            //Specifica il back ground color della cella
            cell.CellFormat.BackColor = Color.FromArgb(192, 192, 192);

            //Specifica lo stesso padding della tabella come false per preservare il cell padding corrente
            cell.CellFormat.SamePaddingsAsTable = false;

            //Specifica il left, right, top e bottom padding della cella
            cell.CellFormat.Paddings.Left   = 5;
            cell.CellFormat.Paddings.Right  = 5;
            cell.CellFormat.Paddings.Top    = 5;
            cell.CellFormat.Paddings.Bottom = 5;

            //Specifica l'allineamento verticale del contenuto del testo
            cell.CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;

            //Accede all'istanza della seconda cella della riga
            cell = row.Cells[1];

            cell.CellFormat.BackColor = Color.FromArgb(192, 192, 192);

            cell.CellFormat.SamePaddingsAsTable = false;

            //Specifica il left, right, top e bottom padding della cella
            cell.CellFormat.Paddings.All = 5;

            cell.CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;

            //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, "TableCellFormatting.docx");

            DefaultLaunch("TableCellFormatting.docx");
        }
        public ActionResult FormatTable(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            // Create a new document.
            WordDocument document = new WordDocument();
            // Adding a new section to the document.
            IWSection section = document.AddSection();

            section.PageSetup.Margins.All        = 50;
            section.PageSetup.DifferentFirstPage = true;
            IWTextRange textRange;
            IWParagraph paragraph = section.AddParagraph();


            #region Table Cell Spacing.
            // --------------------------------------------
            // Table Cell Spacing.
            // --------------------------------------------
            paragraph.AppendText("Table Cell spacing...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            WTextBody textBody = section.Body;

            // Adding a new Table to the textbody.
            IWTable table = textBody.AddTable();
            table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            table.TableFormat.Paddings.All       = 5.4f;
            RowFormat format = new RowFormat();

            format.Paddings.All       = 5;
            format.CellSpacing        = 2;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.IsBreakAcrossPages = true;
            table.ResetCells(25, 5, format, 90);
            IWTextRange text;
            table.Rows[0].IsHeader = true;

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Syncfusion.Drawing.Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Syncfusion.Drawing.Color.FromArgb(203, 211, 226);
            }

            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                    if (i % 2 != 1)
                    {
                        table[i, j].CellFormat.BackColor = Syncfusion.Drawing.Color.FromArgb(231, 235, 245);
                    }
                    else
                    {
                        table[i, j].CellFormat.BackColor = Syncfusion.Drawing.Color.FromArgb(246, 249, 255);
                    }
                }
            }
            (table as WTable).AutoFit(AutoFitType.FitToContent);
            #endregion Table Cell Spacing.

            #region Nested Table
            // --------------------------------------------
            // Nested Table.
            // --------------------------------------------

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.ParagraphFormat.PageBreakBefore = true;
            paragraph.AppendText("Nested Table...").CharacterFormat.FontSize = 14;

            section.AddParagraph();
            paragraph = section.AddParagraph();
            textBody  = section.Body;

            // Adding a new Table to the textbody.
            table = textBody.AddTable();

            format = new RowFormat();
            format.Paddings.All       = 5;
            format.CellSpacing        = 2.5f;
            format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            table.ResetCells(5, 3, format, 100);

            for (int i = 0; i < table.Rows[0].Cells.Count; i++)
            {
                paragraph = table[0, i].AddParagraph() as WParagraph;
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                text.CharacterFormat.Bold        = true;
                text.CharacterFormat.TextColor   = Syncfusion.Drawing.Color.FromArgb(0, 21, 84);
                table[0, i].CellFormat.BackColor = Syncfusion.Drawing.Color.FromArgb(242, 151, 50);
            }
            table[0, 2].Width = 200;
            for (int i = 1; i < table.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = table[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    if ((i == 2) && (j == 2))
                    {
                        text = paragraph.AppendText("Nested Table");
                    }

                    else
                    {
                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    }

                    if ((j == 2))
                    {
                        table[i, j].Width = 200;
                    }

                    text.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(242, 151, 50);
                    text.CharacterFormat.Bold      = true;
                }
            }

            // Adding a nested Table.
            IWTable nestTable = table[2, 2].AddTable();

            format = new RowFormat();

            format.Borders.BorderType  = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
            format.HorizontalAlignment = RowAlignment.Center;
            nestTable.ResetCells(3, 3, format, 45);

            for (int i = 0; i < nestTable.Rows.Count; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    paragraph = nestTable[i, j].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                    nestTable[i, j].CellFormat.BackColor = Syncfusion.Drawing.Color.FromArgb(231, 235, 245);
                    text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                    text.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Black;
                    text.CharacterFormat.Bold      = true;
                }
            }
            (nestTable as WTable).AutoFit(AutoFitType.FitToContent);
            (table as WTable).AutoFit(AutoFitType.FitToWindow);
            #endregion Nested Table
            #region Table with Images

            //Add a new section to the document.
            section = document.AddSection();
            //Add paragraph to the section.
            paragraph = section.AddParagraph();
            //Writing text.
            textRange = paragraph.AppendText("Table with Images");
            textRange.CharacterFormat.FontSize  = 13f;
            textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.DarkBlue;
            textRange.CharacterFormat.Bold      = true;

            //Add paragraph to the section.
            section.AddParagraph();
            paragraph = section.AddParagraph();

            text = null;

            //Adding a new Table to the paragraph.
            table = section.Body.AddTable();
            table.ResetCells(1, 3);

            //Adding rows to the table.
            WTableRow row = table.Rows[0];
            //Set heading row height
            row.Height = 25f;
            //set heading values to the Table.
            for (int i = 0; i < 3; i++)
            {
                //Add paragraph for writing Text to the cells.
                paragraph = (IWParagraph)row.Cells[i].AddParagraph();
                //Set Horizontal Alignment as Center.
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                //Writing Row Heading
                switch (i)
                {
                case 0:
                    text = paragraph.AppendText("SNO");
                    row.Cells[i].Width = 50f; break;

                case 1: text = paragraph.AppendText("Drinks"); break;

                case 2: text = paragraph.AppendText("Showcase Image"); row.Cells[i].Width = 200f; break;
                }
                //Set row Heading formatting
                text.CharacterFormat.Bold      = true;
                text.CharacterFormat.FontName  = "Cambria";
                text.CharacterFormat.FontSize  = 11f;
                text.CharacterFormat.TextColor = Syncfusion.Drawing.Color.White;

                //Set row cells formatting
                row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                row.Cells[i].CellFormat.BackColor         = Syncfusion.Drawing.Color.FromArgb(157, 161, 190);

                row.Cells[i].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            }

            int sno = 1;
            //Writing Sno, Product name and Product Images to the Table.

            WTableRow row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Apple Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
            string     basePath    = _hostingEnvironment.WebRootPath;
            FileStream imageStream = new FileStream(basePath + @"/images/DocIO/Apple Juice.png", FileMode.Open, FileAccess.Read);
            paragraph.AppendPicture(imageStream);

            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Grape Juice");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Syncfusion.Drawing.Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph   = (IWParagraph)row1.Cells[2].AddParagraph();
            imageStream = new FileStream(basePath + @"/images/DocIO/Grape Juice.png", FileMode.Open, FileAccess.Read);
            paragraph.AppendPicture(imageStream);

            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);
            sno++;
            row1 = table.AddRow(false);

            //Writing SNO to the table with formatting text.
            paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText(sno.ToString());
            text.CharacterFormat.Bold     = true;
            text.CharacterFormat.FontSize = 10f;
            row1.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[0].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);
            //Writing Product Name to the table with Formatting.
            paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            text = paragraph.AppendText("Hot Soup");
            text.CharacterFormat.Bold                   = true;
            text.CharacterFormat.FontSize               = 10f;
            text.CharacterFormat.TextColor              = Syncfusion.Drawing.Color.FromArgb(50, 65, 124);
            row1.Cells[1].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[1].CellFormat.BackColor          = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);

            //Writing Product Images to the Table.
            paragraph   = (IWParagraph)row1.Cells[2].AddParagraph();
            imageStream = new FileStream(basePath + @"/images/DocIO/Hot Soup.png", FileMode.Open, FileAccess.Read);
            paragraph.AppendPicture(imageStream);

            paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            row1.Cells[2].CellFormat.VerticalAlignment    = VerticalAlignment.Middle;
            row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
            row1.Cells[2].CellFormat.BackColor            = Syncfusion.Drawing.Color.FromArgb(217, 223, 239);
            sno++;
            (table as WTable).AutoFit(AutoFitType.FixedColumnWidth);
            #endregion Table with Images

            FormatType type        = FormatType.Docx;
            string     filename    = "Sample.docx";
            string     contenttype = "application/vnd.ms-word.document.12";
            #region Document SaveOption
            //Save as .doc format
            if (Group1 == "WordDoc")
            {
                type        = FormatType.Doc;
                filename    = "Sample.doc";
                contenttype = "application/msword";
            }
            //Save as .xml format
            else 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));
        }
        public ActionResult Forms(string Group1)
        {
            if (Group1 == null)
            {
                return(View());
            }
            #region CreateForm
            // Create a new document.
            WordDocument document = new WordDocument();

            // Adding a new section to the document.
            IWSection section = document.AddSection();

            // Adding a new paragraph to the section.
            IWParagraph paragraph = section.AddParagraph();

            #region Document formatting
            //Set background color.
            document.Background.Gradient.Color1         = Color.FromArgb(232, 232, 232);
            document.Background.Gradient.Color2         = Color.FromArgb(255, 255, 255);
            document.Background.Type                    = BackgroundType.Gradient;
            document.Background.Gradient.ShadingStyle   = GradientShadingStyle.Horizontal;
            document.Background.Gradient.ShadingVariant = GradientShadingVariant.ShadingDown;

            section.PageSetup.Margins.All = 30f;
            section.PageSetup.PageSize    = new SizeF(600, 600f);
            #endregion

            #region Title Section
            IWTable table = section.Body.AddTable();
            table.ResetCells(1, 2);

            WTableRow row = table.Rows[0];
            row.Height = 25f;

            IWParagraph          cellPara = row.Cells[0].AddParagraph();
            string               s        = ResolveApplicationDataPath("image.jpg", "Images\\DocIO");
            System.Drawing.Image img      = System.Drawing.Image.FromFile(s);
            IWPicture            pic      = cellPara.AppendPicture(img);
            pic.Height = 80;
            pic.Width  = 180;

            cellPara = row.Cells[1].AddParagraph();
            row.Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            row.Cells[1].CellFormat.BackColor         = Color.FromArgb(173, 215, 255);
            IWTextRange txt = cellPara.AppendText("Job Application Form");
            cellPara.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
            txt.CharacterFormat.Bold     = true;
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 18f;

            row.Cells[0].Width = 200;
            row.Cells[1].Width = 300;
            //row.Cells[1].CellFormat.FitText = true;
            row.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
            #endregion

            section.AddParagraph();

            #region General Information
            table = section.Body.AddTable();
            table.TableFormat.Paddings.All = 5.4f;
            table.ResetCells(2, 1);
            row                = table.Rows[0];
            row.Height         = 20;
            row.Cells[0].Width = 500;
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Thick;
            row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Color.FromArgb(198, 227, 255);
            row.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            txt = cellPara.AppendText(" General Information");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.Bold     = true;
            txt.CharacterFormat.FontSize = 11f;

            row                = table.Rows[1];
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].Width = 500;
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
            row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Color.FromArgb(222, 239, 255);

            txt = cellPara.AppendText("\n Full Name:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            WTextFormField txtField = cellPara.AppendTextFormField("John");
            txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Birth Date:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("BirthDayField", DateTime.Now.ToString("M/d/yyyy"));
            txtField.StringFormat = "M/d/yyyy";
            txtField.Type         = TextFormFieldType.DateText;
            txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;
            txtField.CharacterFormat.TextColor           = Color.MidnightBlue;
            txtField.CharacterFormat.FontName            = "Arial";
            txtField.CharacterFormat.FontSize            = 11f;

            txt = cellPara.AppendText("\n\n Address:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField      = cellPara.AppendTextFormField("221b Baker Street");
            txtField.Type = TextFormFieldType.RegularText;
            txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Phone:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("(206)555-3412");
            txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Email:\t\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("*****@*****.**");
            txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.TextRange.CharacterFormat.FontSize  = 11f;
            cellPara.AppendText("\n");
            #endregion

            section.AddParagraph();

            #region Educational Qualification
            table = section.Body.AddTable();
            table.ResetCells(2, 1);
            table.TableFormat.Paddings.All = 5.4f;
            row                = table.Rows[0];
            row.Height         = 20;
            row.Cells[0].Width = 500;
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Thick;
            row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Color.FromArgb(198, 227, 255);
            row.Cells[0].CellFormat.VerticalAlignment  = VerticalAlignment.Middle;
            txt = cellPara.AppendText(" Educational Qualification");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.Bold     = true;
            txt.CharacterFormat.FontSize = 11f;

            row                = table.Rows[1];
            cellPara           = row.Cells[0].AddParagraph();
            row.Cells[0].Width = 500;
            row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
            row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
            row.Cells[0].CellFormat.BackColor          = Color.FromArgb(222, 239, 255);

            txt = cellPara.AppendText("\n Type:\t\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            WDropDownFormField dropField = cellPara.AppendDropDownFormField();
            dropField.DropDownItems.Add("Higher");
            dropField.DropDownItems.Add("Vocational");
            dropField.DropDownItems.Add("Universal");
            dropField.CharacterFormat.TextColor = Color.MidnightBlue;
            dropField.CharacterFormat.FontName  = "Arial";
            dropField.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n Institution:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txtField = cellPara.AppendTextFormField("Michigan University");
            txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
            txtField.TextRange.CharacterFormat.FontName  = "Arial";
            txtField.CharacterFormat.FontSize            = 11f;

            txt = cellPara.AppendText("\n\n Programming Languages:");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 11f;
            txt = cellPara.AppendText("\n\n\t C#:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 9f;
            dropField = cellPara.AppendDropDownFormField();
            dropField.DropDownItems.Add("Perfect");
            dropField.DropDownItems.Add("Good");
            dropField.DropDownItems.Add("Excellent");
            dropField.CharacterFormat.TextColor = Color.MidnightBlue;
            dropField.CharacterFormat.FontName  = "Arial";
            dropField.CharacterFormat.FontSize  = 11f;

            txt = cellPara.AppendText("\n\n\t VB:\t\t\t\t");
            txt.CharacterFormat.FontName = "Arial";
            txt.CharacterFormat.FontSize = 9f;
            dropField = cellPara.AppendDropDownFormField();
            dropField.DropDownItems.Add("Perfect");
            dropField.DropDownItems.Add("Good");
            dropField.DropDownItems.Add("Excellent");
            dropField.CharacterFormat.TextColor = Color.MidnightBlue;
            dropField.CharacterFormat.FontName  = "Arial";
            dropField.CharacterFormat.FontSize  = 11f;
            #endregion
            //Protect document
            document.ProtectionType = ProtectionType.AllowOnlyFormFields;
            MemoryStream st = new MemoryStream();
            document.Save(st, FormatType.Doc);
            st.Seek(0, SeekOrigin.Begin);
            #endregion CreateForm

            #region FillForm
            // Create a new document.
            WordDocument       document1 = new WordDocument(st, FormatType.Doc);
            IWSection          sec       = document1.LastSection;
            WTextFormField     textFF;
            WDropDownFormField dropFF;

            //Access the text field
            textFF = sec.Body.FormFields[0] as WTextFormField;

            //Fill value for the textfield
            textFF.TextRange.Text = "John";

            //Access the form field with feild name
            textFF = sec.Body.FormFields["BirthDayField"] as WTextFormField;
            textFF.TextRange.Text = "5.13.1980";

            textFF = sec.Body.FormFields[2] as WTextFormField;
            textFF.TextRange.Text = "221b Baker Street";

            textFF = sec.Body.FormFields[3] as WTextFormField;
            textFF.TextRange.Text = "(206)555-3412";

            textFF = sec.Body.FormFields[4] as WTextFormField;
            textFF.TextRange.Text = "*****@*****.**";

            dropFF = sec.Body.FormFields[5] as WDropDownFormField;

            //Set the value
            dropFF.DropDownSelectedIndex = 1;

            textFF = sec.Body.FormFields[6] as WTextFormField;
            textFF.TextRange.Text = "Michigan University";

            dropFF = sec.Body.FormFields[7] as WDropDownFormField;
            dropFF.DropDownSelectedIndex = 1;

            dropFF = sec.Body.FormFields[8] as WDropDownFormField;
            dropFF.DropDownSelectedIndex = 2;

            //Allow only to fill the form.
            document1.ProtectionType = ProtectionType.AllowOnlyFormFields;
            #endregion FillForm

            #region Document save option
            //Save as .doc format
            if (Group1 == "WordDoc")
            {
                return(document.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
            }
            //Save as .docx format
            else 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());
        }
Exemplo n.º 16
0
        public ActionResult FormFillingAndProtection(string Button)
        {
            if (Button == null)
            {
                return(View());
            }
            if (Button == "View Template")
            {
                return(new TemplateResult("ContentControlTemplate.docx", ResolveApplicationDataPath("App_Data\\DocIO"), HttpContext.ApplicationInstance.Response));
            }

            //Creates an Word document instance.
            WordDocument document = new WordDocument();

            //Loads the template document.
            document.Open(ResolveApplicationDataPath("ContentControlTemplate.docx", "App_Data\\DocIO"));

            IWTextRange textRange;
            //Gets table from the template document.
            IWTable   table = document.LastSection.Tables[0];
            WTableRow row   = table.Rows[1];

            #region Fill data and lock the contents of content control
            #region Calendar content control
            IWParagraph cellPara = row.Cells[0].Paragraphs[0];
            //Accesses the date picker content control.
            IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl);
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets today's date to display.
            textRange.Text = DateTime.Now.ToShortDateString();
            textRange.CharacterFormat.FontSize = 14;
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            #endregion

            #region Plain text content controls
            table    = document.LastSection.Tables[1];
            row      = table.Rows[0];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind Analytics";
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind";
            textRange.CharacterFormat.FontSize = 14;

            row      = table.Rows[1];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "10";
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "Nancy Davolio";
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #region CheckBox Content control
            row      = table.Rows[2];
            cellPara = row.Cells[0].LastParagraph;
            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("C#, ");
            textRange.CharacterFormat.FontSize = 14;

            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("VB");
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #region Drop down list content control
            cellPara = row.Cells[1].LastParagraph;
            //Accesses the dropdown list content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default option to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "ASP.NET";
            textRange.CharacterFormat.FontSize = 14;
            inlineControl.ParagraphItems.Add(textRange);

            //Adds items to the dropdown list.
            ContentControlListItem item;
            item             = new ContentControlListItem();
            item.DisplayText = "ASP.NET MVC";
            item.Value       = "2";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "Windows Forms";
            item.Value       = "3";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "WPF";
            item.Value       = "4";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item             = new ContentControlListItem();
            item.DisplayText = "Xamarin";
            item.Value       = "5";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);
            #endregion

            #region Calendar content control
            row      = table.Rows[3];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(-5).ToShortDateString();
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Inserts date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange      = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(10).ToShortDateString();
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #region Block content control
            //Accesses the block content control.
            BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl);
            //Protects the block content control
            blockContentControl.ContentControlProperties.LockContents = true;
            #endregion
            #endregion

            return(document.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment));
        }
Exemplo n.º 17
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Create a new document.
                WordDocument document = new WordDocument();

                // Adding a new section to the document.
                IWSection section = document.AddSection();

                // Adding a new paragraph to the section.
                IWParagraph paragraph = section.AddParagraph();


                #region Document formatting
                //Set background color.
                document.Background.Gradient.Color1         = Color.FromArgb(232, 232, 232);
                document.Background.Gradient.Color2         = Color.FromArgb(255, 255, 255);
                document.Background.Type                    = BackgroundType.Gradient;
                document.Background.Gradient.ShadingStyle   = GradientShadingStyle.Horizontal;
                document.Background.Gradient.ShadingVariant = GradientShadingVariant.ShadingDown;

                section.PageSetup.Margins.All = 72f;
                section.PageSetup.PageSize    = new SizeF(612, 792);

                #endregion

                #region Title Section
                IWTable table = section.Body.AddTable();
                table.ResetCells(1, 2);

                WTableRow row = table.Rows[0];
                row.Height = 25f;

                IWParagraph cellPara = row.Cells[0].AddParagraph();
                Image       img      = Image.FromFile(@"..\..\..\..\..\..\..\Common\images\DocIO\Image.jpg");
                IWPicture   pic      = cellPara.AppendPicture(img);
                pic.Height = 80;
                pic.Width  = 180;

                cellPara = row.Cells[1].AddParagraph();
                row.Cells[1].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row.Cells[1].CellFormat.BackColor         = Color.FromArgb(173, 215, 255);
                IWTextRange txt = cellPara.AppendText("Job Application Form");
                cellPara.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                txt.CharacterFormat.Bold     = true;
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 18f;

                row.Cells[0].Width = 200;
                row.Cells[1].Width = 300;
                row.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
                #endregion

                section.AddParagraph();

                #region General Information
                table = section.Body.AddTable();
                table.TableFormat.Paddings.All = 5.4f;
                table.ResetCells(2, 1);
                row                = table.Rows[0];
                row.Height         = 20;
                row.Cells[0].Width = 500;
                cellPara           = row.Cells[0].AddParagraph();
                row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Thick;
                row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
                row.Cells[0].CellFormat.BackColor          = Color.FromArgb(198, 227, 255);
                row.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                txt = cellPara.AppendText(" General Information");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.Bold     = true;
                txt.CharacterFormat.FontSize = 11f;

                row                = table.Rows[1];
                cellPara           = row.Cells[0].AddParagraph();
                row.Cells[0].Width = 500;
                row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
                row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
                row.Cells[0].CellFormat.BackColor          = Color.FromArgb(222, 239, 255);

                txt = cellPara.AppendText("\n Full Name:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                WTextFormField txtField = cellPara.AppendTextFormField(null);
                txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
                txtField.TextRange.CharacterFormat.FontName  = "Arial";
                txtField.TextRange.CharacterFormat.FontSize  = 11f;

                txt = cellPara.AppendText("\n\n Birth Date:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                txtField = cellPara.AppendTextFormField("BirthDayField", DateTime.Now.ToString("M/d/yyyy"));
                txtField.StringFormat = "M/d/yyyy";
                txtField.Type         = TextFormFieldType.DateText;
                txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
                txtField.TextRange.CharacterFormat.FontName  = "Arial";
                txtField.TextRange.CharacterFormat.FontSize  = 11f;
                txtField.CharacterFormat.TextColor           = Color.MidnightBlue;
                txtField.CharacterFormat.FontName            = "Arial";
                txtField.CharacterFormat.FontSize            = 11f;

                txt = cellPara.AppendText("\n\n Address:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                txtField      = cellPara.AppendTextFormField(null);
                txtField.Type = TextFormFieldType.RegularText;
                txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
                txtField.TextRange.CharacterFormat.FontName  = "Arial";
                txtField.TextRange.CharacterFormat.FontSize  = 11f;

                txt = cellPara.AppendText("\n\n Phone:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                txtField = cellPara.AppendTextFormField(null);
                txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
                txtField.TextRange.CharacterFormat.FontName  = "Arial";
                txtField.TextRange.CharacterFormat.FontSize  = 11f;

                txt = cellPara.AppendText("\n\n Email:\t\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                txtField = cellPara.AppendTextFormField(null);
                txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
                txtField.TextRange.CharacterFormat.FontName  = "Arial";
                txtField.TextRange.CharacterFormat.FontSize  = 11f;
                cellPara.AppendText("\n");
                #endregion

                section.AddParagraph();

                #region Educational Qualification
                table = section.Body.AddTable();
                table.TableFormat.Paddings.All = 5.4f;
                table.ResetCells(2, 1);
                row                = table.Rows[0];
                row.Height         = 20;
                row.Cells[0].Width = 500;
                cellPara           = row.Cells[0].AddParagraph();
                row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Thick;
                row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
                row.Cells[0].CellFormat.BackColor          = Color.FromArgb(198, 227, 255);
                row.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                txt = cellPara.AppendText(" Educational Qualification");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.Bold     = true;
                txt.CharacterFormat.FontSize = 11f;

                row                = table.Rows[1];
                cellPara           = row.Cells[0].AddParagraph();
                row.Cells[0].Width = 500;
                row.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Hairline;
                row.Cells[0].CellFormat.Borders.Color      = Color.FromArgb(155, 205, 255);
                row.Cells[0].CellFormat.BackColor          = Color.FromArgb(222, 239, 255);

                txt = cellPara.AppendText("\n Type:\t\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                WDropDownFormField dropField = cellPara.AppendDropDownFormField();
                dropField.DropDownItems.Add("Higher");
                dropField.DropDownItems.Add("Vocational");
                dropField.DropDownItems.Add("Universal");
                dropField.CharacterFormat.TextColor = Color.MidnightBlue;
                dropField.CharacterFormat.FontName  = "Arial";
                dropField.CharacterFormat.FontSize  = 11f;

                txt = cellPara.AppendText("\n\n Institution:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                txtField = cellPara.AppendTextFormField(null);
                txtField.TextRange.CharacterFormat.TextColor = Color.MidnightBlue;
                txtField.TextRange.CharacterFormat.FontName  = "Arial";
                txtField.CharacterFormat.FontSize            = 11f;

                txt = cellPara.AppendText("\n\n Programming Languages:");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 11f;
                txt = cellPara.AppendText("\n\n\t C#:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 9f;
                dropField = cellPara.AppendDropDownFormField();
                dropField.DropDownItems.Add("Perfect");
                dropField.DropDownItems.Add("Good");
                dropField.DropDownItems.Add("Excellent");
                dropField.CharacterFormat.TextColor = Color.MidnightBlue;
                dropField.CharacterFormat.FontName  = "Arial";
                dropField.CharacterFormat.FontSize  = 11f;

                txt = cellPara.AppendText("\n\n\t VB:\t\t\t\t");
                txt.CharacterFormat.FontName = "Arial";
                txt.CharacterFormat.FontSize = 9f;
                dropField = cellPara.AppendDropDownFormField();
                dropField.DropDownItems.Add("Perfect");
                dropField.DropDownItems.Add("Good");
                dropField.DropDownItems.Add("Excellent");
                dropField.CharacterFormat.TextColor = Color.MidnightBlue;
                dropField.CharacterFormat.FontName  = "Arial";
                dropField.CharacterFormat.FontSize  = 11f;
                #endregion

                btnFill.Enabled = true;

                //Protect document
                document.ProtectionType = ProtectionType.AllowOnlyFormFields;
                document.Save("Sample.doc", FormatType.Doc);
                //Save as doc format
                if (wordDocRadioBtn.Checked)
                {
                    //Saving the document to disk.
                    document.Save("Sample.doc");

                    //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)
                    {
                        //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer]
                        System.Diagnostics.Process.Start("Sample.doc");
                        //Exit
                        this.Close();
                    }
                }
                //Save as docx format
                else 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();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        /// <summary>
        /// Button click event for FormFillingAndProtection.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event args</param>
        private void OnButtonClicked(object sender, EventArgs e)
        {
#if COMMONSB
            string rootPath = "SampleBrowser.Samples.DocIO.Samples.Templates.";
#else
            string rootPath = "SampleBrowser.DocIO.Samples.Templates.";
#endif
            // Load Template document stream.
            Stream inputStream = typeof(FormFillingAndProtection).GetTypeInfo().Assembly.GetManifestResourceStream(rootPath + "ContentControlTemplate.docx");

            // Creates an empty Word document instance.
            WordDocument document = new WordDocument();
            // Opens template document.
            document.Open(inputStream, FormatType.Docx);

            IWTextRange textRange;
            //Gets table from the template document.
            IWTable table = document.LastSection.Tables[0];
            WTableRow row = table.Rows[1];

            #region Inserting content controls

            #region Calendar content control
            IWParagraph cellPara = row.Cells[0].Paragraphs[0];
            //Accesses the date picker content control.
            IInlineContentControl inlineControl = (cellPara.ChildEntities[2] as IInlineContentControl);
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets today's date to display.
            textRange.Text = DateTime.Now.ToString("d");
            textRange.CharacterFormat.FontSize = 14;
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            #endregion

            #region Plain text content controls
            table = document.LastSection.Tables[1];
            row = table.Rows[0];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind Analytics";
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            //Sets text in plain text content control.
            textRange.Text = "Northwind";
            textRange.CharacterFormat.FontSize = 14;

            row = table.Rows[1];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "10";
            textRange.CharacterFormat.FontSize = 14;


            cellPara = row.Cells[1].LastParagraph;
            //Accesses the plain text content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            //Protects the content control.
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets text in plain text content control.
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "Nancy Davolio";
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #region CheckBox Content control
            row = table.Rows[2];
            cellPara = row.Cells[0].LastParagraph;
            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("C#, ");
            textRange.CharacterFormat.FontSize = 14;

            //Inserts checkbox content control.
            inlineControl = cellPara.AppendInlineContentControl(ContentControlType.CheckBox);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets checkbox as checked state.
            inlineControl.ContentControlProperties.IsChecked = true;
            textRange = cellPara.AppendText("VB");
            textRange.CharacterFormat.FontSize = 14;
            #endregion


            #region Drop down list content control
            cellPara = row.Cells[1].LastParagraph;
            //Accesses the dropdown list content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default option to display.
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = "ASP.NET";
            textRange.CharacterFormat.FontSize = 14;
            inlineControl.ParagraphItems.Add(textRange);

            //Adds items to the dropdown list.
            ContentControlListItem item;
            item = new ContentControlListItem();
            item.DisplayText = "ASP.NET MVC";
            item.Value = "2";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item = new ContentControlListItem();
            item.DisplayText = "Windows Forms";
            item.Value = "3";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item = new ContentControlListItem();
            item.DisplayText = "WPF";
            item.Value = "4";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);

            item = new ContentControlListItem();
            item.DisplayText = "Xamarin";
            item.Value = "5";
            inlineControl.ContentControlProperties.ContentControlListItems.Add(item);
            #endregion

            #region Calendar content control
            row = table.Rows[3];
            cellPara = row.Cells[0].LastParagraph;
            //Accesses the date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(-5).ToString("d");
            textRange.CharacterFormat.FontSize = 14;

            cellPara = row.Cells[1].LastParagraph;
            //Inserts date picker content control.
            inlineControl = (cellPara.ChildEntities[1] as IInlineContentControl);
            inlineControl.ContentControlProperties.LockContents = true;
            //Sets default date to display.
            textRange = inlineControl.ParagraphItems[0] as WTextRange;
            textRange.Text = DateTime.Now.AddDays(10).ToString("d");
            textRange.CharacterFormat.FontSize = 14;
            #endregion

            #endregion
            #region Block content control
            //Accesses the block content control.
            BlockContentControl blockContentControl = ((document.ChildEntities[0] as WSection).Body.ChildEntities[2] as BlockContentControl);
            //Protects the block content control
            blockContentControl.ContentControlProperties.LockContents = true;
            #endregion

            MemoryStream stream = new MemoryStream();
            document.Save(stream, FormatType.Docx);
            document.Close();

            if (Device.RuntimePlatform == Device.UWP)
                DependencyService.Get<ISaveWindowsPhone>()
                    .Save("FormFillingAndProtection.docx", "application/msword", stream);
            else
                DependencyService.Get<ISave>().Save("FormFillingAndProtection.docx", "application/msword", stream);
        }
Exemplo n.º 19
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Create a new document.
                WordDocument document = new WordDocument();
                // Adding a new section to the document.
                IWSection section = document.AddSection();
                section.PageSetup.Margins.All        = 50;
                section.PageSetup.DifferentFirstPage = true;
                IWTextRange textRange;
                IWParagraph paragraph = section.AddParagraph();


                #region Table Cell Spacing.
                // --------------------------------------------
                // Table Cell Spacing.
                // --------------------------------------------
                paragraph.AppendText("Table Cell spacing...").CharacterFormat.FontSize = 14;

                section.AddParagraph();
                paragraph = section.AddParagraph();
                WTextBody textBody = section.Body;

                // Adding a new Table to the textbody.
                IWTable table = textBody.AddTable();
                table.TableFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                table.TableFormat.Paddings.All       = 5.4f;
                RowFormat format = new RowFormat();

                format.Paddings.All       = 5;
                format.CellSpacing        = 2;
                format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
                format.IsBreakAcrossPages = true;
                table.ResetCells(25, 5, format, 90);
                IWTextRange text;
                table.Rows[0].IsHeader = true;

                for (int i = 0; i < table.Rows[0].Cells.Count; i++)
                {
                    paragraph = table[0, i].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                    text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                    text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                    text.CharacterFormat.Bold        = true;
                    text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                    table[0, i].CellFormat.BackColor = Color.FromArgb(203, 211, 226);
                }

                for (int i = 1; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        paragraph = table[i, j].AddParagraph() as WParagraph;
                        paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                        text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                        text.CharacterFormat.Bold      = true;
                        if (i % 2 != 1)
                        {
                            table[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                        }
                        else
                        {
                            table[i, j].CellFormat.BackColor = Color.FromArgb(246, 249, 255);
                        }
                    }
                }
                (table as WTable).AutoFit(AutoFitType.FitToContent);
                #endregion Table Cell Spacing.

                #region Nested Table
                // --------------------------------------------
                // Nested Table.
                // --------------------------------------------

                section.AddParagraph();
                paragraph = section.AddParagraph();
                paragraph.ParagraphFormat.PageBreakBefore = true;
                paragraph.AppendText("Nested Table...").CharacterFormat.FontSize = 14;

                section.AddParagraph();
                paragraph = section.AddParagraph();
                textBody  = section.Body;

                // Adding a new Table to the textbody.
                table = textBody.AddTable();

                format = new RowFormat();
                format.Paddings.All       = 5;
                format.CellSpacing        = 2.5f;
                format.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
                table.ResetCells(5, 3, format, 100);


                for (int i = 0; i < table.Rows[0].Cells.Count; i++)
                {
                    paragraph = table[0, i].AddParagraph() as WParagraph;
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                    text = paragraph.AppendText(string.Format("Header {0}", i + 1));
                    text.CharacterFormat.Font        = new Font("Bitstream Vera Serif", 10);
                    text.CharacterFormat.Bold        = true;
                    text.CharacterFormat.TextColor   = Color.FromArgb(0, 21, 84);
                    table[0, i].CellFormat.BackColor = Color.FromArgb(242, 151, 50);
                }
                table[0, 2].Width = 200;
                for (int i = 1; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        paragraph = table[i, j].AddParagraph() as WParagraph;
                        paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        if ((i == 2) && (j == 2))
                        {
                            text = paragraph.AppendText("Nested Table");
                        }

                        else
                        {
                            text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                        }

                        if ((j == 2))
                        {
                            table[i, j].Width = 200;
                        }

                        text.CharacterFormat.TextColor = Color.FromArgb(242, 151, 50);
                        text.CharacterFormat.Bold      = true;
                    }
                }

                // Adding a nested Table.
                IWTable nestTable = table[2, 2].AddTable();

                format = new RowFormat();

                format.Borders.BorderType  = Syncfusion.DocIO.DLS.BorderStyle.DotDash;
                format.HorizontalAlignment = RowAlignment.Center;
                nestTable.ResetCells(3, 3, format, 45);

                for (int i = 0; i < nestTable.Rows.Count; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        paragraph = nestTable[i, j].AddParagraph() as WParagraph;
                        paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;

                        nestTable[i, j].CellFormat.BackColor = Color.FromArgb(231, 235, 245);
                        text = paragraph.AppendText(string.Format("Cell {0} , {1}", i, j + 1));
                        text.CharacterFormat.TextColor = Color.Black;
                        text.CharacterFormat.Bold      = true;
                    }
                }
                (nestTable as WTable).AutoFit(AutoFitType.FitToContent);
                (table as WTable).AutoFit(AutoFitType.FitToWindow);
                #endregion Nested Table

                #region Table with Images
                string dataPath = Application.StartupPath + @"..\..\..\..\..\..\..\..\Common\images\DocIO\";

                //Add a new section to the document.
                section = document.AddSection();
                //Add paragraph to the section.
                paragraph = section.AddParagraph();
                //Writing text.
                textRange = paragraph.AppendText("Table with Images");
                textRange.CharacterFormat.FontSize  = 13f;
                textRange.CharacterFormat.TextColor = Color.DarkBlue;
                textRange.CharacterFormat.Bold      = true;

                //Add paragraph to the section.
                section.AddParagraph();
                paragraph = section.AddParagraph();

                text = null;

                //Adding a new Table to the paragraph.
                table = section.Body.AddTable();
                table.ResetCells(1, 3);

                //Adding rows to the table.
                WTableRow row = table.Rows[0];
                //Set heading row height
                row.Height = 25f;
                //set heading values to the Table.
                for (int i = 0; i < 3; i++)
                {
                    //Add paragraph for writing Text to the cells.
                    paragraph = (IWParagraph)row.Cells[i].AddParagraph();
                    //Set Horizontal Alignment as Center.
                    paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                    //Writing Row Heading
                    switch (i)
                    {
                    case 0:
                        text = paragraph.AppendText("SNO");
                        row.Cells[i].Width = 50f; break;

                    case 1: text = paragraph.AppendText("Drinks"); break;

                    case 2: text = paragraph.AppendText("Showcase Image"); row.Cells[i].Width = 200f; break;
                    }
                    //Set row Heading formatting
                    text.CharacterFormat.Bold      = true;
                    text.CharacterFormat.FontName  = "Cambria";
                    text.CharacterFormat.FontSize  = 11f;
                    text.CharacterFormat.TextColor = Color.White;

                    //Set row cells formatting
                    row.Cells[i].CellFormat.VerticalAlignment = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                    row.Cells[i].CellFormat.BackColor         = Color.FromArgb(157, 161, 190);

                    row.Cells[i].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                }

                int sno = 1;
                //Writing Sno, Product name and Product Images to the Table.

                WTableRow row1 = table.AddRow(false);

                //Writing SNO to the table with formatting text.
                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(sno.ToString());
                text.CharacterFormat.Bold     = true;
                text.CharacterFormat.FontSize = 10f;
                row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
                //Writing Product Name to the table with Formatting.
                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText("Apple Juice");
                text.CharacterFormat.Bold                   = true;
                text.CharacterFormat.FontSize               = 10f;
                text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
                row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

                //Writing Product Images to the Table.
                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                paragraph.AppendPicture(Image.FromFile(dataPath + "Apple Juice.png"));
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
                sno++;
                row1 = table.AddRow(false);

                //Writing SNO to the table with formatting text.
                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(sno.ToString());
                text.CharacterFormat.Bold     = true;
                text.CharacterFormat.FontSize = 10f;
                row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
                //Writing Product Name to the table with Formatting.
                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText("Grape Juice");
                text.CharacterFormat.Bold                   = true;
                text.CharacterFormat.FontSize               = 10f;
                text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
                row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

                //Writing Product Images to the Table.
                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                paragraph.AppendPicture(Image.FromFile(dataPath + "Grape Juice.png"));
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
                sno++;
                row1 = table.AddRow(false);

                //Writing SNO to the table with formatting text.
                paragraph = (IWParagraph)row1.Cells[0].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText(sno.ToString());
                text.CharacterFormat.Bold     = true;
                text.CharacterFormat.FontSize = 10f;
                row1.Cells[0].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[0].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[0].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);
                //Writing Product Name to the table with Formatting.
                paragraph = (IWParagraph)row1.Cells[1].AddParagraph();
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                text = paragraph.AppendText("Hot Soup");
                text.CharacterFormat.Bold                   = true;
                text.CharacterFormat.FontSize               = 10f;
                text.CharacterFormat.TextColor              = Color.FromArgb(50, 65, 124);
                row1.Cells[1].CellFormat.VerticalAlignment  = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[1].CellFormat.Borders.BorderType = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[1].CellFormat.BackColor          = Color.FromArgb(217, 223, 239);

                //Writing Product Images to the Table.
                paragraph = (IWParagraph)row1.Cells[2].AddParagraph();
                paragraph.AppendPicture(Image.FromFile(dataPath + "Hot Soup.png"));
                paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
                row1.Cells[2].CellFormat.VerticalAlignment    = Syncfusion.DocIO.DLS.VerticalAlignment.Middle;
                row1.Cells[2].CellFormat.Borders.BorderType   = Syncfusion.DocIO.DLS.BorderStyle.Single;
                row1.Cells[2].CellFormat.BackColor            = Color.FromArgb(217, 223, 239);
                sno++;
                (table as WTable).AutoFit(AutoFitType.FixedColumnWidth);
                #endregion Table with Images
                //Save as doc format
                if (wordDocRadioBtn.Checked)
                {
                    //Saving the document to disk.
                    document.Save("Sample.doc");

                    //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)
                    {
                        //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.doc")
                        {
                            UseShellExecute = true
                        };
                        process.Start();
#else
                        System.Diagnostics.Process.Start("Sample.doc");
#endif
                        //Exit
                        this.Close();
                    }
                }
                //Save as docx format
                else 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();
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
Exemplo n.º 20
0
        private async void FormattaTabellaWordAsync()
        {
            //Crea una istanza della classe WordDocument (un documento Word vuoto)
            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 = await local.GetFileAsync("Table.docx");

            var streamFile = await storageFile.OpenStreamForReadAsync();

            document.Open(streamFile, FormatType.Docx);

            //Accede all'istanza della prima section del documento Word
            WSection section = document.Sections[0];

            //Accede all'istanza della prima tabella nella section
            WTable table = section.Tables[0] as WTable;

            //Specifica il titolo della tabella
            table.Title = "PriceDetails";

            //Specifica la descrizione della tabella
            table.Description = "This table shows the price details of various fruits";

            //Specifica l'indentazione della tabella
            table.IndentFromLeft = 50;

            //Specifica il colore di background della tabella
            table.TableFormat.BackColor = Color.FromArgb(192, 192, 192);

            //Specifica l'allineamento orizzontale della tabella
            table.TableFormat.HorizontalAlignment = RowAlignment.Left;

            //Specifica il padding left, right, top and bottom di tutte le celle della tabella
            table.TableFormat.Paddings.All = 10;

            //Specifica l'auto resize della tabella per ridimensionare automaticamente tutte le celle sulla base del loro contenuto
            table.TableFormat.IsAutoResized = true;

            //Specifica la dimensione riga del border top, bottom, left and right della tabella
            table.TableFormat.Borders.LineWidth = 2f;

            //Specifica la dimensione riga del border orizzontale
            table.TableFormat.Borders.Horizontal.LineWidth = 2f;

            //Specifica la dimensione riga del border verticale
            table.TableFormat.Borders.Vertical.LineWidth = 2f;

            //Specifica il top, bottom, left and right border color delle tabelle
            table.TableFormat.Borders.Color = Color.Red;

            //Specifica il border color orizzontale della tabella
            table.TableFormat.Borders.Horizontal.Color = Color.Red;

            //Specifica il border color vericale della tabella
            table.TableFormat.Borders.Vertical.Color = Color.Red;

            //Specifica il tipo di border della tabella
            table.TableFormat.Borders.BorderType = BorderStyle.Double;

            //Accede all'istanza della prima riga della tabella
            WTableRow row = table.Rows[0];

            //Specifica l'altezza della riga
            row.Height = 20;

            //Specifica il tipo di altezza della riga
            row.HeightType = TableRowHeightType.AtLeast;

            //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, "TableFormatting.docx");

            DefaultLaunch("TableFormatting.docx");
        }