From 5c9f267a4a101c6af121bc7e564129a234b0fc6c Mon Sep 17 00:00:00 2001 From: zhanghai <120228220@qq.com> Date: Mon, 27 Nov 2023 09:06:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/docus/sw/souyin/SuoyinService.java | 114 ++++++++++-------- .../java/com/docus/sw/word/PdfBoxUtils.java | 2 +- 2 files changed, 67 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/docus/sw/souyin/SuoyinService.java b/src/main/java/com/docus/sw/souyin/SuoyinService.java index 936eb66..f494c8b 100644 --- a/src/main/java/com/docus/sw/souyin/SuoyinService.java +++ b/src/main/java/com/docus/sw/souyin/SuoyinService.java @@ -39,7 +39,7 @@ public class SuoyinService { public static void main(String[] args) throws IOException { Config.loadConfig(); - String path = "C:\\jiahsi-saomiao\\0001"; + String path = "C:\\Users\\zhanghai\\Desktop\\桌面\\test\\aa\\南方医院zong"; new SuoyinService().index(path); } @@ -87,15 +87,16 @@ public class SuoyinService { try { PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath()); File[] listFiles = temp.listFiles(); - genPieces(pieces, listFiles); + genPieces(pieces, listFiles,FileTypeEnum.DOC); + pieceList.add(pieces); } catch (IOException e) { } //删除对应的temp 文件 - file.delete(); + FileUtils.delete(temp); }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"); if(!temp.exists()){ @@ -106,13 +107,13 @@ public class SuoyinService { try { PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath()); File[] listFiles = temp.listFiles(); - genPieces(pieces, listFiles); - + genPieces(pieces, listFiles,FileTypeEnum.DOCX); + pieceList.add(pieces); } catch (IOException e) { } //删除对应的temp 文件 - file.delete(); + FileUtils.delete(temp); }else if(piece.getName().endsWith(".pdf")){ Pieces pieces = new Pieces(FileTypeEnum.DOC, piece.getAbsolutePath(), piece.getName()); @@ -121,33 +122,35 @@ public class SuoyinService { temp.mkdirs(); } - GetPicsDocx.getPics(piece.getAbsolutePath(),temp.getAbsolutePath()); try { PdfBoxUtils.pdf2image(piece.getAbsolutePath(),temp.getAbsolutePath()); File[] listFiles = temp.listFiles(); - genPieces(pieces, listFiles); + genPieces(pieces, listFiles,FileTypeEnum.DOC); + pieceList.add(pieces); } catch (IOException e) { } //删除对应的temp 文件 - file.delete(); +// temp.delete(); + FileUtils.delete(temp); }else{ Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName()); File[] listFiles = piece.listFiles(); - genPieces(pieces, listFiles); + genPieces(pieces, listFiles,FileTypeEnum.JPG); pieceList.add(pieces); } } - //填充卷 Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath()); roll.putAll(pieceList); rollList.add(roll); + } + } 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 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); + + 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); + } 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); + pieces.put(documentList); + } + + + } private List indexGen(IndexPlate indexPlate, List rollList, Integer pianPageNum, Integer totalSize) { @@ -369,10 +377,20 @@ public class SuoyinService { //判断是word,pdf,pic if (o.getName().endsWith(".pdf")) { + 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(".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") || o.getName().endsWith(".jpeg") || o.getName().endsWith(".tif") || o.getName().endsWith(".tiff")) { diff --git a/src/main/java/com/docus/sw/word/PdfBoxUtils.java b/src/main/java/com/docus/sw/word/PdfBoxUtils.java index 6df3485..57660fb 100644 --- a/src/main/java/com/docus/sw/word/PdfBoxUtils.java +++ b/src/main/java/com/docus/sw/word/PdfBoxUtils.java @@ -118,7 +118,7 @@ public class PdfBoxUtils { if (pdxObject instanceof PDImageXObject) { PDImageXObject imageXObject = (PDImageXObject) pdxObject; 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; } else if (pdxObject instanceof PDFormXObject) { PDFormXObject form = (PDFormXObject) pdxObject;