编写所有基础工具,提供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.Config;
import com.docus.sw.word.GetPicsDocx; import com.docus.sw.word.GetPicsDocx;
import com.docus.sw.word.PdfBoxUtils; import com.docus.sw.word.PdfBoxUtils;
import com.docus.sw.word.ReadImgDoc;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.ImageReadException;
@ -111,7 +112,7 @@ public class FenpanService {
file.mkdirs(); file.mkdirs();
} }
GetPicsDocx.getPics(piece.getAbsolutePath(),file.getAbsolutePath()); ReadImgDoc.readPicture(piece.getAbsolutePath(),file.getAbsolutePath());
List<Document> documentList = new ArrayList<>(); List<Document> documentList = new ArrayList<>();
try { try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),file.getAbsolutePath()); 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.FileTypeEnum;
import com.docus.sw.fenpan.Pieces; import com.docus.sw.fenpan.Pieces;
import com.docus.sw.fenpan.Roll; 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.drew.tools.FileUtil;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageInfo;
@ -72,50 +75,69 @@ public class SuoyinService {
for(File piece:piecesFile){ for(File piece:piecesFile){
//区分word和pdf //区分word和pdf
if(piece.getName().endsWith(".doc")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
List<Document> documentList = new ArrayList<>(); File temp = new File("temp");
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName()); if(!temp.exists()){
for (File docfile : piece.listFiles()) { temp.mkdirs();
//非图片模式,跳过。
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 图像文件
ReadImgDoc.readPicture(piece.getAbsolutePath(),temp.getAbsolutePath());
try { try {
BufferedImage image = ImageIO.read(file); PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
int height = image.getHeight(); File[] listFiles = temp.listFiles();
int width = image.getWidth(); genPieces(pieces, listFiles);
Document document = new Document(width, height, 300);
documentList.add(document);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e);
} }
//删除对应的temp 文件
file.delete();
}else if(piece.getName().endsWith(".docx")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
} else { File temp = new File("temp");
if(!temp.exists()){
temp.mkdirs();
}
GetPicsDocx.getPics(piece.getAbsolutePath(),temp.getAbsolutePath());
try { try {
ImageInfo imageInfo = Imaging.getImageInfo(docfile); PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
int height = imageInfo.getHeight(); File[] listFiles = temp.listFiles();
int width = imageInfo.getWidth(); genPieces(pieces, listFiles);
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document);
} catch (IOException e) { } 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) {
} }
pieces.put(documentList); //删除对应的temp 文件
file.delete();
}else{
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName());
File[] listFiles = piece.listFiles();
genPieces(pieces, listFiles);
pieceList.add(pieces); 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) { private List<IndexPageRow> indexGen(IndexPlate indexPlate, List<Roll> rollList, Integer pianPageNum, Integer totalSize) {
List<IndexPageRow> indexPageRows = new ArrayList<>(); List<IndexPageRow> indexPageRows = new ArrayList<>();
int start = 1; 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.Picture;
import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.hwpf.usermodel.Range;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID; import java.util.UUID;
public class ReadImgDoc { public class ReadImgDoc {
@ -43,8 +40,10 @@ public class ReadImgDoc {
} }
private static void readPicture(String path, String toPath) throws Exception { public static void readPicture(String path, String toPath) {
FileInputStream in = new FileInputStream(new File(path)); FileInputStream in = null;
try {
in = new FileInputStream(new File(path));
HWPFDocument doc = new HWPFDocument(in); HWPFDocument doc = new HWPFDocument(in);
int length = doc.characterLength(); int length = doc.characterLength();
PicturesTable pTable = doc.getPicturesTable(); PicturesTable pTable = doc.getPicturesTable();
@ -58,8 +57,35 @@ public class ReadImgDoc {
if (pTable.hasPicture(cr)) { if (pTable.hasPicture(cr)) {
Picture pic = pTable.extractPicture(cr, false); Picture pic = pTable.extractPicture(cr, false);
String afileName = pic.suggestFullFileName(); String afileName = pic.suggestFullFileName();
OutputStream out = new FileOutputStream(new File(toPath + i + afileName)); OutputStream out = null;
try {
out = new FileOutputStream(new File(toPath + i + afileName));
pic.writeImageContent(out); 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