码迷,mamicode.com
首页 > 其他好文 > 详细

DocX开源WORD操作组件的学习系列四

时间:2017-06-09 16:25:58      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:time   page   tom   files   1.0   ict   rdf   static   track   

插入表格

        static void LargeTable()
        {
            Console.WriteLine("\tLargeTable()");
            var _directoryWithFiles = "docs\\";
            using (var output = File.Open(_directoryWithFiles + "LargeTable.docx", FileMode.Create))
            {
                using (var doc = DocX.Create(output))
                {
                    var tbl = doc.InsertTable(1, 18);

                    var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight;
                    var colWidth = wholeWidth / tbl.ColumnCount;
                    var colWidths = new int[tbl.ColumnCount];
                    tbl.AutoFit = AutoFit.Contents;
                    var r = tbl.Rows[0];
                    var cx = 0;
                    foreach (var cell in r.Cells)
                    {
                        cell.Paragraphs.First().Append("Col " + cx);
                        //cell.Width = colWidth;
                        cell.MarginBottom = 0;
                        cell.MarginLeft = 0;
                        cell.MarginRight = 0;
                        cell.MarginTop = 0;

                        cx++;
                    }
                    tbl.SetBorder(TableBorderType.Bottom, BlankBorder);
                    tbl.SetBorder(TableBorderType.Left, BlankBorder);
                    tbl.SetBorder(TableBorderType.Right, BlankBorder);
                    tbl.SetBorder(TableBorderType.Top, BlankBorder);
                    tbl.SetBorder(TableBorderType.InsideV, BlankBorder);
                    tbl.SetBorder(TableBorderType.InsideH, BlankBorder);

                    doc.Save();
                }
            }
            Console.WriteLine("\tCreated: docs\\LargeTable.docx\n");
        }


        static void TableWithSpecifiedWidths()
        {
            Console.WriteLine("\tTableSpecifiedWidths()");
            var _directoryWithFiles = "docs\\";
            using (var output = File.Open(_directoryWithFiles + "TableSpecifiedWidths.docx", FileMode.Create))
            {
                using (var doc = DocX.Create(output))
                {
                    var widths = new float[] { 200f, 100f, 300f };
                    var tbl = doc.InsertTable(1, widths.Length);
                    tbl.SetWidths(widths);
                    var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight;
                    tbl.AutoFit = AutoFit.Contents;
                    var r = tbl.Rows[0];
                    var cx = 0;
                    foreach (var cell in r.Cells)
                    {
                        cell.Paragraphs.First().Append("Col " + cx);
                        //cell.Width = colWidth;
                        cell.MarginBottom = 0;
                        cell.MarginLeft = 0;
                        cell.MarginRight = 0;
                        cell.MarginTop = 0;

                        cx++;
                    }
                    //add new rows 
                    for (var x = 0; x < 5; x++)
                    {
                        r = tbl.InsertRow();
                        cx = 0;
                        foreach (var cell in r.Cells)
                        {
                            cell.Paragraphs.First().Append("Col " + cx);
                            //cell.Width = colWidth;
                            cell.MarginBottom = 0;
                            cell.MarginLeft = 0;
                            cell.MarginRight = 0;
                            cell.MarginTop = 0;

                            cx++;
                        }
                    }
                    tbl.SetBorder(TableBorderType.Bottom, BlankBorder);
                    tbl.SetBorder(TableBorderType.Left, BlankBorder);
                    tbl.SetBorder(TableBorderType.Right, BlankBorder);
                    tbl.SetBorder(TableBorderType.Top, BlankBorder);
                    tbl.SetBorder(TableBorderType.InsideV, BlankBorder);
                    tbl.SetBorder(TableBorderType.InsideH, BlankBorder);

                    doc.Save();
                }
            }
            Console.WriteLine("\tCreated: docs\\TableSpecifiedWidths.docx\n");

        }

 文档加密

   static void ProtectedDocument()
        {
            Console.WriteLine("\tHelloWorldPasswordProtected()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docs\HelloWorldPasswordProtected.docx"))
            {
                // Insert a Paragraph into this document.
                Paragraph p = document.InsertParagraph();

                // Append some text and add formatting.
                p.Append("Hello World!^011Hello World!")
                .Font(new Font("Times New Roman"))
                .FontSize(32)
                .Color(WindowsColor.Blue)
                .Bold();


                // Save this document to disk with different options
                // Protected with password for Read Only
                EditRestrictions erReadOnly = EditRestrictions.readOnly;
                document.AddProtection(erReadOnly, "oracle");
                document.SaveAs(@"docs\\HelloWorldPasswordProtectedReadOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedReadOnly.docx\n");

                // Protected with password for Comments
                EditRestrictions erComments = EditRestrictions.comments;
                document.AddProtection(erComments, "oracle");
                document.SaveAs(@"docs\\HelloWorldPasswordProtectedCommentsOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedCommentsOnly.docx\n");

                // Protected with password for Forms
                EditRestrictions erForms = EditRestrictions.forms;
                document.AddProtection(erForms, "oracle");
                document.SaveAs(@"docs\\HelloWorldPasswordProtectedFormsOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedFormsOnly.docx\n");

                // Protected with password for Tracked Changes
                EditRestrictions erTrackedChanges = EditRestrictions.trackedChanges;
                document.AddProtection(erTrackedChanges, "oracle");
                document.SaveAs(@"docs\\HelloWorldPasswordProtectedTrackedChangesOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedTrackedChangesOnly.docx\n");

                // But it‘s also possible to add restrictions without protecting it with password.

                // Protected with password for Read Only
                document.AddProtection(erReadOnly);
                document.SaveAs(@"docs\\HelloWorldWithoutPasswordReadOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordReadOnly.docx\n");

                // Protected with password for Comments
                document.AddProtection(erComments);
                document.SaveAs(@"docs\\HelloWorldWithoutPasswordCommentsOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordCommentsOnly.docx\n");

                // Protected with password for Forms
                document.AddProtection(erForms);
                document.SaveAs(@"docs\\HelloWorldWithoutPasswordFormsOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordFormsOnly.docx\n");

                // Protected with password for Tracked Changes
                document.AddProtection(erTrackedChanges);
                document.SaveAs(@"docs\\HelloWorldWithoutPasswordTrackedChangesOnly.docx");
                Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordTrackedChangesOnly.docx\n");
            }
        }

 缩进

  private static void Indentation()
        {
            Console.WriteLine("\tIndentation()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docs\Indentation.docx"))
            {
                // Create a new Paragraph.
                Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
                // Indent only the first line of the Paragraph
                p.IndentationFirstLine = 1.0f;
                // Save all changes made to this document.
                document.Save();
                Console.WriteLine("\tCreated: docs\\Indentation.docx\n");
            }
        }

 

DocX开源WORD操作组件的学习系列四

标签:time   page   tom   files   1.0   ict   rdf   static   track   

原文地址:http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx4.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!