支持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 {
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<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);
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<IndexPageRow> indexGen(IndexPlate indexPlate, List<Roll> 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")) {

@ -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;

Loading…
Cancel
Save