如何在现有PDF页面中使用PdfWriter和PdfCopy在一个循环中添加内容

huangapple 未分类评论46阅读模式
英文:

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&lt;InputStream&gt; list, OutputStream outputStream,List&lt;Integer&gt; 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 = &quot;&quot;;
    	ResultSet doc = sql.getDocumentDetails(docId);
    	if(doc.next()) {
	    	String relative_path = doc.getString(&quot;RELATIVE_PATH&quot;); 
	    	
	    	if(relative_path.endsWith(&quot;.pdf&quot;) || relative_path.endsWith(&quot;.PDF&quot;)){
	    		
	    		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(&quot;document merged&quot;);
	return true;
	
}

And another method is:

public boolean mergePDFsinOneFileTe(List&lt;InputStream&gt; list, OutputStream outputStream,List&lt;Integer&gt; 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 = &quot;&quot;;
	String docName = &quot;&quot;;
	if(conDocResult.next())
		docName = conDocResult.getString(&quot;DOCUMENT_NAME&quot;);
	
	for(int docId : allDocId) {
		String finalPath = &quot;&quot;;
		printText = &quot;&quot;;
    	ResultSet doc = sql.getDocumentDetails(docId);
    	if(doc.next()) {
	    	String relative_path = doc.getString(&quot;RELATIVE_PATH&quot;); 
	    	
	    	if(relative_path.endsWith(&quot;.pdf&quot;) || relative_path.endsWith(&quot;.PDF&quot;)){
	    		String docTypePrefix = &quot;&quot;;
	    		int typeValue = 0;
	    		
	    		finalPath = storagePath +relative_path;
	    		PdfReader reader = new PdfReader(finalPath);
	    		System.out.println(&quot;finalPath &quot;+finalPath);
	    		
	    		ResultSet docTypeResult = sql.getDoctypeDetails(doc.getInt(&quot;DOCUMENT_TYPE_ID&quot;));
	    		if(docTypeResult.next())
	    			docTypePrefix = docTypeResult.getString(&quot;CONSO_SUFFIX_NAME&quot;); //PREFIX
	    		
				ResultSet consolValue = sql.getConsolidateTypeValueByTypeId(doc.getInt(&quot;DOCUMENT_TYPE_ID&quot;));
				if(consolValue.next())
					typeValue = Integer.parseInt(consolValue.getString(&quot;VALUE&quot;));
				
	    		printText = docName + &quot;-&quot; + &quot;[&quot; + typeValue + &quot;]&quot; + &quot;-&quot; + docTypePrefix;
				for(int i = 1; i &lt;= 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(&quot;document merged&quot;);
	return true;
	
}

How to write content while looping and merge at same time?

huangapple
  • 本文由 发表于 2020年4月5日 00:07:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/61030895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定