java中CTSectPr,在Java中使用Apache POI XWPF相同word文档中横向和纵向页面

I am trying to use Java and the Apache POI library to create a word document that contained some landscape and some portrait pages. I can change the orientation of all the pages, but is there a way to change just the orientation of some of them? I have tried using different sections and bodies, but to no avail.

At the moment I have written a function that takes a XWPFDocument as input and changes the orientation of a new body. I had hoped changing the orientation for new body would not affect the previous pages, but it affects the entire document.

private void changeOrientation(XWPFDocument document, String orientation){

CTDocument1 doc = document.getDocument();

CTBody body = doc.addNewBody();

body.addNewSectPr();

CTSectPr section = body.getSectPr();

if(!section.isSetPgSz()) {

section.addNewPgSz();

}

CTPageSz pageSize = section.getPgSz();

if(orientation.equals("landscape")){

pageSize.setOrient(STPageOrientation.LANDSCAPE);

pageSize.setW(BigInteger.valueOf(842 * 20));

pageSize.setH(BigInteger.valueOf(595 * 20));

}

else{

pageSize.setOrient(STPageOrientation.PORTRAIT);

pageSize.setH(BigInteger.valueOf(842 * 20));

pageSize.setW(BigInteger.valueOf(595 * 20));

}

}

Thanks!

解决方案

According to OOXML Specification ECMA-376, Fourth Edition, Part 1 - Fundamentals And Markup Language Reference - 17.6.18 sectPr (Section Properties), in a document with multiple sections, section properties (the sectPr element) are stored as the child element of :

the last paragraph in the section, for all sections except the final

section,

the body element, for the final section.

So, to change page orientation of a section one should create or locate corresponding sectPr and use following code:

private void changeOrientation(CTSectPr section, String orientation) {

CTPageSz pageSize = section.isSetPgSz? section.getPgSz() : section.addNewPgSz();

if (orientation.equals("landscape")) {

pageSize.setOrient(STPageOrientation.LANDSCAPE);

pageSize.setW(BigInteger.valueOf(842 * 20));

pageSize.setH(BigInteger.valueOf(595 * 20));

} else {

pageSize.setOrient(STPageOrientation.PORTRAIT);

pageSize.setH(BigInteger.valueOf(842 * 20));

pageSize.setW(BigInteger.valueOf(595 * 20));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Apache POIWord文档添加水印。下面是添加水印的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class AddWatermarkToWord { public static void main(String[] args) throws IOException { // 读取模板文件 InputStream is = AddWatermarkToWord.class.getResourceAsStream("template.docx"); XWPFDocument doc = new XWPFDocument(is); // 添加水印 for (XWPFParagraph p : doc.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); if (text != null && text.contains("水印")) { r.setText(text.replace("水印", ""), 0); r.getCTR().addNewRPr().addNewNoProof(); r.getCTR().getRPr().addNewColor().setVal("C0C0C0"); r.getCTR().getRPr().addNewSz().setVal(48L); r.getCTR().getRPr().addNewRFonts().setAscii("宋体"); r.getCTR().getRPr().addNewRFonts().setHAnsi("宋体"); r.getCTR().getRPr().addNewRFonts().setCs("宋体"); r.getCTR().getRPr().addNewVertAlign().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun.SUPERSCRIPT); } } } // 保存文档 FileOutputStream out = new FileOutputStream("output.docx"); doc.write(out); out.close(); doc.close(); } } ``` 在上面的代码,我们首先读取一个模板文件 `template.docx`,然后遍历文档的每个段落和运行,找到包含“水印”的文本,然后设置文本的格式,使其成为一个水印。最后将修改后的文档保存到 `output.docx` 文件。 值得注意的是,上面的代码只能在使用 docx 格式的 Word 文档上添加水印,如果要在使用 doc 格式的 Word 文档上添加水印,可以使用 HWPF API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值