2013-05-03 112 views
-1

我想通過使用iText API將兩個或多個PDF文檔合併成一個PDF文件合併。但在結果我得到合併pdf與0字節size.I發佈我的代碼如下所示。我也嘗試了iText.jar文件,但給出相同的0尺寸的PDF。Android:使用iText API合併PDF文件不工作

,並得到這樣的: - 「找不到類 'com.itextpdf.text.pdf.PdfPrinterGraphics2D',從法com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes引用」。 我仍然沒有取得任何成功。

代碼:

public class ItextMerge { 
     public static void main() { 
      List<InputStream> list = new ArrayList<InputStream>(); 
      try { 
       // Source pdfs 
       list.add(new FileInputStream(new File("mnt/sdcard/nocturia.pdf"))); 
       list.add(new FileInputStream(new File("mnt/sdcard/Professional Android Application Development.pdf"))); 

       // Resulting pdf 
       OutputStream out = new FileOutputStream(new File("mnt/sdcard/newmerge.pdf")); 

       doMerge(list, out); 

      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (DocumentException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     /** 
     * Merge multiple pdf into one pdf 
     * 
     * @param list 
     *   of pdf input stream 
     * @param outputStream 
     *   output file output stream 
     * @throws DocumentException 
     * @throws IOException 
     */ 
     public static void doMerge(List<InputStream> list, OutputStream outputStream) 
       throws DocumentException, IOException { 
      Document document = new Document(); 
      PdfWriter writer = PdfWriter.getInstance(document, outputStream); 
      document.open(); 
      PdfContentByte cb = writer.getDirectContent(); 

      for (InputStream in : list) { 
       PdfReader reader = new PdfReader(in); 
       for (int i = 1; i <= reader.getNumberOfPages(); i++) { 
        document.newPage(); 
        //import the page from source pdf 
        PdfImportedPage page = writer.getImportedPage(reader, i); 
        //add the page to the destination pdf 
    //    cb.addTemplate(page, 0, 0); 
    //    cb.addTemplate(page, 0, 0); 
       } 
      } 

      outputStream.flush(); 
      document.close(); 
      outputStream.close(); 
     } 
    } 

任何想法?

謝謝

回答

2

請使用iText的的Android的端口:

http://repo.itextsupport.com//android_gae/com/itextpdf/itextgoogle/

你需要一個試用許可證與iText的Android上工作; http://demo.itextsupport.com/newslicense/

+0

當我useed itextgoogle-5.4.0.jar它給我的錯誤 「產生的原因:java.lang.NoClassDefFoundError:org.spongycastle.crypto.engines.AESFastEngine」 – kyogs 2013-05-03 07:55:35

+0

你在你的應用程序需要spongycastle:HTTP: //rtyley.github.io/spongycastle/ – 2013-05-03 08:12:06

+0

得到此錯誤com.itextpdf.license.LicenseKeyException:未加載許可證文件。並生成新的pdf文件與0size – kyogs 2013-05-03 09:19:01

3

我提高了邁克爾的回答,因爲這是你的問題的正確答案。

但是,讀取您的代碼,您還有另一個問題,您不知道:您使用錯誤的代碼來合併PDF。您應該使用PdfCopyPdfSmartCopy,而不是PdfWriter

此之前已經解釋了很多次:

您使用PdfWriter事實表明您沒看過the documentation

此外,你的問題聽起來好像你不知道Lowagie是一個人的名字。其實,這是我的名字,當有人說Lowagie iText API不工作時,它非常尷尬。對於初學者來說,因爲我一直在問iText的stop using those old versions,但也因爲它聽起來像是個人指責,使產品與人類混淆。請參閱What is the difference between lowagie and iText?

+0

得到此errorcom.itextpdf.license.LicenseKeyException:許可證文件未加載。並生成新的PDF文件與0size – kyogs 2013-05-03 09:19:19

+0

我從問題中刪除了你的名字,並取代PdfSmartCopy,但仍沒有得到任何成功。 – kyogs 2013-05-03 09:20:06

+0

不要責怪PdfSmartCopy上的錯誤。例外情況告訴您需要獲取許可證文件:http://demo.itextsupport.com/newslicense/,您需要將其加載到您的代碼中:LicenseKey.loadLicenseFile(path_to_licensekey);其中path_to_licensekey是您存儲許可證密鑰的路徑。 – 2013-05-03 09:34:06

1

下面是合併兩個pdf文件的簡單代碼。

try{ 
     String doc1 = FOLDER_PATH + "Doc1.pdf"; 
     String doc2 = FOLDER_PATH + "Doc2.pdf"; 
     String resultDocFile = FOLDER_PATH + "ResultDoc.pdf"; 

     PdfReader reader1 = new PdfReader(doc1); 
     Document resultDoc = new Document(); 
     PdfCopy copy = new PdfCopy(resultDoc, new FileOutputStream(resultDocFile)); 
     resultDoc.open(); 
     //Copying First Document 
     for(int i = 1; i <= reader1.getNumberOfPages(); i++) { 
      copy.addPage(copy.getImportedPage(reader1, i)); 
     } 
     PdfReader reader2 = new PdfReader(doc2); 
     //Copying Second Document 
     for(int i = 1; i <= reader2.getNumberOfPages(); i++) { 
      copy.addPage(copy.getImportedPage(reader2, i)); 
     } 
     resultDoc.close(); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    }