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

DocX学习系列

DocX开源WORD操作组件的学习系列一 :  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.html

DocX开源WORD操作组件的学习系列二 :  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_005_docx2.html

DocX开源WORD操作组件的学习系列三:  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx3.html

DocX开源WORD操作组件的学习系列四:  http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_csharp_006_docx4.html

1 创建目录

1.1创建目录效果图

目录结构图

1.2创建目录代码:

   static void AddToc()
        {
            Console.WriteLine("\tAddToc()");

            using (var document = DocX.Create(@"docs\Toc2.docx"))
            {
              
                document.InsertTableOfContents("1 目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading1");
               // document.InsertSectionPageBreak();//分页
                var h1 = document.InsertParagraph("2 测试1");
                h1.StyleName = "Heading1";
                document.InsertParagraph("Some very interesting content here");
                var h2 = document.InsertParagraph("3 测试2");
                h2.StyleName = "Heading1";
                document.InsertParagraph("Some very interesting content here as well");
                var h3 = document.InsertParagraph("3.1 测试3.1");
                h3.StyleName = "Heading2";
                var h4 = document.InsertParagraph("3.1.1 测试3.1.1");
                h4.StyleName = "Heading3";
                var h5 = document.InsertParagraph("4 测试4");
                h5.StyleName = "Heading1";
                document.InsertParagraph("Not so very interesting....");
                document.Save();
            }
        }

 2.添加书签

2.1添加书签效果图

2.2添加书签代码:

    private static void Bookmarks()
        {
            Console.WriteLine("\tBookmarks()");

            using (var document = DocX.Create(@"docs\Bookmarks.docx"))
            {
                //添加一个书签
                var paragraph = document.InsertBookmark("firstBookmark");
                //添加一个段落并填充文本
                var paragraph2 = document.InsertParagraph("This is a paragraph which contains a ");
                //给段落2尾部添加一个书签,名字为secondBookmark
                paragraph2.AppendBookmark("secondBookmark");
                //给段落2原有文本的基础上追加文本bookmark
                paragraph2.Append("bookmark");
                //将段落2的secondBookmark的书签位置插入文本handy
                paragraph2.InsertAtBookmark("handy ", "secondBookmark");

                paragraph2.ReplaceText("which","WHICH");
                //遍历书签
                for (var index = 0; index < document.Bookmarks.Count; index++)
                {
                    var item = document.Bookmarks[index];
                    Console.WriteLine(item.Name, item.Paragraph.Text);
                }
                //修改书签TEXT
                for (var index = 0; index < document.Bookmarks.Count; index++)
                {
                    var item = document.Bookmarks[ index];
                    item.SetText("书签" + index);
                    Console.WriteLine(item.Name, item.Paragraph.Text);
                }
                document.Save();
                Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n");

            }
        }

 3.插入各种分隔符

        static void HelloWorldInsertHorizontalLine()
        {
            Console.WriteLine("\tHelloWorldInsertHorizontalLine()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docs\HelloWorldInsertHorizontalLine.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();
                p.InsertHorizontalLine("double", 6, 1, "auto");
                Paragraph p1 = document.InsertParagraph();
                p1.InsertHorizontalLine("double", 6, 1, "red");
                Paragraph p2 = document.InsertParagraph();
                p2.InsertHorizontalLine("single", 6, 1, "red");
                Paragraph p3 = document.InsertParagraph();
                p3.InsertHorizontalLine("triple", 6, 1, "blue");
                Paragraph p4 = document.InsertParagraph();
                p4.InsertHorizontalLine("double", 3, 10, "red");
                // Save this document to disk.
                document.Save();
                Console.WriteLine("\tCreated: docs\\HelloWorldInsertHorizontalLine.docx\n");
            }
        }

