2012-11-14 124 views
2

我一直在尝试根据大小将一个大的PDF文件分割为多个PDF文件。我能够分割它,但它只创建一个单独的文件,其余的文件数据丢失。意味着它不会创建多个文件来分割它。任何人都可以帮忙吗?这里是我的代码根据文件大小将一个PDF文件分割为多个

public static void main(String[] args) { 
    try { 
     PdfReader Split_PDF_By_Size = new PdfReader("C:\\Temp_Workspace\\TestZip\\input1.pdf"); 
     Document document = new Document(); 
     PdfCopy copy = new PdfCopy(document, new FileOutputStream("C:\\Temp_Workspace\\TestZip\\File1.pdf")); 
     document.open(); 

     int number_of_pages = Split_PDF_By_Size.getNumberOfPages(); 
     int pagenumber = 1; /* To generate file name dynamically */ 
     // int Find_PDF_Size; /* To get PDF size in bytes */ 
     float combinedsize = 0; /* To convert this to Kilobytes and estimate new PDF size */ 
     for (int i = 1; i < number_of_pages; i++) { 
      float Find_PDF_Size; 
      if (combinedsize == 0 && i != 1) { 
       document = new Document(); 
       pagenumber++; 
       String FileName = "File" + pagenumber + ".pdf"; 
       copy = new PdfCopy(document, new FileOutputStream(FileName)); 
       document.open(); 
      } 

      copy.addPage(copy.getImportedPage(Split_PDF_By_Size, i)); 
      Find_PDF_Size = copy.getCurrentDocumentSize(); 
      combinedsize = (float)Find_PDF_Size/1024; 
      if (combinedsize > 496 || i == number_of_pages) { 
       document.close(); 
       combinedsize = 0; 
      } 
     } 

     System.out.println("PDF Split By Size Completed. Number of Documents Created:" + pagenumber);       
    } 
    catch (Exception i) 
    { 
     System.out.println(i); 
    } 
} 

}

回答

0

(顺便说一句,那将是巨大的,如果你有标记您的问题与iText的了。)

PdfCopy用于关闭PdfReaders其进口来自页面导入源PdfReader切换或PdfCopy关闭的页面。这是由于原来的预期用例创建了一个目标PDF从多个源PDF与许多用户忘记关闭其PdfReaders的事实相结合。

因此,在关闭第一个目标PdfCopy之后,PdfReader也会关闭,并且不会再提取其他页面。

如果我正确解释了最新的签入到iText SVN信息库中,这个隐含的关闭PdfReaders正在从代码中移除。因此,使用下一个iText版本,您的代码可能按预期工作。

相关问题