英文:
How to add content in existing PDF page using PdfWriter and PdfCopy in one loop
问题
以下是翻译好的部分:
第一个方法:
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();
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");
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;
}
请注意,代码中的变量和方法调用都未被翻译。如果您需要在特定上下文中使用这些代码,请确保在相应的环境中设置适当的变量和方法。
英文:
I have to use two methods to write content in existing PDF page using PdfWriter and to merge PDFs using PdfCopy. I want to do it in one loop.
PdfWriter object is not merging PDF like source page. We have to create a new page (A4) and add content into it from source page. To avoid that, we can use Pdfcopy object which merges exact size of PDF pages as source file has. The problem here is I have to merge first and then call the merged file to add content again. Since the PDF file has lot of pages, it's taking lot of time to perform this operation using two loops.
I can't use PdfCopy option in same loop.
Here aremy two methods:
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;
}
And another method is:
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;
}
How to write content while looping and merge at same time?
专注分享java语言的经验与见解,让所有开发者获益!
评论