4.插入超链接

 private static void Hyperlinks()
        {
            // Create a document.
            using (DocX document = DocX.Create(@"docs\Hyperlinks.docx"))
            {
              
                // Add a hyperlink into the document.
                Hyperlink link1 = document.AddHyperlink("百度一下", new Uri("http://www.baidu.com"));
                Hyperlink link2 = document.AddHyperlink("TOC文档",new Uri("file:///" + @"E:\soft\DocX-master\Examples\bin\Debug\docs\Toc2.docx".Replace("\\", "/")));
                Uri uri = new Uri("http://www.baidu.com");
                Paragraph p1 = document.InsertParagraph();

                // Append content to the Paragraph
                p1.AppendLine("下面是2个超链接").Append(":").Bold();
                p1.AppendLine("百度的: ").AppendHyperlink(link1).Color(WindowsColor.FromArgb(5, 99, 193)).Append(".");
                p1.AppendLine("本地的绝对路径 :").AppendHyperlink(link2).Color(WindowsColor.FromArgb(5, 99, 193)).Append(".");
                p1.AppendLine();
                document.Save();
                Console.WriteLine("\tCreated: docs\\Hyperlinks.docx\n");
            }
        }

5.插入分页符合换行符

 private static void BreakPageLine()
        {
            Console.WriteLine("\tBreakPageLine()");
            // Create a new document.
            using (DocX document = DocX.Create(@"docs\BreakPageLine.docx"))
            {
                // Add Headers and Footers to this document.
             
                Paragraph p0 = document.InsertParagraph();
                p0.Append("Hello First line").Bold();
                p0.InsertPageBreakAfterSelf();
                var p1 = document.InsertParagraph("zhaojiedi");
                document.InsertSectionPageBreak();
                document.InsertParagraph("zhaojiedi2");
                document.InsertSection();
               var p2= document.InsertParagraph("zhaojiedi3");
                p2.AppendLine("zhaojiedi4");
                p2.AppendLine("zhaojiedi5");
                p2.AppendLine("zhaojiedi6");
                document.Save();
            }// Release this document from memory.
        }

 6.插入公式

  private static void Equations()
        {
            Console.WriteLine("\tEquations()");
            // Create a new document.
            using (DocX document = DocX.Create(@"docs\Equations.docx"))
            {
                // Insert first Equation in this document.
                Paragraph pEquation1 = document.InsertEquation("x = y+z");
                // Insert second Equation in this document and add formatting.
                Paragraph pEquation2 = document.InsertEquation("x = (y+z)/t").FontSize(18).Color(WindowsColor.Blue);
                // Save this document to disk.
                document.Save();
                Console.WriteLine("\tCreated: docs\\Equations.docx\n");
            }
        }

