Apache POI对Word的处理

2024-08-27 16:58
文章标签 处理 apache word poi

本文主要是介绍Apache POI对Word的处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

写在前面

当我们对docx进行内容上的修改时,可以在修改后对docx文件用压缩包软件进行解压,然后在解压出来的文件夹里的word文件夹下的document.xml文件进行查看对比(毕竟docx是采用ooxml标准,所以内容及其属性都是以xml的形式来写)。

解压后的文件结构大致如下:

├── [Content_Types].xml
├── _rels
├── _xmlsignatures (该文件夹会在有数字签名的情况下生成)
│   ├── _rels
│   │   └── origin.sigs.rels
│   ├── origin.sigs
│   └── sig1.xml
├── docProps
│   ├── app.xml
│   └── core.xml
└── word├── _rels│   └── document.xml.rels├── document.xml├── fontTable.xml├── settings.xml├── styles.xml├── theme│   └── theme1.xml└── webSettings.xml

代码部分是groovy,与Java差别不大,很容易进行改写。

操作

1.添加图片

1.1 常规

Apache POI官方提供了一些简单的samples,包含了图片和图表的添加等。

def addImage(String doc, String img, int width, int height) {//判断图片格式int format = adjustImageFormat(img)if (format != -1) {//针对已存在的文件,应使用            //XWPFDocument document = new XWPFDocument(new FileInputStream(doc))XWPFDocument document = new XWPFDocument()XWPFParagraph paragraph = document.createParagraph()XWPFRun run = paragraph.createRun()//获取图片文件,路径,格式和大小run.addPicture(new FileInputStream(img), format, img, Units.toEMU(width), Units.toEMU(height))FileOutputStream outputStream = new FileOutputStream(doc)document.write(outputStream)}}/*** 判断图片格式* @param imgFile* @return*/private int adjustImageFormat(String imgFile) {int formatif (imgFile.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMFelse if (imgFile.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMFelse if (imgFile.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICTelse if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) format = XWPFDocument.PICTURE_TYPE_JPEGelse if (imgFile.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNGelse if (imgFile.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIBelse if (imgFile.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIFelse if (imgFile.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFFelse if (imgFile.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPSelse if (imgFile.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMPelse if (imgFile.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPGelse {System.err.println("Unsupported picture: " + imgFile +". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg")return -1}return format}

从目前来看,图片的添加似乎只能进行常规性的操作,就是在文字的后面追加。而对于我们想让图片对文字进行环绕的相关操作(比方说 衬于文字下方 )是没有的。

1.2 图片属性添加

在一开始的时候我们说过,当我们对word的内容进行修改的时候,它是在对document.xml里的相关内容进行修改,那么我们要如何看懂里面的内容呢?可以通过officeopenxml 网站进行了解和学习。

以我们这一节为例,我们需要了解在docx下插入图片后,对其进行相关的调整,那么就应该参考这里 DrawingML Overview

我们新建一个docx文档,然后插入图片,保存后我们可以看到其document.xml里的内容是这样(只截取主要部分):

<w:drawing><wp:inline distB="0" distL="0" distR="0" distT="0"><wp:extent cx="5270500" cy="7058025"/><wp:effectExtent b="3175" l="0" r="0" t="0"/><wp:docPr id="1" name="图片 1"/><wp:cNvGraphicFramePr><a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="1" name="D59B2F44-A382-4B8D-94E7-8457E3581342.jpg"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip cstate="print" r:embed="rId4"><a:extLst><a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/></a:ext></a:extLst></a:blip><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="5270500" cy="7058025"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing>

然后我们把图片的 环绕文字 设置为 浮于文字之上 ,变化后的document.xml为:

<w:drawing><wp:anchor allowOverlap="1" behindDoc="0" distB="0" distL="114300" distR="114300" distT="0" layoutInCell="1" locked="0" relativeHeight="251658240" simplePos="0"><wp:simplePos x="0" y="0"/><wp:positionH relativeFrom="column"><wp:posOffset>1905</wp:posOffset></wp:positionH><wp:positionV relativeFrom="paragraph"><wp:posOffset>40005</wp:posOffset></wp:positionV><wp:extent cx="5270500" cy="7058025"/><wp:effectExtent b="3175" l="0" r="0" t="0"/><wp:wrapNone/><wp:docPr id="1" name="图片 1"/><wp:cNvGraphicFramePr><a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="1" name="D59B2F44-A382-4B8D-94E7-8457E3581342.jpg"/><pic:cNvPicPr/></pic:nvPicPr><pic:blipFill><a:blip cstate="print" r:embed="rId4"><a:extLst><a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/></a:ext></a:extLst></a:blip><a:stretch><a:fillRect/></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="5270500" cy="7058025"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic><wp14:sizeRelH relativeFrom="page"><wp14:pctWidth>0</wp14:pctWidth></wp14:sizeRelH><wp14:sizeRelV relativeFrom="page"><wp14:pctHeight>0</wp14:pctHeight></wp14:sizeRelV></wp:anchor></w:drawing>

通过对比,我们主要看到的一个变化是从< wp:inline >标签替换为< wp:anchor >标签开始,这是图片从内联到浮动的一个标志。然后是对anchor的属性以及它的下级标签等进行一些相关的设置,具体的内容参考 Positioning within a Word Processing Document 。当我们从内联切换到浮动后,对图片进行 浮在文字上 的设置主要是anchor标签下的behindDoc属性设为false,同时出现一个< wp:wrapNone/>的空标签。

以上主要是对从ooxml的角度来看当图片改变时在docx文档上的变化。那么目前我们要怎么通过Apache POI来处理?由于Apache POI应该只是进行了常规性的操作,所以我们需要自己尝试来编写一些规则,可以参考Stack Overflow上的这个问题Wrap Text in Apache POI(docx)? 进行处理。

1.3 参考

关于图片对文字的环绕请参考 :Positioning within a Word Processing Document - Floating Pictures - Text Wrapping

关于图片位置的确定请参考 :Positioning within a Word Processing Document - Floating Pictures - Positioning

1.4 其它

  1. 关于CTAnchor missing dependency CTPosH的问题
    使用Groovy编译的过程中报出以下的错误:
Error:Groovyc: While compiling signer-office_main: 
java.lang.RuntimeException: java.lang.NoClassDefFoundError: 
Unable to load class org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor 
due to missing dependency 
org/openxmlformats/schemas/drawingml/x2006/wordprocessingDrawing/CTPosH

原因在于poi-schema库下缺乏了CTPosH这个类,因为这是一个精简版的jar,只包含一些典型的类,所以体积比较小。因此为了解决这个问题,我们需要使用完整的schema的jar包: I’m using the poi-ooxml-schemas jar, but my code is failing with “java.lang.NoClassDefFoundError: org/openxmlformats/schemas/something


2. 安全

2.1 签名

 def addSignature(String doc) {//pfx证书密码def password = pfxPassoword.toCharArray()File file = new File(pfxPath)KeyStore keystore = KeyStore.getInstance("PKCS12")FileInputStream fis = new FileInputStream(file)keystore.load(fis, password)fis.close()//获取别名Enumeration enumas = keystore.aliases()String alias = nullwhile (enumas.hasMoreElements()) {alias = (String) enumas.nextElement()}//准备密钥对Key key = keystore.getKey(alias, password)KeyPair keyPair = nullif (key instanceof PrivateKey) {Certificate cert = keystore.getCertificate(alias)PublicKey publicKey = cert.getPublicKey()keyPair = new KeyPair(publicKey, (PrivateKey) key)}//签名配置SignatureConfig signatureConfig = new SignatureConfig()signatureConfig.setKey(keyPair.getPrivate())X509Certificate x509 = (X509Certificate) keystore.getCertificate(alias)signatureConfig.setSigningCertificateChain(Collections.singletonList(x509))//打开文件OPCPackage opcPackage = OPCPackage.open(new File(doc), PackageAccess.READ_WRITE)signatureConfig.setOpcPackage(opcPackage)//装载签名配置SignatureInfo si = new SignatureInfo()si.setSignatureConfig(signatureConfig)//调用签名方法si.confirmSignature()//关闭文件opcPackage.close()}

2.2 验签

  boolean verify(String file) {boolean result = falseInputStream inputStream = FileMagic.prepareToCheckMagic(new FileInputStream(file))if (FileMagic.valueOf(inputStream) == FileMagic.OLE2) {System.out.println("Unsupport doc format")} else if (FileMagic.valueOf(inputStream) == FileMagic.OOXML) {try {OPCPackage opcPackage = OPCPackage.open(file, PackageAccess.READ)SignatureConfig signatureConfig = new SignatureConfig()signatureConfig.setOpcPackage(opcPackage)SignatureInfo signatureInfo = new SignatureInfo()signatureInfo.setSignatureConfig(signatureConfig)result = signatureInfo.verifySignature()} catch (InvalidFormatException e) {e.printStackTrace()}}return result}

2.3 获取证书信息

在Apache POI 4.0.0版本里对读取签名文档里的证书信息做了进一步的完善和调整。
4.0.0版本所搭配的ooxml-schemas版本应为1.4

    def poi_version = "4.0.0"compile "org.apache.poi:poi:${poi_version}"compile "org.apache.poi:poi-ooxml:${poi_version}"compile "org.apache.poi:ooxml-schemas:1.4"

在获取证书的时候要注意两个点:

  1. 设置signatureConfig.setUpdateConfigOnValidate(true)
  2. 调用si.verifySignature()
    执行完以上两步后,SignatureConfig的实例就会被更新,我们就可以获取到如签署时间,签名证书等其他信息
            OPCPackage opcPackage = OPCPackage.open(new ByteArrayInputStream(docx))SignatureConfig signatureConfig = new SignatureConfig()signatureConfig.setUpdateConfigOnValidate(true)signatureConfig.setOpcPackage(opcPackage)SignatureInfo si = new SignatureInfo()si.setSignatureConfig(signatureConfig)si.verifySignature()Date signDate = signatureConfig.executionTime//读取签名证书信息for (X509Certificate x509Certificate : signatureConfig.signingCertificateChain) {......}

2.3 参考

Apache POI - Encryption support

Extract configuration while verifying XML signatures

这篇关于Apache POI对Word的处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1112254

相关文章

深入理解Apache Kafka(分布式流处理平台)

《深入理解ApacheKafka(分布式流处理平台)》ApacheKafka作为现代分布式系统中的核心中间件,为构建高吞吐量、低延迟的数据管道提供了强大支持,本文将深入探讨Kafka的核心概念、架构... 目录引言一、Apache Kafka概述1.1 什么是Kafka?1.2 Kafka的核心概念二、Ka

resultMap如何处理复杂映射问题

《resultMap如何处理复杂映射问题》:本文主要介绍resultMap如何处理复杂映射问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录resultMap复杂映射问题Ⅰ 多对一查询:学生——老师Ⅱ 一对多查询:老师——学生总结resultMap复杂映射问题

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

Python实现自动化接收与处理手机验证码

《Python实现自动化接收与处理手机验证码》在移动互联网时代,短信验证码已成为身份验证、账号注册等环节的重要安全手段,本文将介绍如何利用Python实现验证码的自动接收,识别与转发,需要的可以参考下... 目录引言一、准备工作1.1 硬件与软件需求1.2 环境配置二、核心功能实现2.1 短信监听与获取2.