PdfBox:PDFMergerUtility如何重用对象?

时间:2021-09-09 12:01:54
/*PDFMergerUtility mergerUtility = new PDFMergerUtility();*/


 while(rs.next())
    {


        PDFMergerUtility mergerUtility = new PDFMergerUtility();

        try
           {
            in=resultset.getBinaryStream(1);
            mergerUtility.addSource(in);
            mergerUtility.setDestinationStream(bos);
            mergerUtility.mergeDocuments();

           }

    catch (Exception e) 
       { 

       }
        bos.reset();
       /*How to reset/clear mergeutility object?*/
   }

If not instantiating, PDFMergerUtility mergerUtility = new PDFMergerUtility(); inside while loop, gives end of file error.. How to reuse it without declaring in while loop?

如果没有实例化,则PDFMergerUtility mergerUtility = new PDFMergerUtility();在while循环中,给出文件结束错误。如何重用它而不在while循环中声明?

1 个解决方案

#1


0  

In your code, you are not merging. To fix, the code will be like this

在您的代码中,您没有合并。要修复,代码将是这样的

    PDFMergerUtility mergerUtility = new PDFMergerUtility();
    try  {
        while(rs.next())    {      
            in=resultset.getBinaryStream(1);
            mergerUtility.addSource(in);

        }       
        mergerUtility.setDestinationStream(bos);
        mergerUtility.mergeDocuments();

        bos.close();

   }
   catch (Exception e) { 

   }

#1


0  

In your code, you are not merging. To fix, the code will be like this

在您的代码中,您没有合并。要修复,代码将是这样的

    PDFMergerUtility mergerUtility = new PDFMergerUtility();
    try  {
        while(rs.next())    {      
            in=resultset.getBinaryStream(1);
            mergerUtility.addSource(in);

        }       
        mergerUtility.setDestinationStream(bos);
        mergerUtility.mergeDocuments();

        bos.close();

   }
   catch (Exception e) { 

   }