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

插入表格

        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");
            }
        }
时间: 2024-08-21 08:36:29

DocX开源WORD操作组件的学习系列四的相关文章

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

创建目录 效果图 static void AddToc() { Console.WriteLine("\tAddToc()"); using (var document = DocX.Create(@"docs\Toc2.docx")) { document.InsertTableOfContents("1 目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfCo

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

替换文本 private static void ReplaceText() { Console.WriteLine("ReplaceText()"); File.Copy(@"docs\Lists.docx", @"docs\ReplaceText.docx", true); using (var document = DocX.Load(@"docs\ReplaceText.docx")) { //全局替换 documen

开源word操作组件DocX的记录

开源word操作组件DocX的记录 使用开源word操作组件DocX的记录 1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作word非常轻便,有利于减轻开发负担,提升程序效率.DocX在Codeplex和Github上都有开源. 1.2 获取与安装 可以在http://docx.codeplex.com/releases下载获取,也可以直接利用Nu

开源RabbitMQ操作组件

开源RabbitMQ操作组件 对于目前大多的.NET项目,其实使用的技术栈都是差不多,估计现在很少用控件开发项目的了,毕竟一大堆问题.对.NET的项目,目前比较适合的架构ASP.NET MVC,ASP.NET WebAPI,ORM(较多Dapper.NET或者其扩展,稍大一些的项目用EF等等),为了提高速度也会采用缓存(.NET自带的Memcache,或者Redis),请求较多的项目,使用Nginx做负载均衡和使用队列等等. 上面简单的介绍一下.NET的项目的技术架构,具体的技术根据具体的需求做

Identity Server4学习系列四之用户名密码获得访问令牌

1.简介 Identity Server4支持用户名密码模式,允许调用客户端使用用户名密码来获得访问Api资源(遵循Auth 2.0协议)的Access Token,MS可能考虑兼容老的系统,实现了这个功能,但是不建议这么做. 2.实战一服务端配置 接着Identity Server4学习系列三的基础上,直接扩展里面的项目代码,让服务端同时支持密钥认证和用户名密码认证 第一步:扩展ThirdClients类,如下: /// <summary> /// 配置可以访问IdentityServer4

.Net开源Excel、Word操作组件-NPOI、EPPlus、DocX[转]

link: http://www.cnblogs.com/jacktang/p/4493760.html 一.NPOI 简介:NPOI is the .NET version of POI Java project. With NPOI, you can read/write Office 2003/2007 files very easily. 官网地址:https://github.com/tonyqus/npoihttp://npoi.codeplex.com/ 二.EPPlus 简介:E

.Net开源Excel、Word操作组件-NPOI、EPPlus、DocX

一.NPOI 简介: NPOI is the .NET version of POI Java project. With NPOI, you can read/write Office 2003/2007 files very easily. 官网地址: https://github.com/tonyqus/npoi http://npoi.codeplex.com/ 二.EPPlus 简介: EPPlus is a .net library that reads and writes Exc

老牌开源Office操作组件NPOI现已支持.NET Core

昨天在微信群里听到老牌Excel开发利器NPOI的作者瞿总说4.6.1版本的NPOI已经支持.NET Standard 2.0了,这也就意味着你可以在.NET Core中使用NPOI了. 作者:依乐祝 原文地址 :https://www.cnblogs.com/yilezhu/p/10269281.html 写在前面 曾经的.NET Framework时代就很喜欢使用这个组件来对Excel的进行操作,可是随着.NET Core时代的到来以及NPOI不支持.NET Core所以就找到了园子里的大神

简单易用的.NET免费开源RabbitMQ操作组件EasyNetQ解析

对于目前大多的.NET项目,其实使用的技术栈都是差不多,估计现在很少用控件开发项目的了,毕竟一大堆问题.对.NET的项目,目前比较适合的架构ASP.NET MVC,ASP.NET WebAPI,ORM(较多Dapper.NET或者其扩展,稍大一些的项目用EF等等),为了提高速度也会采用缓存(.NET自带的Memcache,或者Redis),请求较多的项目,使用Nginx做负载均衡和使用队列等等. 上面简单的介绍一下.NET的项目的技术架构,具体的技术根据具体的需求做出选择.介绍到队列,很多人都会