2013-05-03 111 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(); 
    }