2017-02-01 4 views
1

私はitextpdfバージョン5.0.1を使用して指定したページ番号にページをカットしています。横向きのPDFをカットしようとすると問題が発生します。私が次のコードを使用すると、横長のPDFは鉢植え指向のPDFのようにカットされ、残りは欠落しています。私が使用していたコードは次のとおりです。iText PDFオリエンテーション

import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import com.itextpdf.text.Document; 
    import com.itextpdf.text.pdf.PdfContentByte; 
    import com.itextpdf.text.pdf.PdfImportedPage; 
    import com.itextpdf.text.pdf.PdfReader; 
    import com.itextpdf.text.pdf.PdfWriter; 
    public class PDFSplitExample { 
    static void splitPdfFile(InputStream inputPdf, 
       OutputStream outputStream, int startPage, 
       int endPage) throws Exception{ 
      //Create document and pdfReader objects. 
      Document document = new Document(); 
      PdfReader pdfReader = new PdfReader(inputPdf); 
      //Get total no. of pages in the pdf file. 
    int totalPages = pdfReader.getNumberOfPages(); 

    //Check the startPage should not be greater than the endPage 
    //and endPage should not be greater than total no. of pages. 
    if(startPage > endPage || endPage > totalPages) { 
     System.out.println("Kindly pass the valid values " + 
      "for startPage and endPage."); 
    }else{ 
     // Create writer for the outputStream 
     PdfWriter writer = 
      PdfWriter.getInstance(document, outputStream); 

     //Open document 
     document.open(); 

     //Contain the pdf data. 
     PdfContentByte pdfContentByte = 
       writer.getDirectContent(); 
     PdfImportedPage page; 

     while(startPage <= endPage) { 
      document.newPage(); 
      page=writer.getImportedPage(pdfReader, startPage); 
      pdfContentByte.addTemplate(page, 0, 0); 
      startPage++; 
     } 

     //Close document and outputStream. 
     outputStream.flush(); 
     document.close(); 
     outputStream.close(); 
    }   
} 

public static void main(String args[]){ 
try {   
    //Prepare output stream for 
    //new pdf file after split process. 
     OutputStream outputStream1 = 
       new FileOutputStream("SplitFile1.pdf"); 
     OutputStream outputStream2 = 
       new FileOutputStream("SplitFile2.pdf"); 

     //call method to split pdf file. 
     splitPdfFile(new FileInputStream("TestFile.pdf"), 
       outputStream1, 1, 10);  
     splitPdfFile(new FileInputStream("TestFile.pdf"), 
       outputStream2, 11, 20); 

     System.out.println("Pdf file splitted successfully."); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
} 
} 
+0

私の問題を解決しました1。 – mkl

+0

私はあなたが言うことを得ることができません。それをもっと丁寧に説明していただけますか? –

答えて

1

あなたの参照「itextpdfバージョン5.0.1」 - そのバージョンが非常に古いであると私は年齢のためにそれを働いていません。私はiText(5.5.11開発ブランチ)の現在の5.xバージョンに対して以下のコードを書いた。それは、古いバージョンでも変更が最小限に抑えられていても動作します。

効果的には、splitPdfFileメソッドは、指定されたドキュメントの一部を抽出します。あなたのアプローチ(ページコンテンツを新しいPDFにコピーする)の代わりに、既存のドキュメントを単にそれ自身のサブセットに制限する方がはるかに簡単です。さらに、このアプローチは、ページコンテンツからではなくプロパティを別々に処理する必要がないという利点を有する。ページの回転。

コードのこのビットは、例えば、

try ( InputStream resource = [...]; 
     OutputStream result = [...]) 
{ 
    PdfReader pdfReader = new PdfReader(resource); 
    pdfReader.selectPages("2-4"); 
    new PdfStamper(pdfReader, result).close(); 
} 

SubDocument.java試験方法testExtractSubDocument

は、入力された文書からの結果文書に、ページ2を書き込む3、及び4。


サイドノート:それほど単純ではないユースケースについても。アプローチを使用して複数のソースPDFからページを収集することは、最適ではありません。 PdfWriterの代わりに、Pdf*Copy*クラスのクラスを使用して、ページのローテーションを正しくコピーする必要があります。

0

次のコードは、あなたが本当に代わりに `PdfWriter`ベースの(他の利点の中のページの回転を制御する必要はありません)` PdfCopy`ベースの実装を使用する必要があり、あなたの仕事のために

private static void manipulateWithCopy(PdfReader reader,String result) 
     throws IOException, DocumentException { 
    int n = reader.getNumberOfPages(); 
    Document document = new Document(); 
    PdfCopy copy = new PdfCopy(document, new FileOutputStream(result)); 
    document.open(); 
    for (int i = 0; i < n;) { 
     copy.addPage(copy.getImportedPage(reader, ++i)); 
    } 
    document.close(); 
} 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    try{ 
    String path= "path/of/the/file/in/the/smb/storage"; 
     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "username", "password");//storing authentication details 
     SmbFile sFile = new SmbFile(path, auth); 
     FileOutputStream fileOutputStream; 
     InputStream fileInputStream; 
     String destFilename = "path/to/the/file"; 
     fileOutputStream = new FileOutputStream(destFilename); 
     fileInputStream = sFile.getInputStream(); 
     byte[] buf; 
     int len; 
     buf = new byte[16 * 1024 * 1024]; 
     while ((len = fileInputStream.read(buf)) > 0) { 
      fileOutputStream.write(buf, 0, len); 
     } 
     InputStream inputPdf = new FileInputStream(destFilename); 
     PdfReader reader= new PdfReader(inputPdf); 
     reader.selectPages(stpg+"-"+endpg); 
     manipulateWithCopy(reader,destFilename); 
     reader.close(); 
    } 
    catch(Exception e){ 
     System.out.println(e); 
    } 
}