在C#中实现Word页眉页脚的全部功能

页眉页脚经常使用于文章排版,在Word工具栏里。我们能够加入页眉,页脚,页码,日期和时间。图片等信息和内容。页眉/页脚有两个额外选项:首页不同,奇偶页不同。有时在不同的节(section)里插入不同的页眉页脚。

从零開始在C#实现这些功能。工作量巨大。

所以,今天向大家推荐一款免费的API库,Free Spire.Doc能够从CSDN官网,和 Nuget直接下载。功能强大,easy上手。

这篇文章分为三个部分:
1. 怎样在C#里为Word加入页码。
2. 怎样在C#里实现Word页眉页脚的图文混排。
3. 怎样在C#里实现Word页眉页脚的奇偶页不同和首页不同。

友情提示:Free Spire.Doc 能够独立创建和载入Word文档,这里的微软Word仅用于查看效果。

第一部分:在C#里为Word加入页码

假设Word文档包括很多页,我们能够在页眉页脚处加入页码。

该页码可显示总页数,当前页数。加入Free Spire.Doc Bin 目录里的.dll至Visual Studio作为引用。我使用了下面代码在C#中了实现对Word页码的加入。

            //Create a Word document and add a section
            Document doc = new Document();
            Section section = doc.AddSection();

            //Initial a HeaderFooter class 
            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
            Paragraph headerParagraph = header.AddParagraph();

            //Use the AppendField method to get the FieldPage and FieldNumpages
            headerParagraph.AppendField("page number", FieldType.FieldPage);
            headerParagraph.AppendText(" of ");
            headerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
            headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

            //Save and launch the document
            doc.SaveToFile("Test.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Test.docx");

在页脚处加入页码的方法与上述代码类似,这里我就不再赘述了。有须要的朋友能够參考上面的部分。因为我在文档中没有加入额外的页数,所以截图部分显示的是 1 of 1.

这里写图片描写叙述

第二部分:在C#里实现Word页眉页脚的图文混排

与文本相比。图片更easy吸引人注意。我们经常同一时候使用文本和图片(即图文混排)来引人注目。在一些正式报告,法律文件里的页眉页脚会使用到图文混排,以达到上述目的。这里我使用了维基百科的标志和关于它的一段介绍来展示在C#里实现页脚部分的图文混排。同一时候,大家别忘了加入系统引用”System. Drawing”。

            //Create a Word document and initial the footer class
            Document document = new Document();
            Section section = document.AddSection();
            HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;

            //Add text and image to the footer
            Paragraph paragraph = footer.AddParagraph();
            DocPicture footerImage = paragraph.AppendPicture(Image.FromFile("Wiki.bmp"));
            TextRange TR = paragraph.AppendText("Supported and Hosted by the non-profit Wikimedia Foundation.");

            //Format the text and image
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TR.CharacterFormat.FontName = "Calibri";
            TR.CharacterFormat.FontSize = 13;
            TR.CharacterFormat.TextColor = Color.Black;
            TR.CharacterFormat.Bold = true;

            // Save the document and launch to see the output
            document.SaveToFile("Test.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Test.docx");

这里写图片描写叙述

值得一提的是,能够使用 ” TextWrappingStyle” 和 ”TextWrappingType”来编辑图片在文本中的位置和自己主动换行:

            footerImage.TextWrappingStyle = TextWrappingStyle.Through;
            footerImage.TextWrappingType = TextWrappingType.Left;

我尝试过使用free Spire.Doc在页眉页脚中加入表格,居然可行。有这需求的朋友,也能够測试一下。

第三部分:在C#里实现Word页眉页脚的奇偶页不同和首页不同

Word文件有默认设置每一页的页眉页脚都同样。然而在报告、书籍等排版中往往须要不同的页眉页脚来美化排版。日常编程中,假设我们仅仅须要首页页眉页脚不同。我们能够把首页单独成节。Spire.Doc 提供了更加简便高速的方法来设置页眉页脚首页不同,奇偶页不同。这里,我先以在C#中实现页眉奇偶页不同为例。代码例如以下:

            //Create a document and section
            Document document = new Document();
            Section section = document.AddSection();

            //Set the bool true and add the Odd Header and Even Header
            section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;
            Paragraph oddHeader = section.HeadersFooters.OddHeader.AddParagraph();
            TextRange oddHT = oddHeader.AppendText("Coding is the art of life");
            Paragraph evenHeader = section.HeadersFooters.EvenHeader.AddParagraph();
            TextRange evenHT = evenHeader.AppendText("Time is the most valuable thing");

            //Format the headers
            oddHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;
            oddHT.CharacterFormat.FontName = "Calibri";
            oddHT.CharacterFormat.FontSize = 20;
            oddHT.CharacterFormat.TextColor = Color.Green;
            oddHT.CharacterFormat.Bold = true;
            evenHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;
            evenHT.CharacterFormat.FontName = "Calibri";
            evenHT.CharacterFormat.FontSize = 20;
            evenHT.CharacterFormat.TextColor = Color.Green;
            evenHT.CharacterFormat.Bold = true;

            //Launch to see effects
            document.SaveToFile("R.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("R.docx");

这里写图片描写叙述

使用该工具,还能够在奇数页,偶数页中设置不同的页码格式。加入不同的图片和表格,并设置不同的格式。实现代码可參照第一部分和第二部分。至于首页不同。代码与奇偶页不同相差无几,此处我仅列举两者代码中不同的部分:

            //set the first page header and footer 
            section.PageSetup.DifferentFirstPageHeaderFooter = true;
            Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
            Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
            //set the rest page header and footer 
            Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
            Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();

假设你仅仅须要首页的页眉页脚,不设置其它页的页眉页脚就能够了。

这样就仅仅会有首页的页眉页脚。

结论:
在我看来, free Spire.Doc完美满足了我在C#中为Word加入页眉页脚的需求,是一款值得花时间測试使用的工具,对项目效率有极大的提升。
感谢阅读,欢迎留下宝贵意见。

posted on 2017-06-10 16:02  yjbjingcha  阅读(5642)  评论(0编辑  收藏  举报

导航