java - 如何在一个循环中使用 PdfWriter 和 PdfCopy 在现有 PDF 页面中添加内容
问题描述
我必须使用两种方法使用 PdfWriter 在现有 PDF 页面中写入内容,并使用 PdfCopy 合并 PDF。我想在一个循环中完成。
PdfWriter 对象没有像源页面那样合并 PDF。我们必须创建一个新页面 (A4) 并从源页面向其中添加内容。为了避免这种情况,我们可以使用 Pdfcopy 对象,它将 PDF 页面的确切大小合并为源文件。这里的问题是我必须先合并,然后再调用合并的文件添加内容。由于 PDF 文件有很多页面,因此使用两个循环执行此操作需要花费大量时间。
我不能在同一个循环中使用 PdfCopy 选项。
这是我的两种方法:
public boolean mergePDFsinOneFile(List<InputStream> list, OutputStream outputStream,List<Integer> allDocId,int conDocId){
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
String storagePath = sql.getDirectoryPath();
PdfReader reader = null;
for(int docId : allDocId) {
String finalPath = "";
ResultSet doc = sql.getDocumentDetails(docId);
if(doc.next()) {
String relative_path = doc.getString("RELATIVE_PATH");
if(relative_path.endsWith(".pdf") || relative_path.endsWith(".PDF")){
finalPath = storagePath +relative_path;
reader = new PdfReader(finalPath);
copy.addDocument(reader);
copy.freeReader(reader);
}
}
}
reader.close();
outputStream.flush();
document.close();
outputStream.close();
System.out.println("document merged");
return true;
}
另一种方法是:
public boolean mergePDFsinOneFileTe(List<InputStream> list, OutputStream outputStream,List<Integer> allDocId,int conDocId){
com.itextpdf.text.Document document = new com.itextpdf.text.Document();//PageSize.A4
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
BaseFont bf = BaseFont.createFont();
cb.setFontAndSize(bf, 8);
String storagePath = sql.getDirectoryPath();
ResultSet conDocResult = sql.getDocumentDetails(conDocId);
String printText = "";
String docName = "";
if(conDocResult.next())
docName = conDocResult.getString("DOCUMENT_NAME");
for(int docId : allDocId) {
String finalPath = "";
printText = "";
ResultSet doc = sql.getDocumentDetails(docId);
if(doc.next()) {
String relative_path = doc.getString("RELATIVE_PATH");
if(relative_path.endsWith(".pdf") || relative_path.endsWith(".PDF")){
String docTypePrefix = "";
int typeValue = 0;
finalPath = storagePath +relative_path;
PdfReader reader = new PdfReader(finalPath);
System.out.println("finalPath "+finalPath);
ResultSet docTypeResult = sql.getDoctypeDetails(doc.getInt("DOCUMENT_TYPE_ID"));
if(docTypeResult.next())
docTypePrefix = docTypeResult.getString("CONSO_SUFFIX_NAME"); //PREFIX
ResultSet consolValue = sql.getConsolidateTypeValueByTypeId(doc.getInt("DOCUMENT_TYPE_ID"));
if(consolValue.next())
typeValue = Integer.parseInt(consolValue.getString("VALUE"));
printText = docName + "-" + "[" + typeValue + "]" + "-" + docTypePrefix;
for(int i = 1; i <= reader.getNumberOfPages();i++){
PdfImportedPage page = writer.getImportedPage(reader, i);
cb.addTemplate(page, 0, 0);
cb.beginText();
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(printText), document.left(), document.bottom() - 20, 0);
cb.endText();
document.newPage();
}
}
}
}
outputStream.flush();
document.close();
outputStream.close();
System.out.println("document merged");
return true;
}
如何在循环和合并的同时编写内容?
解决方案
推荐阅读
- statistics - 哪个模型应该用于(事件百分比,时间)预测。分布不是线性的
- c - 循环线程中的 pthread_barrier_t
- html - 从 git 克隆后,我的 web 界面发生了一些变化
- python - 等待使用 Python (iframe) 在 Selenium 中可点击
- java - 正则表达式更改电话号码的形式
- javascript - 从另一个组件重新渲染组件的一部分
- c# - 困惑:抽象类属性
- google-chrome - 流行浏览器中的 Webcryto getRandomValues() 实现
- javascript - 当用户单击提交时,我的 AJAX 没有发布或响应
- android - 如何一键暂停/播放多个 MediaPlayer 对象?