Word .NET庫(kù)組件Spire.Doc系列教程(33): 在Word中添加和刪除頁(yè)眉頁(yè)腳
Spire.Doc for .NET是一個(gè)專(zhuān)業(yè)的Word .NET庫(kù),設(shè)計(jì)用于幫助開(kāi)發(fā)人員高效地開(kāi)發(fā)創(chuàng)建、閱讀、編寫(xiě)、轉(zhuǎn)換和打印任何來(lái)自.NET( C#, VB.NET, ASP.NET)平臺(tái)的Word文檔文件的功能。
本系列教程將為大家?guī)?lái)Spire.Doc for .NET在使用過(guò)程中的各類(lèi)實(shí)際操作,在Word工具欄里,我們通常會(huì)設(shè)置頁(yè)眉,頁(yè)腳,頁(yè)碼來(lái)對(duì)word文檔進(jìn)行排版。本文將詳細(xì)介紹如何使用C#為word文檔添加頁(yè)眉,頁(yè)腳和頁(yè)碼。>>下載Spire.Doc最新試用版體驗(yàn)
為 Word 文檔添加頁(yè)眉,頁(yè)腳和頁(yè)碼
如果Word文檔包含許多頁(yè),我們可以在頁(yè)眉頁(yè)腳處添加頁(yè)碼。該頁(yè)碼可顯示當(dāng)前頁(yè)數(shù), 總頁(yè)數(shù)。我們以在頁(yè)腳處添加頁(yè)碼為例:
Document document = new Document(); Section sec = document.AddSection(); Paragraph para = sec.AddParagraph(); para.AppendText("Page 1"); para.AppendBreak(BreakType.PageBreak); para.AppendText("Page 2"); HeaderFooter footer = sec.HeadersFooters.Footer; Paragraph footerPara = footer.AddParagraph(); footerPara.AppendField("頁(yè)碼", FieldType.FieldPage); footerPara.AppendText(" of "); footerPara.AppendField("總頁(yè)數(shù)", FieldType.FieldNumPages); footerPara.Format.HorizontalAlignment = HorizontalAlignment.Right; document.SaveToFile("添加頁(yè)碼.docx", FileFormat.Docx);
圖片和文字都能被添加為頁(yè)眉或頁(yè)腳,我們用C#來(lái)為word文檔添加圖文混排的頁(yè)眉為例。
Document document = new Document(); Section sec = document.AddSection(); Paragraph para = sec.AddParagraph(); para.AppendText("Page 1"); HeaderFooter header = sec.HeadersFooters.Header; Paragraph headerPara = header.AddParagraph(); //Add text and image to the header DocPicture headerImage = headerPara.AppendPicture(Image.FromFile("logo.jpg")); TextRange TR = headerPara.AppendText("成都冰藍(lán)科技"); document.SaveToFile("圖文頁(yè)眉.docx", FileFormat.Docx);
同時(shí),我們可以通過(guò) TextWrappingStyle 和 TextWrappingType來(lái)設(shè)置圖片在文本中的位置和自動(dòng)換行:
headerImage.TextWrappingStyle = TextWrappingStyle.Through; headerImage.TextWrappingType = TextWrappingType.Left;
在C#里實(shí)現(xiàn)Word頁(yè)眉頁(yè)腳的奇偶頁(yè)不同和金喜正規(guī)買(mǎi)球不同。主要代碼設(shè)置如下所示,具體的頁(yè)眉和頁(yè)腳步驟參照上述。
sec.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true; sec.PageSetup.DifferentFirstPageHeaderFooter = true;
刪除word文檔中的頁(yè)眉頁(yè)腳
徹底刪除word文檔中的頁(yè)眉頁(yè)腳,運(yùn)行后,word文檔中所有頁(yè)面的頁(yè)眉頁(yè)腳全部被清除。
//創(chuàng)建一個(gè)Document實(shí)例并加載示例文檔 Document doc = new Document(); doc.LoadFromFile("Sample.docx"); //獲取第一個(gè)section Section section = doc.Sections[0]; //刪除頁(yè)眉 section.HeadersFooters.Header.ChildObjects.Clear(); //刪除頁(yè)腳 section.HeadersFooters.Footer.ChildObjects.Clear(); //保存文檔 doc.SaveToFile("ClearHeaderFooter.docx", FileFormat.Docx);
僅刪除word文檔第一頁(yè)的頁(yè)眉頁(yè)腳,其余頁(yè)面保留頁(yè)眉頁(yè)腳。
//創(chuàng)建一個(gè)Document實(shí)例并加載示例文檔 Document doc = new Document(); doc.LoadFromFile("Sample.docx"); //獲取第一個(gè)section Section section = doc.Sections[0]; //設(shè)置頁(yè)眉頁(yè)腳金喜正規(guī)買(mǎi)球不同 section.PageSetup.DifferentFirstPageHeaderFooter = true; //刪除金喜正規(guī)買(mǎi)球頁(yè)眉頁(yè)腳 section.HeadersFooters.FirstPageHeader.ChildObjects.Clear(); //保存文檔 doc.SaveToFile("Result.docx", FileFormat.Docx);
推薦閱讀:【想要快速完成文檔格式轉(zhuǎn)換嗎?Spire系列組件格式轉(zhuǎn)換完整攻略來(lái)啦!】
*購(gòu)買(mǎi)Spire.Doc正版授權(quán)的朋友可以點(diǎn)擊哦~~