2012-11-14 4 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를로 질문을 태그 한 경우 BTW,이 컸을 것입니다.)

PdfCopy는 가져온 PdfReaders를 닫 사용 페이지 가져 오기의 소스 PdfReader이 전환되거나 PdfCopy이 닫힐 때마다 페이지가 표시됩니다. 이것은 하나의 대상 PDF를 다중 소스 PDF를 많은 사용자가 자신의 PdfReaders을 닫는 것을 잊어 버린 사실과 함께 작성하는 원래 의도 된 유스 케이스 때문이었습니다.

따라서 첫 번째 대상인 PdfCopy을 닫으면 PdfReader도 닫히고 더 이상 페이지가 추출되지 않습니다.

가장 최근의 체크 인을 iText SVN 리포지토리에 올바르게 해석하는 경우이 암시 적 클로우즈 PdfReaders이 코드에서 제거되는 중입니다. 따라서 다음 iText 버전 중 하나를 사용하면 코드가 의도 한대로 작동 할 수 있습니다.

관련 문제