Java Android iText PDF创建
我正在android上使用iText,这基本上是我的场景: 运行HTTP调用以获取XML数据 解析XML以获取其中的字符串 使用字符串创建PDF 我从XML中提取的字符串是来自web上另一个pdf的物理编码pdf数据 我已经完成了解码过程,所以它是一个解码字符串。。。现在我只需要将它写入某种形式的API,以便正确保存它 我目前正在使用iText,当我尝试添加一个段落时,它会完全按照我的预期操作并写入字符串(包括其标题和作者等标记) 有没有一种方法可以简单地将此字符串写入PDF文档并正确保存 (我删除了前面关于这个问题的一个问题,以便尽可能地重新表述这个问题)。试试这个方法Java Android iText PDF创建,java,android,itext,Java,Android,Itext,我正在android上使用iText,这基本上是我的场景: 运行HTTP调用以获取XML数据 解析XML以获取其中的字符串 使用字符串创建PDF 我从XML中提取的字符串是来自web上另一个pdf的物理编码pdf数据 我已经完成了解码过程,所以它是一个解码字符串。。。现在我只需要将它写入某种形式的API,以便正确保存它 我目前正在使用iText,当我尝试添加一个段落时,它会完全按照我的预期操作并写入字符串(包括其标题和作者等标记) 有没有一种方法可以简单地将此字符串写入PDF文档并正确保存 (我
try {
Document document = new Document();
File root = new File(Environment.getExternalStorageDirectory(),
"foldername");
String sdcardhtmlpath = root.getPath().toString() + "/filename.pdf";
PdfWriter.getInstance(document,
new FileOutputStream(sdcardhtmlpath));
document.open();
Paragraph paragraph = new Paragraph("your string");//add ur string here
paragraph.setAlignment(Element.ALIGN_LEFT);
document.add(paragraph);
document.close();
} catch (Exception e) {
Log.v("PDFcreation", e.toString());
}
这是usinf droidText库编写的简单程序。试试这个,它能用
try{
File obj=new File(Environment.getExternalStorageDirectory(),"pdf");
if(!obj.exists())
{
obj.mkdirs();
}
String name=obj.getAbsolutePath()+"/mypdf.pdf";
FileOutputStream fop=new FileOutputStream(name);
Document doc=new Document();
PdfWriter.getInstance(doc, fop);
doc.open();
Font font=new Font(FontFamily.TIMES_ROMAN, 20, Font.STRIKETHRU, BaseColor.GREEN);
Paragraph p=new Paragraph("sample pdf",font);
p.setAlignment(Element.ALIGN_LEFT);
doc.add(p);
doc.close();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try{
File obj=new File(Environment.getExternalStorageDirectory(),"pdf");
if(!obj.exists())
{
obj.mkdirs();
}
String name=obj.getAbsolutePath()+"/mypdf.pdf";
FileOutputStream fop=new FileOutputStream(name);
Document doc=new Document();
PdfWriter.getInstance(doc, fop);
doc.open();
Font font=new Font(FontFamily.TIMES_ROMAN, 20, Font.STRIKETHRU, BaseColor.GREEN);
Paragraph p=new Paragraph("sample pdf",font);
p.setAlignment(Element.ALIGN_LEFT);
doc.add(p);
doc.close();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}