• <menu id="w2i4a"></menu>
  • logo Aspose中文文檔

    文檔首頁(yè)>>Aspose中文文檔>>更改或替換頁(yè)眉和頁(yè)腳

    更改或替換頁(yè)眉和頁(yè)腳


    Aspose.Words是一種高級(jí)Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無(wú)需在跨平臺(tái)應(yīng)用程序中直接使用Microsoft Word。

    Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。

    Aspose.Words for .NET 最新下載

    使用 Aspose.Words

    在 Aspose.Words 中,頁(yè)眉或頁(yè)腳通常使用DocumentBuilder進(jìn)行更改。使用其方法MoveToHeaderFooter更新頁(yè)眉或頁(yè)腳。

    以下代碼示例顯示如何替換頁(yè)眉或頁(yè)腳:


    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Create header and specify header title for the first page.
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    builder.Write("Aspose.Words Header");
    // Create footer for pages other than first and specify Footer text.
    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    builder.Write("Aspose.Words Footer");
    // Save the resulting document.
    doc.Save("ChangeHeadersAndFooters.docx");
    

    點(diǎn)擊復(fù)制


    使用 Open XML SDK

    需要使用的命名空間:


    using System.Collections.Generic;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using NUnit.Framework;

    點(diǎn)擊復(fù)制


    以下代碼示例顯示如何替換頁(yè)眉或頁(yè)腳:

    public void ChangeOrReplaceHeaderAndFooterFeature()
    {
    // Replace header in target document with header of source document.
    using (WordprocessingDocument document =
      ArtifactsDir + "Change or replace header and footer - OpenXML.docx",
        WordprocessingDocumentType.Document))
      {
        // Get the main document part
        MainDocumentPart mainDocumentPart = document.MainDocumentPart;
        // Delete the existing header and footer parts
        mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts);
        mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts);
        // Create a new header and footer part
        HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();
        FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();
        // Get Id of the headerPart and footer parts
        string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);
        string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);
        GenerateHeaderPartContent(headerPart);
        GenerateFooterPartContent(footerPart);
        // Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id
        IEnumerable<SectionProperties> sections = mainDocumentPart.Document.Body.Elements<SectionProperties>();
        foreach (var section in sections)
        {
            // Delete existing references to headers and footers
            section.RemoveAllChildren<HeaderReference>();
            section.RemoveAllChildren<FooterReference>();
            // Create the new header and footer reference node
            section.PrependChild<HeaderReference>(new HeaderReference() { Id = headerPartId });
            section.PrependChild<FooterReference>(new FooterReference() { Id = footerPartId });
        }
      }
    }
    private void GenerateHeaderPartContent(HeaderPart part)
    {
      Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
      header1.AddNamespaceDeclaration("wpc",   "https://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
      header1.AddNamespaceDeclaration("mc", "https://schemas.openxmlformats.org/markup-compatibility/2006");
      header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
      header1.AddNamespaceDeclaration("r", "https://schemas.openxmlformats.org/officeDocument/2006/relationships");
      header1.AddNamespaceDeclaration("m", "https://schemas.openxmlformats.org/officeDocument/2006/math");
      header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
      header1.AddNamespaceDeclaration("wp14", "https://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
      header1.AddNamespaceDeclaration("wp", "https://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
      header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
      header1.AddNamespaceDeclaration("w", "https://schemas.openxmlformats.org/wordprocessingml/2006/main");
      header1.AddNamespaceDeclaration("w14", "https://schemas.microsoft.com/office/word/2010/wordml");
      header1.AddNamespaceDeclaration("wpg", "https://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
      header1.AddNamespaceDeclaration("wpi", "https://schemas.microsoft.com/office/word/2010/wordprocessingInk");
      header1.AddNamespaceDeclaration("wne", "https://schemas.microsoft.com/office/word/2006/wordml");
      header1.AddNamespaceDeclaration("wps", "https://schemas.microsoft.com/office/word/2010/wordprocessingShape");
      Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
      ParagraphProperties paragraphProperties1 = new ParagraphProperties();
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
      paragraphProperties1.Append(paragraphStyleId1);
      Run run1 = new Run();
      Text text1 = new Text();
      text1.Text = "Header";
      run1.Append(text1);
      paragraph1.Append(paragraphProperties1);
      paragraph1.Append(run1);
      header1.Append(paragraph1);
      part.Header = header1;
    }
    public static void GenerateFooterPartContent(FooterPart part)
    {
      Footer footer1 = new Footer() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
      footer1.AddNamespaceDeclaration("wpc", "https://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
      footer1.AddNamespaceDeclaration("mc", "https://schemas.openxmlformats.org/markup-compatibility/2006");
      footer1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
      footer1.AddNamespaceDeclaration("r", "https://schemas.openxmlformats.org/officeDocument/2006/relationships");
      footer1.AddNamespaceDeclaration("m", "https://schemas.openxmlformats.org/officeDocument/2006/math");
      footer1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
      footer1.AddNamespaceDeclaration("wp14", "https://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
      footer1.AddNamespaceDeclaration("wp", "https://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
      footer1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
      footer1.AddNamespaceDeclaration("w", "https://schemas.openxmlformats.org/wordprocessingml/2006/main");
      footer1.AddNamespaceDeclaration("w14", "https://schemas.microsoft.com/office/word/2010/wordml");
      footer1.AddNamespaceDeclaration("wpg", "https://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
      footer1.AddNamespaceDeclaration("wpi", "https://schemas.microsoft.com/office/word/2010/wordprocessingInk");
      footer1.AddNamespaceDeclaration("wne", "https://schemas.microsoft.com/office/word/2006/wordml");
      footer1.AddNamespaceDeclaration("wps", "https://schemas.microsoft.com/office/word/2010/wordprocessingShape");
      Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };
      ParagraphProperties paragraphProperties1 = new ParagraphProperties();
      ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" };
      paragraphProperties1.Append(paragraphStyleId1);
      Run run1 = new Run();
      Text text1 = new Text();
      text1.Text = "Footer";
      run1.Append(text1);
      paragraph1.Append(paragraphProperties1);
      paragraph1.Append(run1);
      footer1.Append(paragraph1);
      part.Footer = footer1;
    }
    
    

    點(diǎn)擊復(fù)制



    您可以從Aspose.Words GitHub 下載此示例的示例文件。
    掃碼咨詢


    添加微信 立即咨詢

    電話咨詢

    客服熱線
    023-68661681

    TOP
    三级成人熟女影院,欧美午夜成人精品视频,亚洲国产成人乱色在线观看,色中色成人论坛 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();