首页 > 解决方案 > 如何在一个循环中使用 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;
    
}

如何在循环和合并的同时编写内容?

标签: javaitext

解决方案


推荐阅读