2015-04-02 56 views
0

創建於Android的PDF我已經在Android的日食利用iText

創建一個利用iText PDF文件的問題我不能,如果給予引導項,在我的情況的Android 5.0.1創建PDF。如果我刪除引導條目,我可以創建PDF,但無法啓動活動,因爲Android 5.0.1包含Android jar。你能告訴我如何解決這個問題嗎? 另外,如果我讓它成爲非活動類,我希望能夠在另一個活動類中創建非活動類的對象,以便我可以從活動類調用方法。請告訴我如何實現這一點。

回答

0

從這裏下載iText API http://itextpdf.com/product/itextg並將其添加到您的項目中。

使用這個類,並調用所需的功能創建PDF

public class CreatePDF { 

    private static Font normalFont = new Font(Font.FontFamily.TIMES_ROMAN, 25, 
      Font.NORMAL, BaseColor.BLACK); 

    private static Font Head = new Font(Font.FontFamily.TIMES_ROMAN, 35, 
      Font.BOLD, BaseColor.BLACK); 

    //Path is the path where you want your pdf to get stored 
    public void createPDFDoc(ArrayList<notesWrapper> notesList,String path) { 
     // TODO Auto-generated method stub 
     Document document = new Document(); 
     try { 
      PdfWriter.getInstance(document, new FileOutputStream(path)); 
      document.open(); 

      for(int i=0;i<notesList.size();i++) 
      {    


       addContentHead(document,"Image "+(i+1));     
       addContent(document,notesList.get(i).message); 
       if(i<notesList.size()) 
       { 
        document.newPage(); 
       } 
      } 

      document.close(); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    private void addContent(Document document,String content) throws DocumentException { 


     Paragraph preface = new Paragraph();    
     addEmptyLine(preface, 1); 

     if(!content.equalsIgnoreCase("insert note")) 
     preface.add(new Paragraph(content, normalFont)); 

     else 
      addEmptyLine(new Paragraph(), 1); 

     addEmptyLine(preface, 3); 
     document.add(preface); 



    } 


    private void addContentHead(Document document,String content) throws DocumentException { 


     Paragraph preface = new Paragraph();    
     addEmptyLine(preface, 1); 

     preface.add(new Paragraph(content, Head)); 
     addEmptyLine(preface, 3); 
     document.add(preface); 



    } 



    private static void addEmptyLine(Paragraph paragraph, int number) { 
     for (int i = 0; i < number; i++) { 
      paragraph.add(new Paragraph(" ")); 
     } 
    } 


} 
+0

通過iText API的應用做ü意味着jar文件?我已經有了jar文件,並且已經添加到構建路徑。我希望能夠通過活動課來到這個班。請告訴我 – Sam 2015-04-02 11:46:29

+0

是的,當然是jar文件 – Ajeet 2015-04-02 11:48:27

+0

請告訴我如何從一個活動課來到這個班,例如。 – Sam 2015-04-02 11:55:06