7.插入页眉页脚

        private static void HeadersAndFooters2()
        {
            Console.WriteLine("\tHeadersAndFooters()");

            // Create a new document.
            using (DocX document = DocX.Create(@"docs\HeadersAndFooters.docx"))
            {
                // Add Headers and Footers to this document.
                document.AddHeaders();
                document.AddFooters();

                // Force the first page to have a different Header and Footer.
                document.DifferentFirstPage = true;

                // Force odd & even pages to have different Headers and Footers.
                document.DifferentOddAndEvenPages = true;

                // Get the first, odd and even Headers for this document.
                Header header_first = document.Headers.first;
                Header header_odd = document.Headers.odd;
                Header header_even = document.Headers.even;

                // Get the first, odd and even Footer for this document.
                Footer footer_first = document.Footers.first;
                Footer footer_odd = document.Footers.odd;
                Footer footer_even = document.Footers.even;

                // Insert a Paragraph into the first Header.
                Paragraph p0 = header_first.InsertParagraph();
                p0.Append("Hello First Header.").Bold();

                // Insert a Paragraph into the odd Header.
                Paragraph p1 = header_odd.InsertParagraph();
                p1.Append("Hello Odd Header.").Bold();

                // Insert a Paragraph into the even Header.
                Paragraph p2 = header_even.InsertParagraph();
                p2.Append("Hello Even Header.").Bold();

                // Insert a Paragraph into the first Footer.
                Paragraph p3 = footer_first.InsertParagraph();
                p3.Append("Hello First Footer.").Bold();

                // Insert a Paragraph into the odd Footer.
                Paragraph p4 = footer_odd.InsertParagraph();
                p4.Append("Hello Odd Footer.").Bold();

                // Insert a Paragraph into the even Header.
                Paragraph p5 = footer_even.InsertParagraph();
                p5.Append("Hello Even Footer.").Bold();

                // Insert a Paragraph into the document.
                Paragraph p6 = document.InsertParagraph();
                p6.AppendLine("Hello First page.");

                // Create a second page to show that the first page has its own header and footer.
                p6.InsertPageBreakAfterSelf();

                // Insert a Paragraph after the page break.
                Paragraph p7 = document.InsertParagraph();
                p7.AppendLine("Hello Second page.");

                // Create a third page to show that even and odd pages have different headers and footers.
                p7.InsertPageBreakAfterSelf();

                // Insert a Paragraph after the page break.
                Paragraph p8 = document.InsertParagraph();
                p8.AppendLine("Hello Third page.");

                //Insert a next page break, which is a section break combined with a page break
                document.InsertSectionPageBreak();

                //Insert a paragraph after the "Next" page break
                Paragraph p9 = document.InsertParagraph();
                p9.Append("Next page section break.");

                //Insert a continuous section break
                document.InsertSection();

                //Create a paragraph in the new section
                var p10 = document.InsertParagraph();
                p10.Append("Continuous section paragraph.");

                // Save all changes to this document.
                document.Save();

                Console.WriteLine("\tCreated: docs\\HeadersAndFooters.docx\n");
            }// Release this document from memory.
        }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java 有许多完全开源免费的工具可以将 Word 文档转换为 PDF。这里列举几个常用的: - Apache POI:一个开源的 Java 库,可以用来读写 Microsoft Office 格式的文档(包括 Word)。POI 可以将 Word 文档转换为 PDF,但是需要使用另一个库(例如 iText)来生成 PDF。 - iText:一个开源的 Java 库,可以用来生成和操作 PDF 文档。iText 可以将 Word 文档转换为 PDF,但是需要使用另一个库(例如 Apache POI)来读取 Word 文档。 - JODConverter:一个开源的 Java 库,可以用来在不安装 Microsoft Office 的情况下将 Word 文档转换为 PDF。JODConverter 使用了 OpenOffice 服务器来进行文档转换,因此需要安装 OpenOffice。 - Aspose.Words:一个商业的 Java 库,可以用来读写操作 Word 文档。Aspose.Words 可以将 Word 文档转换为 PDF。 ### 回答2: 目前有几款Java开源免费的Word转PDF组件可以选择使用。 1. Apache POI:Apache POI是一个流行的Java API,用于操作Microsoft Office格式文件,包括Word。它提供了Word转PDF的功能,可以方便地将Word文档转换为PDF格式。 2. Docx4j:Docx4j是一个用于创建和操作Office Open XML文档格式(包括Word)的Java库。它可以将Word文档直接转换为PDF格式,并且支持一些自定义选项和样式。 3. JODConverter:JODConverter是一个Java库,用于将各种文档格式相互转换,包括Word转PDF。它使用OpenOffice或LibreOffice作为转换器,可以将Word文档转换为PDF等格式。 4. FreeSpire.Doc for Java:FreeSpire.Doc for Java是一个免费的Java库,用于创建和操作Microsoft Word文档。它可以将Word文档转换为PDF格式,并且支持一些自定义设置和格式。 这些开源免费的Word转PDF组件都具有一定的功能和灵活性,可以根据需要选择适合自己的组件进行使用。 ### 回答3: 目前有以下几种完全开源免费的Java Word转PDF组件可以选择: 1. Apache POI:Apache POI是一个用于处理Microsoft Office格式文件的Java库,其中包括Word格式文件的读写功能,通过使用POI库中的API,可以将Word文件转换为PDF格式。 2. iText:iText是一个流行的开源Java PDF库,它可以用来创建、操作和处理PDF文件。iText也提供了将Word文件转换为PDF的功能,具有较高的可靠性和灵活性。 3. Docx4j:Docx4j是一个用于处理Microsoft Office Open XML(OOXML)格式文件的Java库,它支持Word格式文件的读写和转换。通过使用Docx4j库中的API,可以将Word文件转换为PDF格式。 4. JODConverter:JODConverter是一个开源的Java库,它使用LibreOffice软件来实现文档转换功能。通过使用JODConverter,可以将Word文件转换为PDF格式。 需要注意的是,以上列举的组件都是完全开源免费的,可以根据自己的需求选择适合的组件进行Word转PDF操作。同时,这些组件在使用过程中可能会有一些限制或问题,需要根据具体情况进行适当的调整和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值