1. 程式人生 > >在C#中實現Word頁首頁尾的所有功能

在C#中實現Word頁首頁尾的所有功能

頁首頁尾常用於文章排版,在Word工具欄裡,我們可以新增頁首,頁尾,頁碼,日期和時間,圖片等資訊和內容。頁首/頁尾有兩個額外選項:首頁不同,奇偶頁不同。有時在不同的節(section)裡插入不同的頁首頁尾。從零開始在C#實現這些功能,工作量巨大。所以,今天向大家推薦一款免費的API庫,Free Spire.Doc可以從CSDN官網,和 Nuget直接下載。功能強大,容易上手。

這篇文章分為三個部分:
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頁首頁尾的圖文混排

與文字相比,圖片更容易吸引人注意。我們經常同時使用文字和圖片(即圖文混排)來引人注目。在一些正式報告,法律檔案中的頁首頁尾會使用到圖文混排,以達到上述目的。這裡我使用了維基百科的標誌和關於它的一段介紹來展示在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新增頁首頁尾的需求,是一款值得花時間測試使用的工具,對專案效率有極大的提升。
感謝閱讀,歡迎留下寶貴意見。