当前位置: 首页 > news >正文

poi通过在word中写入了表格,通过libreoffice转换成PDF后,word中刚才画的表格宽度无限拉伸问题的解决。

一、复现:

poi版本:

   		<poi>3.17</poi><poi-ooxml>3.17</poi-ooxml><poi-ooxml-schemas>3.17</poi-ooxml-schemas><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>${poi}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi-ooxml}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>${poi-ooxml-schemas}</version></dependency>

1、生成word:

在这里插入图片描述

2、通过libreoffice转换成pdf:

在这里插入图片描述

二、失败尝试:设置单元格长度宽度,设置表格居中均无效。

三、解决思路:

1、在word的单元格中,打几个空格,空格其实在红色指到的地方,然后转换成PDF后发现没问题了。

在这里插入图片描述

2、将成功和失败的文档进行对比:最后发现表格中格式不太一样

①成功的

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xml-fragment xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oel="http://schemas.microsoft.com/office/2019/extlst" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16du="http://schemas.microsoft.com/office/word/2023/wordml/word16du" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"><w:tblPr><w:tblW w:type="auto" w:w="0"/><w:tblInd w:type="dxa" w:w="10"/><w:tblBorders><w:top w:color="auto" w:space="0" w:sz="0" w:val="single"/><w:left w:color="auto" w:space="0" w:sz="0" w:val="single"/><w:bottom w:color="auto" w:space="0" w:sz="0" w:val="single"/><w:right w:color="auto" w:space="0" w:sz="0" w:val="single"/><w:insideH w:color="auto" w:space="0" w:sz="0" w:val="single"/><w:insideV w:color="auto" w:space="0" w:sz="0" w:val="single"/></w:tblBorders><w:tblCellMar><w:left w:type="dxa" w:w="10"/><w:right w:type="dxa" w:w="10"/></w:tblCellMar><w:tblLook w:firstColumn="0" w:firstRow="0" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="0" w:val="0000"/></w:tblPr><w:tblGrid><w:gridCol w:w="26"/></w:tblGrid><w:tr w14:paraId="77529834" w14:textId="77777777" w:rsidR="0005540F"><w:tblPrEx><w:tblCellMar><w:top w:type="dxa" w:w="0"/><w:bottom w:type="dxa" w:w="0"/></w:tblCellMar></w:tblPrEx><w:tc><w:tcPr><w:tcW w:type="auto" w:w="0"/></w:tcPr><w:p w14:paraId="56EDAEAA" w14:textId="1DDD1BD7" w:rsidR="0005540F" w:rsidRDefault="001E39EF"><w:pPr><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia"/></w:rPr><w:t xml:space="preserve"/></w:r></w:p></w:tc></w:tr></xml-fragment>

②失败的

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xml-fragment xmlns:main="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><main:tblPr><main:tblW main:type="auto" main:w="0"/><main:tblBorders><main:top main:val="single"/><main:left main:val="single"/><main:bottom main:val="single"/><main:right main:val="single"/><main:insideH main:val="single"/><main:insideV main:val="single"/></main:tblBorders></main:tblPr><main:tr><main:tc><main:p/></main:tc></main:tr></xml-fragment>

四、解决方案:设置一下样式,自己画的table放入如下方法

    private static void setTableStyle(XWPFTable table) {// 获取表格的底层 CTTbl 对象CTTbl ctTbl = table.getCTTbl();// 获取或创建表格属性 (tblPr)CTTblPr tblPr = ctTbl.getTblPr();if (tblPr == null) {tblPr = ctTbl.addNewTblPr();}// 获取或创建表格宽度属性 (tblW)CTTblWidth tblWidth = tblPr.getTblW();if (tblWidth == null) {tblWidth = tblPr.addNewTblW();}// 设置表格宽度的类型为 "auto"tblWidth.setType(STTblWidth.AUTO);// 设置宽度为 0,因为当 type="auto" 时,宽度会根据内容自动调整tblWidth.setW(BigInteger.ZERO);// 设置表格网格CTTblGrid tblGrid = ctTbl.getTblGrid();if (tblGrid == null) {tblGrid = ctTbl.addNewTblGrid();CTTblGridCol ctTblGridCol = tblGrid.addNewGridCol();ctTblGridCol.setW(BigInteger.valueOf(9000));}}

五、效果:表格拉宽了。(居中代码就从其他地方抄一下吧,文章很多的)

在这里插入图片描述

http://www.lryc.cn/news/462754.html

相关文章:

  • 尚硅谷rabbitmq2024 集群篇仲裁队列 第52节 答疑
  • 《Spring Cloud 微服务:构建高效、灵活的分布式系统》
  • OpenFeign 入门与实战:快速搭建 Spring Cloud 微服务客户端
  • 上门按摩系统开发方案源码搭建
  • 【数据结构】宜宾大学-计院-实验四
  • selenium的IDE插件进行录制和回放并导出为python/java脚本(10)
  • 从0到1封装一个image/pdf预览组件
  • Android build子系统(02)Ninja语法与复杂依赖构建解读
  • JavaScript的第三天
  • 初识git · 有关模型
  • 基于SpringBoot+Vue+uniapp的海产品加工销售一体化管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
  • 解锁机器人视觉与人工智能的潜力,从“盲人机器”改造成有视觉能力的机器人(下)
  • CORS预检请求配置流程图 srpingboot和uniapp
  • 用Spring AI 做智能客服,基于私有知识库和RAG技术
  • TemporalBench:一个专注于细粒度时间理解的多模态视频理解的新基准。
  • 网友提问:网上申请流量卡不通过怎么办?
  • JavaWeb 22.Node.js_简介和安装
  • APIJSON的使用
  • 简单三步完成 Telegram 生态的 Web3 冷启动
  • Go Wails 学习笔记:创建第一个项目
  • Postman使用-基础篇
  • LeetCode 202.快乐数
  • Redis-03 持久化(RDB, AOF,混合持久化)及原理
  • TikTok账号策略:IP和网络环境的要求分析
  • vue后台管理系统从0到1(5)
  • OpenAI的新功能Canvas,效果还不错
  • 了解一些常用的Javascript对象方法
  • 【知识科普】GraphQL一个强大的API查询语言
  • Spring Boot 整合达梦
  • Vue.js 组件开发基本步骤