java - 在Word中添加BORDURE页面,由maven项目中的apache poi生成

标签 java apache ms-word apache-poi frame

我创建了一个 java maven 应用程序,在其中使用 apache poi 生成 Word 文档作为文凭。一切正常,除了我没有找到任何有关在 word apache poi 中为页面添加边框的文档。

enter image description here

我使用这个代码:

private void diplomas () throws InvalidFormatException, 
FileNotFoundException, IOException
{
XWPFDocument document = new XWPFDocument ();
String landscape = "landscape";
changeOrientation (document, landscape);

// create header-footer
XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy ();
if (headerFooterPolicy == null) headerFooterPolicy = 
document.createHeaderFooterPolicy ();
// ....
// create header start
XWPFHeader header = headerFooterPolicy.createHeader 
(XWPFHeaderFooterPolicy.DEFAULT);
//....}

最佳答案

根据您之前的问题,您已经知道正文的部分属性。您知道页面大小和方向已设置。还可以设置页面边框和页面边框线。

不幸的是,没有任何关于 ooxml 模式(apache poi 的低级基本对象)的公开文档。所以我们需要下载ooxml-schemas的源码然后通过 javadoc 形成这些内容来获取 API 文档。在那里我们找到 CTSectPrCTPageBorders,它们是页面边框线。

注意 ooxml-schemas 版本 1.4apache poi 4.0.0 一起使用。

创 build 置了页面边框线的 Word 页面的最简单示例:

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

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

  XWPFDocument document= new XWPFDocument();

  CTDocument1 ctDocument = document.getDocument();
  CTBody ctBody = ctDocument.getBody();
  CTSectPr ctSectPr = (ctBody.isSetSectPr())?ctBody.getSectPr():ctBody.addNewSectPr();
  CTPageSz ctPageSz = (ctSectPr.isSetPgSz())?ctSectPr.getPgSz():ctSectPr.addNewPgSz();
  //paper size letter
  ctPageSz.setW(java.math.BigInteger.valueOf(Math.round(8.5 * 1440))); //8.5 inches
  ctPageSz.setH(java.math.BigInteger.valueOf(Math.round(11 * 1440))); //11 inches
  //page borders
  CTPageBorders ctPageBorders = (ctSectPr.isSetPgBorders())?ctSectPr.getPgBorders():ctSectPr.addNewPgBorders();
  ctPageBorders.setOffsetFrom(STPageBorderOffset.PAGE);
  for (int b = 0; b < 4; b++) {
   CTBorder ctBorder = (ctPageBorders.isSetTop())?ctPageBorders.getTop():ctPageBorders.addNewTop();
   if (b == 1) ctBorder = (ctPageBorders.isSetBottom())?ctPageBorders.getBottom():ctPageBorders.addNewBottom();
   else if (b == 2) ctBorder = (ctPageBorders.isSetLeft())?ctPageBorders.getLeft():ctPageBorders.addNewLeft();
   else if (b == 3) ctBorder = (ctPageBorders.isSetRight())?ctPageBorders.getRight():ctPageBorders.addNewRight();
   ctBorder.setVal(STBorder.THREE_D_EMBOSS);
   ctBorder.setSz(java.math.BigInteger.valueOf(24));
   ctBorder.setSpace(java.math.BigInteger.valueOf(24));
   ctBorder.setColor("FF0000");
  }

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");

  FileOutputStream out = new FileOutputStream("CreateWordPageBorder.docx");  
  document.write(out);
  out.close();
  document.close();

 }
}

关于java - 在Word中添加BORDURE页面,由maven项目中的apache poi生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53628945/

相关文章:

java - 如何将 apache solr 与 eclipse 和 Apache tomcat 集成

apache - 为什么 Gzip 压缩我的 css & javascript 而不是我的 HTML/php?

java - 需要从 PHP 翻译成 Java

c# - 在 C# Windows 窗体中预览 .doc、.docx

c# - 如何使用 word interop 一次将一个项目添加到 word 文档的新行

java - 快速迭代具有 5100 万个素数的数据结构

java - 如何将此 SQL 查询写入 jOOQ?

c - 寻找开源http服务器

vba - 使用word宏只改变三列表

java - 获取一个国家的货币代码