记录EXCEL或WORD转图片
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-excel</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
String data = DocumentImageUtil.documentImage(params, multipartFiles, null, null);
public class DocumentImageUtil {
public static String documentImage(Map<String, Object> params, MultipartFile multipartFiles, String path, String name) throws IOException {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = new byte[0];
if (params.containsKey("base64String")) {
bytes = decoder.decodeBuffer(String.valueOf(params.get("base64String")));
}
if (null != multipartFiles && multipartFiles.isEmpty()) {
bytes = multipartFiles.getBytes();
}
try {
ByteArrayInputStream docxInputStream = new ByteArrayInputStream(bytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
IConverter converter = LocalConverter.builder().build();
String type = params.get("type").toString();
if (Integer.valueOf(type) == 1) {
converter.convert(docxInputStream).as(DocumentType.MS_WORD).to(outputStream).as(DocumentType.PDF).execute();
}
if (Integer.valueOf(type) == 2) {
converter.convert(docxInputStream).as(DocumentType.MS_EXCEL).to(outputStream).as(DocumentType.PDF).execute();
}
PDDocument load = PDDocument.load(outputStream.toByteArray());
try {
List<String> list = new ArrayList<>();
if (StringUtils.isEmpty(name)) {
list = pdf2Image(load, 300);
} else {
pdf2ImageFile(load, 300, path, name);
}
outputStream.close();
return StringUtils.join(list.toArray(), ",");
} catch (Exception e) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static List<String> pdf2Image(PDDocument pdDocument, int dpi) {
List<String> list = new ArrayList<>();
try {
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pages = pdDocument.getNumberOfPages();
for (int i = 0; i < pages; i++) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "jpg", outputStream);
byte[] bytes = outputStream.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
String string = encoder.encode(bytes);
list.add(string.replaceAll("\\r\\n", ""));
pdDocument.close();
}
} catch (Exception e) {
}
return list;
}
public static void pdf2ImageFile(PDDocument pdDocument, int dpi, String path, String name) {
List<String> list = new ArrayList<>();
try {
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pages = pdDocument.getNumberOfPages();
if (createDirectory(path)) {
for (int i = 0; i < pages; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
File file = new File(path + name + ".jpg");
ImageIO.write(image, "jpg", file);
pdDocument.close();
}
}
} catch (Exception e) {
}
}
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
}