Java 添加Word页眉、页脚

Tina_Tang

在Word文档中,页眉位于文档中每个页面的顶部区域,而页脚则位于文档中每个页面的底部区域。它们都常用于显示文档的附加信息,例如时间、图形、公司微标或作者姓名等。本文将通过使用Java程序来演示如何在Word文档中添加页眉和页脚。主要包括:添加文本、图片和页码至页眉、页脚;设置奇偶页页眉页脚不同;设置首页页眉页脚不同;锁定页眉页脚。

使用工具:Free Spire.Doc for Java(免费版)

Jar文件获取及导入:

方法1:通过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Doc.jar文件导入Java程序。(如下图)
image001.jpg

方法2:通过maven仓库安装导入。具体安装详情参见此网页

【示例1】添加页眉、页脚(文本、图片、页码)

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*;

public class AddHeaderAndFooter {
    public static void main(String[] args) {

        //加载需要添加页眉页脚的文档
        Document doc= new Document("D:\\Desktop\\Sample.docx");
        Section sec = doc.getSections().get(0);

        //调用方法添加页眉页脚
        AddHeaderFooter(sec);

        //保存文档
        doc.saveToFile("output/AddHeaderFooter.docx");    }

        //自定义方法来添加图片、文字页眉及页码
        private static void AddHeaderFooter(Section sec){

        //加载图片添加到页眉,并设置图片在段落中的对齐方式
        HeaderFooter header = sec.getHeadersFooters().getHeader();
        Paragraph hpara= header.addParagraph();
        DocPicture pic =hpara.appendPicture("D:\\Desktop\\Image.png");
        pic.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
        pic.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        pic.setVerticalAlignment(ShapeVerticalAlignment.Center);

        //添加文字到页眉,并设置字体、字号、字体加粗、对齐方式
        TextRange txt = hpara.appendText("中青在线");
        txt.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
        txt.getCharacterFormat().setTextColor(Color.GRAY);
        txt.getCharacterFormat().setFontName("仿宋");
        txt.getCharacterFormat().setFontSize(12f);
        txt.getCharacterFormat().setBold(true);
        hpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        //设置图片的文本环绕方式、页眉底部边线(粗细、间距)
        pic.setTextWrappingStyle(TextWrappingStyle.Behind);
        hpara.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
        hpara.getFormat().getBorders().getBottom().setLineWidth(0.5f);
        hpara.getFormat().getBorders().setSpace(2f);

        //添加页码到页脚,并设置页脚对齐方式,顶部边线粗细、间距
        HeaderFooter footer = sec.getHeadersFooters().getFooter();
        Paragraph fpara= footer.addParagraph();
        fpara.appendField("页码",FieldType.Field_Page);
        fpara.appendText("/");
        fpara.appendField("总页数",FieldType.Field_Num_Pages);
        fpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
        fpara.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        fpara.getFormat().getBorders().getTop().setLineWidth(1f);
        fpara.getFormat().getBorders().getTop().setSpace(2f);

    }
}

页眉、页脚添加效果:

image002.jpg

【示例2】设置奇偶页页眉页脚不同

import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;

public class HeaderFooterForOddAndEvenPages {
    public static void main(String[] args) {
        
        //加载测试文档
        Document doc = new Document("D:\\Desktop\\Sample.docx");
        Section sec = doc.getSections().get(0);
        
        //设置奇偶页页眉页脚不同
        sec.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
        
        //设置奇数页页眉页脚
        HeaderFooter oddheader = sec.getHeadersFooters().getOddHeader();
        Paragraph para1 = oddheader.addParagraph();
        TextRange textRange1 = para1.appendText("奇数页页眉");
        para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
        textRange1.getCharacterFormat().setTextColor(Color.ORANGE);
        textRange1.getCharacterFormat().setBold(true);
        HeaderFooter oddfooter = sec.getHeadersFooters().getFooter();
        Paragraph para2 = oddfooter.addParagraph();
        TextRange textRange2 = para2.appendText("奇数页页脚");
        para2.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
        textRange2.getCharacterFormat().setTextColor(Color.ORANGE);
        textRange2.getCharacterFormat().setBold(true);
        
        //设置偶数页页眉页脚
        HeaderFooter evenheader = sec.getHeadersFooters().getEvenHeader();
        Paragraph para3 = evenheader.addParagraph();
        TextRange textRange3 = para3.appendText("偶数页页眉");
        para3.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
        textRange3.getCharacterFormat().setTextColor(Color.BLUE);
        textRange3.getCharacterFormat().setBold(true);
        HeaderFooter evenfooter = sec.getHeadersFooters().getEvenFooter();
        Paragraph para4 = evenfooter.addParagraph();
        TextRange textRange4 = para4.appendText("偶数页页脚");
        para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
        textRange4.getCharacterFormat().setTextColor(Color.BLUE);
        textRange4.getCharacterFormat().setBold(true);
        
        //保存文档
        doc.saveToFile("output/result.docx",FileFormat.Docx_2010);
    }
}

奇偶页页眉页脚设置效果:

image003.jpg

【示例3】设置首页页眉页脚不同

import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;

public class HeaderFooterDifferentFromFirstPage {
    public static void main(String[] args) {

        //加载测试文的
        Document doc = new Document("D:\\Desktop\\Sample.docx");
        Section sec = doc.getSections().get(0);
        
        //设置首页页眉页脚不同
        sec.getPageSetup().setDifferentFirstPageHeaderFooter(true);
        
        //添加首页页眉页脚
        HeaderFooter firstpageheader = sec.getHeadersFooters().getFirstPageHeader();
        Paragraph para1 = firstpageheader.addParagraph();
        TextRange textRange1 = para1.appendText("首页页眉");
        para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        textRange1.getCharacterFormat().setBold(true);
        textRange1.getCharacterFormat().setTextColor(Color.GREEN);
        HeaderFooter firstpagefooter = sec.getHeadersFooters().getFirstPageFooter();
        Paragraph para2 = firstpagefooter.addParagraph();
        TextRange textRange2 = para2.appendText("首页页脚");
        para2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        textRange2.getCharacterFormat().setBold(true);
        textRange2.getCharacterFormat().setTextColor(Color.GREEN);
        
        //添加页眉页脚到其他页面
        Paragraph para3 = sec.getHeadersFooters().getHeader().addParagraph();
        para3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        TextRange textRange3 = para3.appendText("非首页页眉");
        textRange3.getCharacterFormat().setBold(true);
        Paragraph para4 = sec.getHeadersFooters().getFooter().addParagraph();
        para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        TextRange textRange4 = para4.appendText("非首页页脚");
        textRange4.getCharacterFormat().setBold(true);
        
        //保存文档
        doc.saveToFile("output/result2.docx",FileFormat.Docx_2010);

    }
}

页眉页脚设置效果:

image004.jpg

【示例4】锁定页眉页脚

import com.spire.doc.*;
public class LockHeaderFooter {
    public static void main(String[] args) {

        //加载测试文档
        Document doc = new Document("D:\\Desktop\\result2.docx");

        //获取第一节
        Section sec = doc.getSections().get(0);

        // 设置保护类型及密码
        doc.protect(ProtectionType.Allow_Only_Form_Fields,"123");
        sec.setProtectForm(false);

        //保存文档
        doc.saveToFile("output/LockHeaderFooter.docx",FileFormat.Docx_2010);
    }
}

锁定页眉页脚后,文档除页眉页脚外其他内容依旧可进行编辑。

(本文完)

阅读 1.8k
284 声望
6 粉丝
0 条评论
284 声望
6 粉丝
文章目录
宣传栏