修改国标病历导出图片比例问题

master
linjj 2 years ago
parent a02feee98c
commit 3b539ddd2b

@ -50,9 +50,9 @@ public class BatchExportServiceImpl implements BatchExportService {
@Override
public String batchExportPdf(int taskId) throws Exception {
//是否存在目录不存在创建
File file = new File (exportPdfPath);
if(!file.isDirectory ()){
file.mkdirs ();
File file = new File(exportPdfPath);
if (!file.isDirectory()) {
file.mkdirs();
}
//导出文件名
String fileName = null;
@ -74,7 +74,7 @@ public class BatchExportServiceImpl implements BatchExportService {
} catch (ParseException e) {
throw new RuntimeException(e);
}
Boolean aBoolean = imageToPdf(filePathList, fileName,exportPdfPath);
Boolean aBoolean = imageToPdf(filePathList, fileName, exportPdfPath);
//为true时修改任务状态1完成2失败
if (aBoolean) {
exportTaskDetailsMapper.upStatc(1, list.getId());
@ -88,83 +88,96 @@ public class BatchExportServiceImpl implements BatchExportService {
}
public static Boolean imageToPdf(List<ExportDetailsVo> scanPathVos, String fileName,String exportPdfPath) {
Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfWriter writer;
public static Boolean imageToPdf(List<ExportDetailsVo> scanPathVos, String fileName, String exportPdfPath) {
ByteArrayOutputStream bos = null;
FileOutputStream fileOutputStream = null;
PdfWriter writer = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(exportPdfPath+fileName+".pdf");
writer = PdfWriter.getInstance(document,fileOutputStream);//创建编写器PDF类型
imgToPdf(document, writer, scanPathVos);
bos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器
fileOutputStream = new FileOutputStream(exportPdfPath + fileName + ".pdf");
writer = PdfWriter.getInstance(document, fileOutputStream);//创建编写器PDF类型
//上一个目录名称
String lastOutline = null;
//是否增加标签
boolean outFlag = true;
//标签顺序
Integer outNum = 1;
// 添加目录
document.open(); //打开容器
float w, h;
PdfContentByte cb = writer.getDirectContent();
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
cb.beginText();
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "目录", 300, 780, 0);
cb.endText();
// 创建目录
PdfOutline root = cb.getRootOutline();
// 添加图片到PDF
for (int i = 0; i < scanPathVos.size(); i++) {
String assortName = scanPathVos.get(i).getAssortName();
if (StringUtils.isNotBlank(lastOutline) && lastOutline.equals(assortName)) {
outFlag = false;
}
if (StringUtils.isBlank(lastOutline)) {
lastOutline = assortName;
outFlag = true;
}
if (!lastOutline.equals(assortName)) {
lastOutline = assortName;
outFlag = true;
}
String imagePath = scanPathVos.get(i).getFilePath();
Image image = Image.getInstance(imagePath);
/*处理图片缩放比例*/
w = image.getWidth();
h = image.getHeight();
image.setAlignment(com.lowagie.text.Image.ALIGN_CENTER);
image.scaleAbsolute(PageSize.A4.getWidth(), PageSize.A4.getWidth() * (h / w));
// 创建新页面并添加图片
if (i != 0) {
document.newPage();
}
document.add(image);
if (outFlag) {
//目录跳转页面内容设置。
PdfAction action = PdfAction.gotoLocalPage(i + 1, new PdfDestination(PdfDestination.FIT), writer);
//标题目录
String title = outNum + "." + scanPathVos.get(i).getAssortName();
new PdfOutline(root, action, title, false);
outNum++;
}
}
document.close(); //关闭容器
} catch (Exception e) {
ExceptionPrintUtil.printException(e);
e.printStackTrace();
return false;
} finally {
try {
bos.flush();
bos.close();
if (writer != null) {
writer.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (bos != null) {
bos.flush();
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
document.close();
}
return true;
}
public static void imgToPdf(Document document, PdfWriter writer, List<ExportDetailsVo> scanPathVos) throws Exception {
//上一个目录名称
String lastOutline = null;
//是否增加标签
boolean outFlag = true;
//标签顺序
Integer outNum = 1;
// 添加目录
document.open(); //打开容器
PdfContentByte cb = writer.getDirectContent();
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
cb.beginText();
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "目录", 300, 780, 0);
cb.endText();
// 创建目录
PdfOutline root = cb.getRootOutline();
// 添加图片到PDF
for (int i = 0; i < scanPathVos.size(); i++) {
String assortName = scanPathVos.get(i).getAssortName();
if (StringUtils.isNotBlank(lastOutline) && lastOutline.equals(assortName)) {
outFlag = false;
}
if (StringUtils.isBlank(lastOutline)) {
lastOutline = assortName;
outFlag=true;
}
if (!lastOutline.equals(assortName)){
lastOutline = assortName;
outFlag=true;
}
String imagePath = scanPathVos.get(i).getFilePath();
Image image = Image.getInstance(imagePath);
image.setAlignment(Image.MIDDLE);
image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
// 等比例缩放图片以填满PDF页面
image.scaleAbsolute(document.getPageSize());
// 创建新页面并添加图片
//document.newPage();
document.add(image);
if (outFlag) {
//目录跳转页面内容设置。
PdfAction action = PdfAction.gotoLocalPage(i+1, new PdfDestination(PdfDestination.FIT), writer);
//标题目录
String title = outNum + "." + scanPathVos.get(i).getAssortName();
new PdfOutline(root, action, title, false);
outNum++;
}
}
document.close(); //关闭容器
}

Loading…
Cancel
Save