利用ITEXT操作PDF

iTextJava中用于创建和操作PDF文件的开源

开发者可以用iText来:

  • 从XML文件或数据库来动态生成PDF文档
  • 为浏览器生成PDF文档
  • 利用PDF格式的许多互动功能
  • 添加书签、页码、水印、条形码等
  • 分区、拼接和处理PDF页面
  • 自动填写PDF表单
  • 给PDF文件添加数字签名

通常情况下,在具有下列情况之一的项目中会使用iText:

  • 内容不是提前准备好的:它基于用户输入或数据库的实时信息来计算、处理。
  • 内容太多,PDF文件无法手动生成。
  • 在批处理过程中,文档需要在无人值守模式下创建。
  • 需要对内容进行定制或个性化。例如,最终用户的名字需要被印在多个页面上。
/**
* 图片转换成pdf
* @param imgFile
* @param pdfFile
* @throws Exception
*/
public void image2pdf(File imgFile, File pdfFile) throws Exception {
Document document = new Document(PageSize.A4);
try {
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
Image img = Image.getInstance(imgFile.toURI().toURL());
float imgWidth = img.getWidth();
float imgHeight = img.getHeight();
if (imgWidth > imgHeight) {
img.setRotationDegrees(270f);//旋转图片
}
img.scaleToFit(560f, 840f);
img.setAbsolutePosition(15f, 20f);
document.add(img);
} catch (DocumentException | IOException e) {
throw e;
} finally {
document.close();
FileUtils.deleteQuietly(imgFile);
}
}

/**
* 根据新文件合并pdf
* @param mergedPdf 老文件
* @param attchpdf 新文件
* @throws DocumentException
* @throws IOException
*/
public void mergePdf(File mergedPdf, File attchpdf)
throws DocumentException, IOException {
SecureRandom srand = new SecureRandom();
PdfReader reader = null;
String tmpName = String.format("pre%s.tmp.pdf",
Math.abs(srand.nextLong()));
File preMergFile = new File(mergedPdf.getParentFile(), tmpName);
if (mergedPdf.exists()) {
FileUtils.copyFile(mergedPdf, preMergFile);
}
Document document = new Document(PageSize.A4);
try {
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(
mergedPdf));
document.open();
if (preMergFile.exists()) {
reader = new PdfReader(preMergFile.getAbsolutePath());
copy.addDocument(reader);
reader.close();
}
logger.debug("attchpdf.getAbsolutePath()={}",
attchpdf.getAbsolutePath());
reader = new PdfReader(attchpdf.getAbsolutePath());
copy.addDocument(reader);
} catch (DocumentException | IOException e) {
throw e;
} finally {
if (reader != null) {
reader.close();
}
document.close();
FileUtils.deleteQuietly(preMergFile);
}
}

/**
* 根据url合并pdf

* @param mergedPdf
* @param url
* @throws DocumentException
* @throws IOException
*/
public void mergePdfFromURL(File mergedPdf, String url)
throws DocumentException, IOException {
SecureRandom srand = new SecureRandom();
PdfReader reader = null;
String tmpName = String.format("pre%s.tmp.pdf",
Math.abs(srand.nextLong()));
File preMergFile = new File(mergedPdf.getParentFile(), tmpName);
if (mergedPdf.exists()) {
// FileUtils.moveFile(mergedPdf, preMergFile);
FileUtils.copyFile(mergedPdf, preMergFile);
}
Document document = new Document(PageSize.A4);
try {
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(
mergedPdf));
document.open();
if (preMergFile.exists()) {
reader = new PdfReader(preMergFile.getAbsolutePath());
copy.addDocument(reader);
reader.close();
}
reader = new PdfReader(url);
copy.addDocument(reader);
} catch (DocumentException | IOException e) {
throw e;
} finally {
if (reader != null) {
reader.close();
}
document.close();
FileUtils.deleteQuietly(preMergFile);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值