该文章将详细介绍如何使用Spire.Doc for Java为word文档设置页边距和添加页面边框。
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class PageSetup {
public static void main(String[] args) throws Exception{
String inputFile="Sample.docx";
String outputFile="out/result.docx";
//加载示例文档
Document document= new Document(inputFile);
Section section = document.getSections().get(0);
//设置页边距
section.getPageSetup().getMargins().setTop(30f);
section.getPageSetup().getMargins().setBottom(30f);
section.getPageSetup().getMargins().setLeft(60f);
section.getPageSetup().getMargins().setRight(80f);
//设置页面边框样式,颜色和位置
section.getPageSetup().getBorders().setBorderType(BorderStyle.Double_Wave);
section.getPageSetup().getBorders().setColor(java.awt.Color.green);
section.getPageSetup().getBorders().getTop().setSpace(20);
section.getPageSetup().getBorders().getBottom().setSpace(30);
section.getPageSetup().getBorders().getLeft().setSpace(30);
section.getPageSetup().getBorders().getRight().setSpace(25);
//保存文档
document.saveToFile(outputFile, FileFormat.Docx);
}
}
设置页边距和边框后的如果如图所示: