Java 在Word中添加由ApachePOI在maven项目中生成的BORDURE页面

Java 在Word中添加由ApachePOI在maven项目中生成的BORDURE页面,java,apache,ms-word,apache-poi,frame,Java,Apache,Ms Word,Apache Poi,Frame,我已经创建了一个JavaMaven应用程序,其中我使用ApachePOI生成Word文档作为凭证。除了我没有找到任何关于在word ApachePOI中为页面添加bordure的文档外,所有这些都可以正常工作 我使用以下代码: private void diplomas () throws InvalidFormatException, FileNotFoundException, IOException { XWPFDocument document = new XWPFDocument

我已经创建了一个JavaMaven应用程序,其中我使用ApachePOI生成Word文档作为凭证。除了我没有找到任何关于在word ApachePOI中为页面添加bordure的文档外,所有这些都可以正常工作

我使用以下代码:

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模式的文档,ooxml模式是
ApachePOI
的底层基本对象,是公开的。因此,我们需要下载的源代码,然后从这些源代码中执行
javadoc
以获得API文档。在那里我们可以找到
CTSectPr
CTPageBorders
,它们是页面边框线

注意
ooxml模式
version
1.4
apachepoi4.0.0
一起使用

创建设置了页面边框线的
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();

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.xwpf.usermodel.*;
导入org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
公共类CreateWordPageBorder{
公共静态void main(字符串[]args)引发异常{
XWPFDocument document=新的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();
//纸张大小的信
ctPageSz.setW(java.math.biginger.valueOf(math.round(8.5*1440));//8.5英寸
ctPageSz.setH(java.math.biginger.valueOf(math.round(11*1440));//11英寸
//页面边框
CTPageBorders CTPageBorders=(ctSectPr.isSetPgBorders())?ctSectPr.getPgBorders():ctSectPr.addNewPgBorders();
ctPageBorders.setOffsetFrom(STPageBorderOffset.PAGE);
对于(int b=0;b<4;b++){
CTBorder CTBorder=(ctPageBorders.isSetTop())?ctPageBorders.getTop():ctPageBorders.addNewTop();
如果(b==1)ctBorder=(ctPageBorders.isSetBottom())?ctPageBorders.getBottom():ctPageBorders.addNewBottom();
如果(b==2)ctBorder=(ctPageBorders.isSetLeft())?ctPageBorders.getLeft():ctPageBorders.addNewLeft();
如果(b==3)ctBorder=(ctPageBorders.isSetRight())?ctPageBorders.getRight():ctPageBorders.addNewRight();
ctBorder.setVal(STBorder.THREE_D_浮雕);
setSz(java.math.biginger.valueOf(24));
setSpace(java.math.biginger.valueOf(24));
ctBorder.setColor(“FF0000”);
}
XWPFParagraph paragraph paragraph=document.createParagraph();
XWPFRun=段落.createRun();
run.setText("知识本身是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德如果你不轻率,就必须为自己的行为负责。”;
FileOutputStream out=新的FileOutputStream(“CreateWordPageBorder.docx”);
文件。写(出);
out.close();
document.close();
}
}

对于我来说,真的不清楚“为页面添加框架(在毕业生中可以找到框架)”是什么意思”“你能澄清一下吗?也许在Word中添加一个屏幕截图。我同意这是不清楚的。。。你的意思是你想在这一页周围加上边框吗?你能告诉我们,作为一个用户,你会使用哪些命令来实现你在Word应用程序中想要的吗?是的,我有一个学校管理应用程序,一个按钮生成毕业生选择的学生私人无效文凭()抛出无效格式异常,FileNotFoundException,IOException{infoFormation();infoedition();XWPFDocument document=new XWPFDocument();String横向=“横向”;changeOrientation(document,横向);//创建页眉页脚XWPFHeaderFooterPolicy headerFooterPolicy=document.getHeaderFooterPolicy();if(headerFooterPolicy==null)headerFooterPolicy=document.createHeaderFooterPolicy();/…}