无法在Android上编写pdf

时间:2013-11-26 09:42:02

标签: android pdf itext

我使用以下代码创建pdf文件,但是在执行代码文件后没有在设备的物理位置创建。

tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/";     
ContextWrapper cw = new ContextWrapper(getApplicationContext());   
File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE); 
prepareDirectory();      
String uniqueId = getTodaysDate() + "_" + getCurrentTime() + "_" + Math.random();    

String current = uniqueId + ".pdf";       
File  mypath= new File(directory,current);    
System.out.println(".....................1");
try {
    mypath.createNewFile();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
Document document = new Document();  
System.out.println(".....................2");
try {
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(mypath));
    System.out.println(".....................3");

    document.open();

    System.out.println(".....................4");
    Paragraph preface = new Paragraph();
    preface.add(new Paragraph("First Paragraph"));
    document.add(preface);
    System.out.println(".....................5");


    document.add(chap);
    document.close();
    System.out.println(".....................6");

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    System.out.println(".................inside filenotfound exception");
    e.printStackTrace();
} catch (DocumentException e) {
    // TODO Auto-generated catch block
    System.out.println(".................inside document exception");
    e.printStackTrace();
}  

1 个答案:

答案 0 :(得分:3)

PdfPrinterGraphics2D类是iText(适用于Java)的一部分,但不属于iTextG(适用于Android / GAE)。如果你得到找不到类异常,那么你使用的是错误的iText。您应该使用Android端口:http://itextpdf.com/product/itextg