支持pdf

master
zhanghai 2 years ago
parent 2c0817360e
commit 5c9f267a4a

@ -39,7 +39,7 @@ public class SuoyinService {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Config.loadConfig(); Config.loadConfig();
String path = "C:\\jiahsi-saomiao\\0001"; String path = "C:\\Users\\zhanghai\\Desktop\\桌面\\test\\aa\\南方医院zong";
new SuoyinService().index(path); new SuoyinService().index(path);
} }
@ -87,15 +87,16 @@ public class SuoyinService {
try { try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath()); PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles(); File[] listFiles = temp.listFiles();
genPieces(pieces, listFiles); genPieces(pieces, listFiles,FileTypeEnum.DOC);
pieceList.add(pieces);
} catch (IOException e) { } catch (IOException e) {
} }
//删除对应的temp 文件 //删除对应的temp 文件
file.delete(); FileUtils.delete(temp);
}else if(piece.getName().endsWith(".docx")){ }else if(piece.getName().endsWith(".docx")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName()); Pieces pieces = new Pieces(FileTypeEnum.DOCX, piece.getAbsolutePath(), piece.getName());
File temp = new File("temp"); File temp = new File("temp");
if(!temp.exists()){ if(!temp.exists()){
@ -106,13 +107,13 @@ public class SuoyinService {
try { try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath()); PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles(); File[] listFiles = temp.listFiles();
genPieces(pieces, listFiles); genPieces(pieces, listFiles,FileTypeEnum.DOCX);
pieceList.add(pieces);
} catch (IOException e) { } catch (IOException e) {
} }
//删除对应的temp 文件 //删除对应的temp 文件
file.delete(); FileUtils.delete(temp);
}else if(piece.getName().endsWith(".pdf")){ }else if(piece.getName().endsWith(".pdf")){
Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName()); Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName());
@ -121,33 +122,35 @@ public class SuoyinService {
temp.mkdirs(); temp.mkdirs();
} }
GetPicsDocx.getPics(piece.getAbsolutePath(),temp.getAbsolutePath());
try { try {
PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath()); PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath());
File[] listFiles = temp.listFiles(); File[] listFiles = temp.listFiles();
genPieces(pieces, listFiles); genPieces(pieces, listFiles,FileTypeEnum.DOC);
pieceList.add(pieces);
} catch (IOException e) { } catch (IOException e) {
} }
//删除对应的temp 文件 //删除对应的temp 文件
file.delete(); // temp.delete();
FileUtils.delete(temp);
}else{ }else{
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName()); Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName());
File[] listFiles = piece.listFiles(); File[] listFiles = piece.listFiles();
genPieces(pieces, listFiles); genPieces(pieces, listFiles,FileTypeEnum.JPG);
pieceList.add(pieces); pieceList.add(pieces);
} }
} }
//填充卷 //填充卷
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath()); Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
roll.putAll(pieceList); roll.putAll(pieceList);
rollList.add(roll); rollList.add(roll);
} }
} }
indexPlate.putAll(rollList); indexPlate.putAll(rollList);
@ -252,46 +255,51 @@ public class SuoyinService {
// //
} }
private static void genPieces(Pieces pieces, File[] listFiles) { private static void genPieces(Pieces pieces, File[] listFiles,FileTypeEnum fileTypeEnum) {
List<Document> documentList = new ArrayList<>(); List<Document> documentList = new ArrayList<>();
for (File docfile : listFiles) {
//非图片模式,跳过。 for (File docfile : listFiles) {
if(!(docfile.getName().endsWith(".jpg") || docfile.getName().endsWith(".png") //非图片模式,跳过。
|| docfile.getName().endsWith(".jpeg") || docfile.getName().endsWith(".tif") if(!(docfile.getName().endsWith(".jpg") || docfile.getName().endsWith(".png")
|| docfile.getName().endsWith(".tiff")|| docfile.getName().endsWith(".jp2") || docfile.getName().endsWith(".jpm"))|| docfile.getName().endsWith(".gif")){ || docfile.getName().endsWith(".jpeg") || docfile.getName().endsWith(".tif")
continue; || 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);
} }
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 { } else {
try { try {
ImageInfo imageInfo = Imaging.getImageInfo(docfile); ImageInfo imageInfo = Imaging.getImageInfo(docfile);
int height = imageInfo.getHeight(); int height = imageInfo.getHeight();
int width = imageInfo.getWidth(); int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi(); int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi); Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document); documentList.add(document);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("非图片格式", e); throw new RuntimeException("非图片格式", e);
} catch (ImageReadException e) { } catch (ImageReadException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}
} }
} pieces.put(documentList);
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) {
@ -369,10 +377,20 @@ public class SuoyinService {
//判断是word,pdf,pic //判断是word,pdf,pic
if (o.getName().endsWith(".pdf")) { if (o.getName().endsWith(".pdf")) {
File rofile = o.getParentFile().getParentFile();
// new Plate() // new Plate()
if(map.get(rofile.getAbsolutePath())==null){
IndexPlate indexPlate = new IndexPlate(rofile.getAbsolutePath(), rofile.getName(),rofile.getParentFile().getName());
map.put(rofile.getAbsolutePath(),indexPlate);
}
} else if (o.getName().endsWith(".docx") || o.getName().endsWith(".doc")) { } else if (o.getName().endsWith(".docx") || o.getName().endsWith(".doc")) {
File rofile = o.getParentFile().getParentFile();
// new Plate()
if(map.get(rofile.getAbsolutePath())==null){
IndexPlate indexPlate = new IndexPlate(rofile.getAbsolutePath(), rofile.getName(),rofile.getParentFile().getName());
map.put(rofile.getAbsolutePath(),indexPlate);
}
} else if (o.getName().endsWith(".jpg") || o.getName().endsWith(".png") } else if (o.getName().endsWith(".jpg") || o.getName().endsWith(".png")
|| o.getName().endsWith(".jpeg") || o.getName().endsWith(".tif") || o.getName().endsWith(".jpeg") || o.getName().endsWith(".tif")
|| o.getName().endsWith(".tiff")) { || o.getName().endsWith(".tiff")) {

@ -118,7 +118,7 @@ public class PdfBoxUtils {
if (pdxObject instanceof PDImageXObject) { if (pdxObject instanceof PDImageXObject) {
PDImageXObject imageXObject = (PDImageXObject) pdxObject; PDImageXObject imageXObject = (PDImageXObject) pdxObject;
BufferedImage image = imageXObject.getImage(); BufferedImage image = imageXObject.getImage();
ImageIO.write(image,"jpg",new File(this.des + this.name + "-" + this.imageNum + ".jpg")); ImageIO.write(image,"jpg",new File(this.des +"/"+ this.name + "-" + this.imageNum + ".jpg"));
++this.imageNum; ++this.imageNum;
} else if (pdxObject instanceof PDFormXObject) { } else if (pdxObject instanceof PDFormXObject) {
PDFormXObject form = (PDFormXObject) pdxObject; PDFormXObject form = (PDFormXObject) pdxObject;

Loading…
Cancel
Save