错误itext不会生成pdf文件

时间:2015-12-02 07:51:52

标签: java android pdf itext

我已在itextpdf-5.1.0.jar上下载了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>并已启用AndroidManifest.xml,我刚开始itext并尝试了一些教程,但由于某种原因我无法生成一个pdf。

有人知道这段代码有什么问题吗?

import android.os.Environment;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;


public class MainActivity{

public static void main(String[] args) {

    Document document = new Document();

    try {
        PdfWriter.getInstance(document,new FileOutputStream(Environment.getExternalStorageDirectory() + "/Paragraph.pdf"));

        document.open();
        Paragraph paragraph = new Paragraph();

        for(int i=0; i<10; i++){
            Chunk chunk = new Chunk(
                    "This is a sentence which is long " + i + ". ");
            paragraph.add(chunk);
        }

        document.add(paragraph);
        document.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
}

0 个答案:

没有答案