编写所有基础工具,提供word util pdf util

master
zhanghai 2 years ago
parent f5a56078ef
commit 2c0817360e

@ -4,6 +4,7 @@ import com.alibaba.excel.util.FileUtils;
import com.docus.sw.Config;
import com.docus.sw.word.GetPicsDocx;
import com.docus.sw.word.PdfBoxUtils;
import com.docus.sw.word.ReadImgDoc;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
@ -111,7 +112,7 @@ public class FenpanService {
file.mkdirs();
}
GetPicsDocx.getPics(piece.getAbsolutePath(),file.getAbsolutePath());
ReadImgDoc.readPicture(piece.getAbsolutePath(),file.getAbsolutePath());
List<Document> documentList = new ArrayList<>();
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),file.getAbsolutePath());

@ -7,6 +7,9 @@ import com.docus.sw.fenpan.Document;
import com.docus.sw.fenpan.FileTypeEnum;
import com.docus.sw.fenpan.Pieces;
import com.docus.sw.fenpan.Roll;
import com.docus.sw.word.GetPicsDocx;
import com.docus.sw.word.PdfBoxUtils;
import com.docus.sw.word.ReadImgDoc;
import com.drew.tools.FileUtil;
import com.google.gson.Gson;
import org.apache.commons.imaging.ImageInfo;
@ -72,50 +75,69 @@ public class SuoyinService {
for(File piece:piecesFile){
//区分word和pdf
if(piece.getName().endsWith(".doc")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
List<Document> documentList = new ArrayList<>();
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName());
for (File docfile : piece.listFiles()) {
File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
}
ReadImgDoc.readPicture(piece.getAbsolutePath(),temp.getAbsolutePath());
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles();
genPieces(pieces, listFiles);
} catch (IOException e) {
}
//删除对应的temp 文件
file.delete();
}else if(piece.getName().endsWith(".docx")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
}
GetPicsDocx.getPics(piece.getAbsolutePath(),temp.getAbsolutePath());
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles();
genPieces(pieces, listFiles);
} catch (IOException e) {
//非图片模式,跳过。
if(!(docfile.getName().endsWith(".jpg") || docfile.getName().endsWith(".png")
|| docfile.getName().endsWith(".jpeg") || docfile.getName().endsWith(".tif")
|| docfile.getName().endsWith(".tiff")|| file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm"))|| file.getName().endsWith(".gif")){
continue;
}
if (file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm")) {
// 读取 JPEG 2000 图像文件
try {
BufferedImage image = ImageIO.read(file);
int height = image.getHeight();
int width = image.getWidth();
Document document = new Document(width, height, 300);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
ImageInfo imageInfo = Imaging.getImageInfo(docfile);
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException("非图片格式", e);
} catch (ImageReadException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
}
//删除对应的temp 文件
file.delete();
}else if(piece.getName().endsWith(".pdf")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
}
GetPicsDocx.getPics(piece.getAbsolutePath(),temp.getAbsolutePath());
try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles();
genPieces(pieces, listFiles);
} catch (IOException e) {
}
//删除对应的temp 文件
file.delete();
}else{
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName());
File[] listFiles = piece.listFiles();
genPieces(pieces, listFiles);
pieceList.add(pieces);
}
pieces.put(documentList);
pieceList.add(pieces);
}
@ -230,6 +252,48 @@ public class SuoyinService {
//
}
private static void genPieces(Pieces pieces, File[] listFiles) {
List<Document> documentList = new ArrayList<>();
for (File docfile : listFiles) {
//非图片模式,跳过。
if(!(docfile.getName().endsWith(".jpg") || docfile.getName().endsWith(".png")
|| docfile.getName().endsWith(".jpeg") || docfile.getName().endsWith(".tif")
|| docfile.getName().endsWith(".tiff")|| docfile.getName().endsWith(".jp2") || docfile.getName().endsWith(".jpm"))|| docfile.getName().endsWith(".gif")){
continue;
}
if (docfile.getName().endsWith(".jp2") || docfile.getName().endsWith(".jpm")) {
// 读取 JPEG 2000 图像文件
try {
BufferedImage image = ImageIO.read(docfile);
int height = image.getHeight();
int width = image.getWidth();
Document document = new Document(width, height, 300);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
ImageInfo imageInfo = Imaging.getImageInfo(docfile);
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException("非图片格式", e);
} catch (ImageReadException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
}
}
pieces.put(documentList);
}
}
private List<IndexPageRow> indexGen(IndexPlate indexPlate, List<Roll> rollList, Integer pianPageNum, Integer totalSize) {
List<IndexPageRow> indexPageRows = new ArrayList<>();
int start = 1;

@ -6,10 +6,7 @@ import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.UUID;
public class ReadImgDoc {
@ -43,23 +40,52 @@ public class ReadImgDoc {
}
private static void readPicture(String path, String toPath) throws Exception {
FileInputStream in = new FileInputStream(new File(path));
HWPFDocument doc = new HWPFDocument(in);
int length = doc.characterLength();
PicturesTable pTable = doc.getPicturesTable();
// int TitleLength=doc.getSummaryInformation().getTitle().length();
public static void readPicture(String path, String toPath) {
FileInputStream in = null;
try {
in = new FileInputStream(new File(path));
HWPFDocument doc = new HWPFDocument(in);
int length = doc.characterLength();
PicturesTable pTable = doc.getPicturesTable();
// int TitleLength=doc.getSummaryInformation().getTitle().length();
// System.out.println(TitleLength);
// System.out.println(length);
for (int i = 0; i < length; i++) {
Range range = new Range(i, i + 1, doc);
CharacterRun cr = range.getCharacterRun(0);
if (pTable.hasPicture(cr)) {
Picture pic = pTable.extractPicture(cr, false);
String afileName = pic.suggestFullFileName();
OutputStream out = new FileOutputStream(new File(toPath + i + afileName));
pic.writeImageContent(out);
// System.out.println(TitleLength);
// System.out.println(length);
for (int i = 0; i < length; i++) {
Range range = new Range(i, i + 1, doc);
CharacterRun cr = range.getCharacterRun(0);
if (pTable.hasPicture(cr)) {
Picture pic = pTable.extractPicture(cr, false);
String afileName = pic.suggestFullFileName();
OutputStream out = null;
try {
out = new FileOutputStream(new File(toPath + i + afileName));
pic.writeImageContent(out);
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.getMessage();
}finally {
if(out!= null){
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
} catch (IOException e) {
e.getMessage();
}finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Loading…
Cancel
Save