我已在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();
}
}
}