java poi改变word某一页横向

2021-12-17  本文已影响0人  板栗炖牛肉

前言

解决方案


    public static final String SUBSECTIONONE = "v";

    public static final String SUBSECTIONTWO = "landscape";

/**
     * 分段
     *
     * @param xwpfDocument
     * @param result SUBSECTIONONE / SUBSECTIONTWO 
     */
    public void subsection(XWPFDocument xwpfDocument, String result) {
        CTDocument1 doc = xwpfDocument.getDocument();
        CTBody body = doc.getBody();
        CTP ctp = body.addNewP();
        CTPPr ctpPr = ctp.addNewPPr();
        CTSectPr ctSectPr = ctpPr.addNewSectPr();
        changeOrientation(ctSectPr, result);
    }


    /**
     * @param section
     * @param orientation 字符串方向
     */
    private void changeOrientation(CTSectPr section, String orientation) {
        CTPageSz pageSize = section.isSetPgSz() ? section.getPgSz() : section.addNewPgSz();
        if (orientation.equals(SUBSECTIONTWO)) {
            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));
        }
    }
...文档内容
wordUtil.subsection(xwpfDocument, WordUtil.SUBSECTIONONE);
...文档内容(此页为横向)
wordUtil.subsection(xwpfDocument, WordUtil.SUBSECTIONTWO);
上一篇 下一篇

猜你喜欢

热点阅读