package com.byx.oa.watchdog.utils;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.convert.out.FOSettings;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.finders.ClassFinder;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.jaxb.Context;
import org.docx4j.model.datastorage.migration.VariablePrepare;
import org.docx4j.model.table.TblFactory;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.*;
import org.springframework.core.io.ClassPathResource;
import javax.xml.bind.JAXBElement;
import java.io.*;
import java.util.*;
/**
* Time: 2021/12/20 13:22
* Author: Dily
* Remark: maven 以下依赖一个不能少
* <dependency>
* <groupId>com.itextpdf</groupId>
* <artifactId>itextpdf</artifactId>
* <version>5.5.13.2</version>
* </dependency>
* <dependency>
* <groupId>freemarker</groupId>
* <artifactId>freemarker</artifactId>
* <version>2.3.8</version>
* </dependency>
* <dependency>
* <groupId>org.docx4j</groupId>
* <artifactId>docx4j</artifactId>
* <version>6.1.2</version>
* </dependency>
* <dependency>
* <groupId>org.docx4j</groupId>
* <artifactId>docx4j-export-fo</artifactId>
* <version>8.1.7</version>
* </dependency>
* </dependency>
* <dependency>
* <groupId>javax.xml.bind</groupId>
* <artifactId>jaxb-api</artifactId>
* <version>2.3.1</version>
* </dependency>
* <dependency>
* <groupId>javax.activation</groupId>
* <artifactId>activation</artifactId>
* <version>1.1</version>
* </dependency>
* <dependency>
* <groupId>org.glassfish.jaxb</groupId>
* <artifactId>jaxb-runtime</artifactId>
* <version>2.3.5</version>
* </dependency>
* <!-- 条形码 -->
* <dependency>
* <groupId>net.sf.barcode4j</groupId>
* <artifactId>barcode4j-light</artifactId>
* <version>2.0</version>
*/
public class Docx4jUtils {
/**
* 创建一个空白 Docx 文档
*
* @param filePath 文件路径
*/
public void createDocx(String filePath) throws Docx4JException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.save(new File(filePath));
}
/**
* 加载 Docx 文件
*
* @param filePath 文件地址
* @return WordProcessingMLPackage操作包
*/
public WordprocessingMLPackage loadDocx(String filePath) throws Docx4JException {
return WordprocessingMLPackage.load(new File(filePath));
}
/**
* 创建一个空白 Docx 文档
*
* @param outputStream 流
*/
public void createDocx(OutputStream outputStream) throws Docx4JException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.save(outputStream);
}
/**
* 添加带样式的文本/段落
*
* @param wordMLPackage docx 操作包
* @param styled 样式
* @param conText 文本
* @return WordProcessingMLPackage 操作包
*/
public WordprocessingMLPackage addContext(WordprocessingMLPackage wordMLPackage, String styled, String conText) {
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText(styled, conText);
return wordMLPackage;
}
/**
* 添加文本/段落
*
* @param wordMLPackage docx 操作包
* @param conText 正文
* @return WordProcessingMLPackage 操作包
*/
public WordprocessingMLPackage addContext(WordprocessingMLPackage wordMLPackage, String conText) {
wordMLPackage.getMainDocumentPart().addParagraphOfText(conText);
return wordMLPackage;
}
/**
* 添加图片
*
* @param wordMLPackage 操作包
* @param imagePath 图片地址
* @return WordProcessingMLPackage 操作包
*/
public WordprocessingMLPackage addImage(WordprocessingMLPackage wordMLPackage, String imagePath) throws Exception {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, new File(imagePath));
Inline inline = imagePart.createImageInline("Filename hint", "Alternative text", 1, 2, false);
ObjectFactory factory = new ObjectFactory();
P paragraph = factory.createP();
R run = factory.createR();
paragraph.getContent().add(run);
Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
wordMLPackage.getMainDocumentPart().addObject(paragraph);
return wordMLPackage;
}
/**
* 添加空表格
*
* @param wordMLPackage 操作包
* @param row 行数
* @param col 列数
* @return WordProcessingMLPackage 操作包
*/
public WordprocessingMLPackage addTable(WordprocessingMLPackage wordMLPackage, int row, int col) {
Tbl table = TblFactory.createTable(row, col, 20000 / col);
wordMLPackage.getMainDocumentPart().addObject(table);
return wordMLPackage;
}
/**
* 添加带数据表格, 数据必须是整齐的
* 表头为数据的 key, 表头在第一行
*
* @param wordMLPackage 操作包
* @param list 数据
* @return WordProcessingMLPackage 操作包
*/
public WordprocessingMLPackage addTableWithDataAndTopHeader(WordprocessingMLPackage wordMLPackage, List<Map<String, String>> list) {
Set<String> keySet = new HashSet<>(list.get(0).keySet());
ObjectFactory factory = Context.getWmlObjectFactory();
Tbl table = TblFactory.createTable(0, 0, 20000 / list.get(0).size());
// 表头
Tr tableHeader = factory.createTr();
keySet.forEach(e -> {
Tc tableCell = factory.createTc();
tableCell.getContent().add(wordMLPackage.getMainDocumentPart().createParagraphOfText(e));
tableHeader.getContent().add(tableCell);
});
table.getContent().add(tableHeader);
// 数据
list.forEach(e -> {
Tr tableRow = factory.createTr();
keySet.forEach(item -> {
Tc tableCell = factory.createTc();
tableCell.getContent().add(wordMLPackage.getMainDocumentPart().createParagraphOfText(e.get(item)));
tableRow.getContent().add(tableCell);
});
table.getContent().add(tableRow);
});
wordMLPackage.getMainDocumentPart().addObject(table);
return wordMLPackage;
}
/**
* 添加带数据表格, 数据必须是整齐的
* 表头为数据的 key, 表头在第一列
*
* @param wordMLPackage 操作包
* @param list 数据
* @return WordProcessingMLPackage 操作包
*/
public WordprocessingMLPackage addTableWithDataAndLeftHeader(WordprocessingMLPackage wordMLPackage, List<Map<String, String>> list) {
Set<String> keySet = new HashSet<>(list.get(0).keySet());
ObjectFactory factory = Context.getWmlObjectFactory();
Tbl table = TblFactory.createTable(0, 0, 20000 / list.get(0).size());
keySet.forEach(e -> {
Tr tableRow = factory.createTr();
Tc tableHeader = factory.createTc();
tableHeader.getContent().add(wordMLPackage.getMainDocumentPart().createParagraphOfText(e));
tableRow.getContent().add(tableHeader);
list.forEach(item -> {
Tc tableCell = factory.createTc();
tableCell.getContent().add(wordMLPackage.getMa
评论0