2013-03-09 74 views
0

該代碼在Java應用程序中運行良好。但是當我轉移到Android時我無法編譯。錯誤信息:「該類型java.awt.geom.AffineTransform中不能得到解決它間接地從需要的.class文件中引用」在該行:iText在Android中合併pdf錯誤

cb.addTemplate(page, 0, 0);//compile error at this line 

然後我試圖註釋掉上面的行...但發生了什麼事情,這次合併的PDF文件是在Android中創建的,但內部沒有內容......只有空白頁。

這是使用的代碼。任何幫助表示讚賞。(嘗試也同時使用droidText並單獨正常iText的罐子...仍然沒有運氣)

public void concatPDFs() { 

      Document document = new Document(); 
      try { 
       uploadedFile.setVisibility(View.VISIBLE); 
       File sdCard = Environment.getExternalStorageDirectory(); 
       uploadedFile.setText(sdCard.getAbsolutePath()); 
       List<InputStream> pdfs = new ArrayList<InputStream>(); 
       pdfs.add(new FileInputStream("/storage/extSdCard/1.pdf")); 
       pdfs.add(new FileInputStream("/storage/extSdCard/2.pdf")); 
       outputStream = new FileOutputStream("/storage/extSdCard/merge.pdf"); 
       uploadedFile.setText("Added Files"); 
       List<PdfReader> readers = new ArrayList<PdfReader>(); 
       int totalPages = 0; 
       Iterator<InputStream> iteratorPDFs = pdfs.iterator(); 

       // Create Readers for the pdfs. 
       while (iteratorPDFs.hasNext()) { 
        InputStream pdf = iteratorPDFs.next(); 
        PdfReader pdfReader = new PdfReader(pdf); 
        readers.add(pdfReader); 
        totalPages += pdfReader.getNumberOfPages(); 
       } 
       // Create a writer for the outputstream 
       PdfWriter writer = PdfWriter.getInstance(document, outputStream); 
       uploadedFile.setText("PdfWriter"); 
       document.open(); 
       BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
         BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
       PdfContentByte cb = writer.getDirectContent(); // Holds the PDF 
       // data 

       PdfImportedPage page; 
       int currentPageNumber = 0; 
       int pageOfCurrentReaderPDF = 0; 
       Iterator<PdfReader> iteratorPDFReader = readers.iterator(); 

       // Loop through the PDF files and add to the output. 
       while (iteratorPDFReader.hasNext()) { 
        PdfReader pdfReader = iteratorPDFReader.next(); 

        // Create a new page in the target for each source page. 
        while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { 
         document.newPage(); 
         pageOfCurrentReaderPDF++; 
         currentPageNumber++; 
         page = writer.getImportedPage(pdfReader, 
           pageOfCurrentReaderPDF); 
         cb.addTemplate(page, 0, 0); 
         uploadedFile.setText("getImportedPage"); 
         // Code for pagination. 
         if (true) { 
          cb.beginText(); 
          cb.setFontAndSize(bf, 9); 
          cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" 
            + currentPageNumber + " of " + totalPages, 520, 
            5, 0); 
          cb.endText(); 
         } 
        } 
        pageOfCurrentReaderPDF = 0; 
       } 
       outputStream.flush(); 
       document.close(); 
       outputStream.close(); 
       uploadedFile.setText("Done Pdf"); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       if (document.isOpen()) 
        document.close(); 
       try { 
        if (outputStream != null) 
         outputStream.close(); 
       } catch (IOException ioe) { 
        ioe.printStackTrace(); 
       } 
      } 
    } 

回答

2

我upvoted以前的答案,但我想補充一點,user614454使用,而不是官方Android端口的iText的Java版本。官方安卓端口可以在這裏找到:http://itextsupport.com/download/android.html

在這個官方Android端口中,iText所需的所有java.awt類都被替換爲輕量級替代品。

請注意,我是iText的原始作者。我刪除了對DroidText的引用,因爲這是一個不應該再使用的iText版本的Android端口:http://lowagie.com/itext2

+0

感謝您的澄清! – kaderud 2013-03-09 10:47:26

+0

第一個鏈接不工作 – Azahar 2014-04-12 11:29:08

+1

請參閱http://www.itextpdf.com/product/itextg – 2014-04-12 11:32:16