2017-08-21 123 views
1

從另一個(已回答)問題中,我學到了如何將頁計數器添加到Word文檔中。此外,我需要在字段(即頁面計數器)上設置字體家族樣式(顏色,粗體,斜體,下劃線...)。這怎麼能實現?Apache POI:如何爲字段設置字體樣式(PAGE,PAGENUM,PAGEREF ...)

CTSimpleField ctSimpleField = paragraph.getCTP().addNewFldSimple(); 

CTSimpleField不提供直接設置這些屬性的方法。

原題:How to add page numbers in format X of Y while creating a word document using apache poi api?

import java.io.*; 

import org.apache.poi.xwpf.usermodel.*; 

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; 

public class CreateWordHeaderFooter { 

public static void main(String[] args) throws Exception { 

    XWPFDocument doc= new XWPFDocument(); 

    // the body content 
    XWPFParagraph paragraph = doc.createParagraph(); 
    XWPFRun run=paragraph.createRun(); 
    run.setText("The Body:"); 

    paragraph = doc.createParagraph(); 
    run=paragraph.createRun(); 
    run.setText("Lorem ipsum.... page 1"); 

    paragraph = doc.createParagraph(); 
    run=paragraph.createRun(); 
    run.addBreak(BreakType.PAGE); 
    run.setText("Lorem ipsum.... page 2"); 

    paragraph = doc.createParagraph(); 
    run=paragraph.createRun(); 
    run.addBreak(BreakType.PAGE); 
    run.setText("Lorem ipsum.... page 3"); 

    // create header-footer 
    XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy(); 
    if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy(); 

    // create header start 
    XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); 

    paragraph = header.getParagraphArray(0); 
    if (paragraph == null) paragraph = header.createParagraph(); 
    paragraph.setAlignment(ParagraphAlignment.LEFT); 

    run = paragraph.createRun(); 
    run.setText("The Header:"); 

    // create footer start 
    XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT); 

    paragraph = footer.getParagraphArray(0); 
    if (paragraph == null) paragraph = footer.createParagraph(); 
    paragraph.setAlignment(ParagraphAlignment.CENTER); 

    run = paragraph.createRun(); 
    run.setText("Page "); 
    // this adds the page counter 
    paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT"); 
    run = paragraph.createRun(); 
    run.setText(" of "); 
    // this adds the page total number 
    paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT"); 

    doc.write(new FileOutputStream("CreateWordHeaderFooter.docx")); 

} 
} 
+0

格式僅格式 「頁」 「Y的頁x」 和 「的」,但不能x或y。
XWPFRun run = paragraph.createRun(); run.setText(「Page」); run.setBold(true); run.setFontFamily(「Arial」); –

回答

2

爲了能夠格式化,我們需要在Word運行。但要使用運行創建字段,我們需要爲每個字段運行三次。一次運行標記FldChar開始,然後一次運行標記字段InstrText和第三次運行標記FldChar結束。每次運行都可以,但也必須根據需要進行格式化。

實施例:

import java.io.*; 

import org.apache.poi.xwpf.usermodel.*; 

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; 

public class CreateWordHeaderFooter { 

public static void main(String[] args) throws Exception { 

    XWPFDocument doc= new XWPFDocument(); 

    // the body content 
    XWPFParagraph paragraph = doc.createParagraph(); 
    XWPFRun run=paragraph.createRun(); 
    run.setText("The Body:"); 

    paragraph = doc.createParagraph(); 
    run=paragraph.createRun(); 
    run.setText("Lorem ipsum.... page 1"); 

    paragraph = doc.createParagraph(); 
    run=paragraph.createRun(); 
    run.addBreak(BreakType.PAGE); 
    run.setText("Lorem ipsum.... page 2"); 

    paragraph = doc.createParagraph(); 
    run=paragraph.createRun(); 
    run.addBreak(BreakType.PAGE); 
    run.setText("Lorem ipsum.... page 3"); 

    // create header-footer 
    XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy(); 
    if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy(); 

    // create header start 
    XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); 

    paragraph = header.getParagraphArray(0); 
    if (paragraph == null) paragraph = header.createParagraph(); 
    paragraph.setAlignment(ParagraphAlignment.LEFT); 

    run = paragraph.createRun(); 
    run.setText("The Header:"); 

    // create footer start 
    XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT); 

    paragraph = footer.getParagraphArray(0); 
    if (paragraph == null) paragraph = footer.createParagraph(); 
    paragraph.setAlignment(ParagraphAlignment.CENTER); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.setText("Page "); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.BEGIN); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.getCTR().addNewInstrText().setStringValue("PAGE \\* MERGEFORMAT"); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.END); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.setText(" of "); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.BEGIN); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.getCTR().addNewInstrText().setStringValue("NUMPAGES \\* MERGEFORMAT"); 

    run = paragraph.createRun(); 
    run.setBold(true); 
    run.getCTR().addNewFldChar().setFldCharType(org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType.END); 

    doc.write(new FileOutputStream("CreateWordHeaderFooter.docx")); 

} 
} 
相關問題