开发者可以用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);
}
}