2017-02-01 128 views
1

我正在使用itextpdf版本5.0.1将页面切割成指定的页码。当我尝试剪切以横向为导向的PDF时遇到问题。当我使用下面的代码时,面向横向的pdf将会像potrait 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

对于你的任务,你真的应该使用'PdfCopy'基于实现(除其它优势外并不需要你控制页面旋转),而不是你的'PdfWriter'的基础一。 – 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

下面的代码解决我的问题

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); 
    } 
}