docx4j

java实现word生成并转pdf

大城市里の小女人 提交于 2020-09-25 10:59:14
前言 本篇博客主要解决java后台动态生成word(docx格式),并将word转换为pdf并添加水印。 思考 项目需求是要导出带水印的pdf,表格样式还是有点复杂的,之前考虑过用itextpdf根据html来生成pdf,但框架用的是前后台分 离的,前台用的是react,并且是在没有展示出表格的情况下,所以没法通过前台获取html代码块生成,后来又自己手动拼接 html,但代码量太大,难维护,且样式不怎么好看。所以决定用freemarker模板生成word,再转成pdf。翻阅网上很多资料给 出了很多方案将word转pdf,有用poi的、有用第三方工具的等等。用poi的写的都太复杂,jar引用很多,用第三方工具的有局 限性,不适合夸平台,需要安装服务。所以决定用docx4j,但docx4j只支持docx格式的word转pdf,所以需要freemarker 生成docx的word。 动手 1、pom引入依赖 < dependency > < groupId > com.itextpdf </ groupId > < artifactId > itextpdf </ artifactId > < version > 5.4.3 </ version > </ dependency > < dependency > < groupId > freemarker </ groupId > <

SUM(ABOVE) functionality in apache poi or docx4j

こ雲淡風輕ζ 提交于 2020-05-17 06:05:49
问题 I am trying to implement =SUM(ABOVE) function in docx which is used to sum all elements above to tht column. I was able to implement this with apache poi using : CTSimpleField sumAbove = paragraphInCell.getCTP().addNewFldSimple(); sumAbove.setInstr("=SUM(ABOVE)"); //set sum field dirty, so it must be calculated while opening the document sumAbove.setDirty(STOnOff.TRUE); This is ok when someone opens the document and it calculates it. But if I need to convert the document without opening it to

SUM(ABOVE) functionality in apache poi or docx4j

一曲冷凌霜 提交于 2020-05-17 06:04:24
问题 I am trying to implement =SUM(ABOVE) function in docx which is used to sum all elements above to tht column. I was able to implement this with apache poi using : CTSimpleField sumAbove = paragraphInCell.getCTP().addNewFldSimple(); sumAbove.setInstr("=SUM(ABOVE)"); //set sum field dirty, so it must be calculated while opening the document sumAbove.setDirty(STOnOff.TRUE); This is ok when someone opens the document and it calculates it. But if I need to convert the document without opening it to

java 在线拆分 word文档采用什么技术比较好?

五迷三道 提交于 2020-04-30 11:43:07
在Java项目开发中,偶尔会遇到通过程序动态拆分word文档的需求,由于Java本身不能操作Word文档,在网上也都是讨论如何动态合并word,所以这个需求实现起来相当困难,下面就将近期对于Word文件拆分的需求及其实现方式的研究做个简单的总结,供大家参考: 1 VBA可以实现word文档拆分功能,在网上也很容易找到示例代码,然而VBA虽然强大,但是对于Java开发的项目提供不了任何帮助。 2 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能,但POI的接口非常庞大,方法与属性晦涩难懂,所以多用于简单的处理一下word文件中的数据,比如动态填充数据到word,或修改一下段落格式、文本样式等。 3 docx4j算是一款处理word文档的强大工具。虽然它对WORD各种处理在API层面进行了封装,但是像WORD本身的拆分,合并,其作者(Jason Harrop)是单独提出来了,封装成了商用的JAR包来提供支持。docx4j建立了XML文件与JAVA对象互相转换的一个桥梁,所以顾名思义,docx4j主要用来对docx进行一些操作,对于二进制结构化文档的.doc格式就无能为力了。另外就是稍微大一点的docx文档中的xml都会异常庞大和复杂,在服务器端解析xml会占用大量的系统资源

Android - java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;

北城余情 提交于 2020-04-18 05:36:20
问题 I did some search already regarding this run time error: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory and here's the "libs" in my Android project: As you can see, slf4j-android-1.7.25.jar in there. I also tried latest slf4j-android-1.7.30.jar, still having this issue. Basically, I am trying to convert MS WORD file to pdf and here's the code I am trying which I found somewhere: public static void docToPdf(InputStream is, String outputPath){

Docx4j 添加页码

回眸只為那壹抹淺笑 提交于 2020-03-06 11:22:38
Office 2007 之后使用 XML 格式来存储文档数据,这个变化其实是非常不错的。如何操作这些XML生成文件呢?或许你还在使用 POI,也有可能你在使用已经放弃更新的 iText (只是doc已经放弃更新,PDF持续更新),但是我觉得,你可以选择使用 Docx4j,这是个不错的开源软件,国内很少使用而已。 下面就简单的使用 docx4j添加页码的代码做一个简单的介绍。 /** * 创建页脚(分页信息) */ public static void createFooter(WordprocessingMLPackage wordMLPackage) throws Exception { Relationship relationship = DocxUtil.createFootPart(wordMLPackage); DocxUtil.createFootReference(wordMLPackage, relationship); } /** * 创建页脚的组件 */ public static Relationship createFootPart( WordprocessingMLPackage wordprocessingMLPackage) throws Exception { FooterPart footerPart = new FooterPart();

Converting multiple pages of a Docx into multiple PNG files

我只是一个虾纸丫 提交于 2020-01-25 07:37:11
问题 I am trying to convert a .docx document into a .png file using Docx4j in java , and achieved the conversion using the piece of code below. The generated image is for the first page only, while I want to have multiple png files , one for each page of the docx document. I could not find any way to put this thing in a loop or extract the document pagewise. WordprocessingMLPackage wordMLPckg = Docx4J.load(theFile); OutputStream os = new FileOutputStream(outFile); FOSettings settings = Docx4J

Adding image with docx4j to doc file

蓝咒 提交于 2020-01-16 19:25:33
问题 I'm trying to add an image to a docx file using docx4j library within Android. I've faced to an exception: E/AndroidRuntime(21818): java.lang.ExceptionInInitializerError E/AndroidRuntime(21818): at org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.newSource(AbstractImageSessionContext.java:134) E/AndroidRuntime(21818): at org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.needSource(AbstractImageSessionContext.java:280) E/AndroidRuntime(21818): at org

docx4j cannot render <text-indent>

余生长醉 提交于 2020-01-15 11:17:30
问题 I am using docx4j-ImportXHTML to convert an XHTML into a docx. When rendering to docx I notice it doesn't render CSS text-indent property. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Title of document</title> </head> <body> <p style="text-indent:100px;">indent text</p> </body> </html> When running above code I do not see any indentation of the first line of

How would I convert UTF-8mb4 to UTF-8?

依然范特西╮ 提交于 2020-01-15 09:09:31
问题 I'm using docx4j to convert .docx files into html, then saving that data into a MySQL database. Unfortunately, we've hit a snag. When we convert a doc that includes any characters encoded in utf8mb4, and then try and submit that data to our MySQL server, we're hit with a Generic JDBC Exception which states that it doesn't know how to parse the utf8mb4 characters. ERROR pool-3-thread-20 org.hibernate.util.JDBCExceptionReporter - Incorrect string value: '\xEF\xBF\xBD???...' for column 'u