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

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.kler.cn/news/343670.html

相关文章:

  • 基于微信小程序的家校联动平台管理系统的设计与实现(毕业论文)
  • 思维链ChatGPT
  • windows server 2019中安装.net framework 3.5功能出错
  • [AWS云]kafka调用和创建
  • leetcode15:三数之和
  • R语言统计分析——气泡图
  • 100套深度学习毕业设计项目合集【含源码 + 操作文档】
  • 笔试强训day37
  • 基于华为云智慧生活生态链设计的智能鱼缸
  • cronet的119.0.6045.31版本支持arm编译
  • stm32通过RS485总线控制云台运动
  • linux中的bootfs vendorfs rootfs userfs的作用
  • 单片机教案 2.2 ATmega2560单片机闪烁灯的制作和编程
  • 从零学编程-C语言-第17天
  • 嵌入式面试——FreeRTOS篇(四) 信号量
  • 如何删除不需要的右键菜单
  • 云计算相关
  • 2024系统分析师---试题三:论云原生架构及其应用
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-11
  • RabbitMQ 高级特性——死信队列