2015-04-02 58 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