2017-01-19 67 views

回答

2

您的圖片顯示來自Openoffice或Libreoffice Writer的頁面樣式對話框,而不是來自Word的apache poi主要用於。但儘管如此:

所謂的「頁腳 - 間距」的作家是在Word的頁面下邊距和頁腳邊距之間的差異。但請注意,底部有一個不可打印的頁面範圍,取決於打印機。這個差距也必須考慮。

例:

import java.io.*; 

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

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar; 

import java.math.BigInteger; 

public class CreateWordHeaderFooterSpacing { 

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

    XWPFDocument document = new XWPFDocument(); 

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

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

    XWPFParagraph paragraph = footer.getParagraphArray(0); 
    paragraph.setAlignment(ParagraphAlignment.CENTER); 

    XWPFRun run = paragraph.createRun(); 
    run.setText("Footer"); 

    CTSectPr sectPr = document.getDocument().getBody().getSectPr(); 
    if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr(); 
    CTPageMar pageMar = sectPr.getPgMar(); 
    if (pageMar == null) pageMar = sectPr.addNewPgMar(); 
    pageMar.setLeft(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5" 
    pageMar.setRight(BigInteger.valueOf(720)); 
    pageMar.setTop(BigInteger.valueOf(1440)); //1440 Twips = 1440/20 = 72 pt = 72/72 = 1" 
    pageMar.setFooter(BigInteger.valueOf(720)); //0.5" footer margin 
    long notPrintableBottomPageRange = (long)(0.038888*72*20); //0.038888" gap for non printable bottom page range 
    pageMar.setBottom(BigInteger.valueOf(1152+720+notPrintableBottomPageRange)); //1152 Twips = 1152/20/72 = 0.8" 
    //bottom margin = 0.8" footer spacing + 0.5" footer margin + 0.038888" gap for non printable bottom page range 

    document.write(new FileOutputStream("CreateWordHeaderFooterSpacing.docx")); 

    document.close(); 

} 
} 

這導致我的作家苛求0.8" 頁腳間距

相關問題