您现在的位置是:首页 > 编程 > 

JAVA - 使用Apache POI生成word(二) 设置纸张大小、调整纸张方向

2025-07-15 17:31:43
JAVA - 使用Apache POI生成word(二) 设置纸张大小、调整纸张方向 前言 之前开发时,需要将纸张方向由纵向改为横向,查询资料得出只需要设置一下纸张的长度与宽度便可实现相同的效果。 1. pom引入依赖 <dependen

JAVA - 使用Apache POI生成word(二) 设置纸张大小、调整纸张方向

前言
之前开发时,需要将纸张方向由纵向改为横向,查询资料得出只需要设置一下纸张的长度与宽度便可实现相同的效果。

1. pom引入依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.4</version>
        </dependency>

2. 相关代码

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
    /**
     * 设置纸张大小
     *
     * @param document doc对象
     * @param width    宽
     * @param height   长
     */
    public static void setPageSize(XWPFDocument document, long width, long height) {
        CTSectPr sectPr = document.getDocument().getBody().addewSectPr();
        CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addewPgSz();
        pgsz.setW(BigInteger.valueOf(width));
        pgsz.setH(BigInteger.valueOf(height));
    }

    /**
     * 保存文件
     *
     * @param document doc对象
     * @param savePath 保存路径
     * @param fileame 文件名称
     */
    public static void saveDoc(XWPFDocument document, String savePath, String fileame) throws IOException {
        File file = new File(savePath);
        if (!file.exists()) {
            // 判断生成目录是否存在,不存在时创建目录。
            file.mkdirs();
        }
        // 保存
        fileame = .docx;
        FileOutputStream out = new FileOutputStream(new File(savePath  File.separator  fileame));
        document.write(out);
        // 关闭资源
        out.flush();
        out.close();
        document.close();
    }

    public static void main(String[] args) throws IOException {
        XWPFDocument document = new XWPFDocument();
        // 将纸张大小设置为横向A4
        setPageSize(document, 16840, 11907);
        // 保存文件
        String savePath = D:\\poi;
        String fileame = PoiWord;
        saveDoc(document, savePath, fileame);
    }

结果如下:

width与height的取值规则
规则是纸张的长度(磅数)* 20
例横向A4纸的大小是 29.7(厘米)* 21(厘米),首先厘米转磅数在乘以20
宽:29.7 * 28.5 * 20 = 1689.9 ≈ 16840
长:21 * 28.5 * 20 = 11907

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1098991.html

相关标签:无
上传时间: 2025-07-15 10:30:08
留言与评论(共有 15 条评论)
本站网友 白银美食
15分钟前 发表
1. pom引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.4</version> </dependency> 2. 相关代码 import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; /** * 设置纸张大小 * * @param document doc对象 * @param width 宽 * @param height 长 */ public static void setPageSize(XWPFDocument document
本站网友 大学城租房网
27分钟前 发表
\\poi; String fileame = PoiWord; saveDoc(document
本站网友 富士康是干嘛的
1分钟前 发表
file.mkdirs(); } // 保存 fileame = .docx; FileOutputStream out = new FileOutputStream(new File(savePath  File.separator  fileame)); document.write(out); // 关闭资源 out.flush(); out.close(); document.close(); } public static void main(String[] args) throws IOException { XWPFDocument document = new XWPFDocument(); // 将纸张大小设置为横向A4 setPageSize(document
本站网友 服务精神
23分钟前 发表
1. pom引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.4</version> </dependency> 2. 相关代码 import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; /** * 设置纸张大小 * * @param document doc对象 * @param width 宽 * @param height 长 */ public static void setPageSize(XWPFDocument document
本站网友 木瓜的功效
14分钟前 发表
long width
本站网友 光子美白
23分钟前 发表
String savePath
本站网友 赶黄草
3分钟前 发表
fileame); } 结果如下: width与height的取值规则 规则是纸张的长度(磅数)* 20 例横向A4纸的大小是 29.7(厘米)* 21(厘米),首先厘米转磅数在乘以20 宽:29.7 * 28.5 * 20 = 1689.9 ≈ 16840 长:21 * 28.5 * 20 = 11907
本站网友 太原儿童医院
25分钟前 发表
调整纸张方向 前言 之前开发时,需要将纸张方向由纵向改为横向,查询资料得出只需要设置一下纸张的长度与宽度便可实现相同的效果
本站网友 泌尿外科女医生
8分钟前 发表
long height) { CTSectPr sectPr = document.getDocument().getBody().addewSectPr(); CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz()
本站网友 vir
11分钟前 发表
\\poi; String fileame = PoiWord; saveDoc(document
本站网友 坪山二手房
21分钟前 发表
调整纸张方向 前言 之前开发时,需要将纸张方向由纵向改为横向,查询资料得出只需要设置一下纸张的长度与宽度便可实现相同的效果
本站网友 牵手向明天
1分钟前 发表
JAVA - 使用Apache POI生成word(二) 设置纸张大小
本站网友 harpoon
1分钟前 发表
16840
本站网友 降准对房价的影响
26分钟前 发表
String savePath