Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ConvertPicUtil.java #385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions server/src/main/java/cn/keking/utils/ConvertPicUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@ public static void convertJpg2Pdf(String strJpgFile, String strPdfFile) {
* @return File
*/
public static File convertTif2Pdf(String strTifFile, String strPdfFile) {
RandomAccessFileOrArray rafa = null;
Document document = null;
try {
RandomAccessFileOrArray rafa = new RandomAccessFileOrArray(strTifFile);
rafa = new RandomAccessFileOrArray(strTifFile);

Document document = new Document();
document = new Document();
// 设置文档页边距
document.setMargins(0, 0, 0, 0);

Expand All @@ -192,40 +194,42 @@ public static File convertTif2Pdf(String strTifFile, String strPdfFile) {
Image image;
File filePDF;

if (intPages == 1) {
String strJpg = strTifFile.substring(0, strTifFile.lastIndexOf(".")) + ".jpg";
File fileJpg = new File(strJpg);
List<String> listPic2Jpg = convertTif2Jpg(strTifFile, strJpg);

if (listPic2Jpg != null && fileJpg.exists()) {
convertJpg2Pdf(strJpg, strPdfFile);
}

} else {
for (int i = 1; i <= intPages; i++) {
for (int i = 1; i <= intPages; i++) {
try {
image = TiffImage.getTiffImage(rafa, i);
// 设置页面宽高与图片一致
Rectangle pageSize = new Rectangle(image.getScaledWidth(), image.getScaledHeight());
document.setPageSize(pageSize);
// 图片居中
image.setAlignment(Image.ALIGN_CENTER);
//新建一页添加图片
document.newPage();
document.add(image);
} catch (Throwable e) { // 部分tif文件intPages会大于1,但实际只有1页,这里做下兼容。
logger.error(e.getMessage(), e);
continue;
}

document.close();
// 设置页面宽高与图片一致
Rectangle pageSize = new Rectangle(image.getScaledWidth(), image.getScaledHeight());
document.setPageSize(pageSize);
// 图片居中
image.setAlignment(Image.ALIGN_CENTER);
//新建一页添加图片
document.newPage();
document.add(image);
}

rafa.close();

filePDF = new File(strPdfFile);

return filePDF;
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (document != null) {
document.close();
}
if (rafa != null) {
try {
rafa.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return null;
}

}
}