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

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().addNewSectPr();
        CTPageSz pgsz = sectPr.isSetPgSz() ? sectPr.getPgSz() : sectPr.addNewPgSz();
        pgsz.setW(BigInteger.valueOf(width));
        pgsz.setH(BigInteger.valueOf(height));
    }

    /**
     * 保存文件
     *
     * @param document doc对象
     * @param savePath 保存路径
     * @param fileName 文件名称
     */
    public static void saveDoc(XWPFDocument document, String savePath, String fileName) throws IOException {
        File file = new File(savePath);
        if (!file.exists()) {
            // 判断生成目录是否存在,不存在时创建目录。
            file.mkdirs();
        }
        // 保存
        fileName += ".docx";
        FileOutputStream out = new FileOutputStream(new File(savePath + File.separator + fileName));
        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 fileName = "PoiWord";
        saveDoc(document, savePath, fileName);
    }

结果如下:
在这里插入图片描述

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

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用Apache POI生成Word文档,你可以按照以下步骤进行操作: 1. 导入所需的POI库: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 编写代码来创建并编辑Word文档: ```java import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class WordDocumentGenerator { public static void main(String[] args) { // 创建一个新的Word文档 XWPFDocument document = new XWPFDocument(); // 创建段落 XWPFParagraph paragraph = document.createParagraph(); // 创建文本运行 XWPFRun run = paragraph.createRun(); // 设置文本内容 run.setText("这是一个生成Word文档。"); // 添加换行符 paragraph.createRun().addBreak(); // 添加一个带有样式的文本 XWPFRun styledRun = paragraph.createRun(); styledRun.setText("这是带有样式的文本。"); styledRun.setBold(true); styledRun.setFontSize(14); // 保存文档 try { FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); System.out.println("生成Word文档成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码创建了一个简单的Word文档,其中包含两个段落。第一个段落包含文本"这是一个生成Word文档。",第个段落包含带有样式的文本"这是带有样式的文本。"。最后,将文档保存为名为`example.docx`的文件。你可以根据需要添加更多的段落、表格、图片以及其他格式设置。记得根据实际情况进行异常处理。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值