【发布时间】:2017-04-04 18:30:40
【问题描述】:
请问,我怎样才能将位图图像直接传递给 pdf 文件。我用 GraphView 制作了一个图表,最后我将它转换为位图,在 OnClickListener 内:
write.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
Bitmap bitmap;
graph.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(graph.getDrawingCache());
graph.setDrawingCacheEnabled(false);
String filename = "imagen";
FileOperations fop = new FileOperations();
fop.write(filename, bitmap);
if (fop.write(filename,bitmap)) {
Toast.makeText(getApplicationContext(),
filename + ".pdf created", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getApplicationContext(), "I/O error",
Toast.LENGTH_SHORT).show();
}
}
});
问题是在 FileOperations 类中:
public FileOperations() {
}
public Boolean write(String fname, Bitmap bm) {
try {
String fpath = "/sdcard/" + fname + ".pdf";
File file = new File(fpath);
if (!file.exists()) {
file.createNewFile();
}
Document document = new Document();
PdfWriter.getInstance(document,
new FileOutputStream(file.getAbsoluteFile()));
document.open();
String filename = bm.toString();
com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(filename);
document.add(image);
document.add(new Paragraph("Hello World2!"));
// step 5
document.close();
Log.d("Suceess", "Sucess");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
我想知道如何传递位图图像以将其添加到 pdf 文档中,我这样做了,但我认为这只有在我给它一个路径时才有效。
字符串文件名 = bm.toString(); com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(filename);
【问题讨论】:
标签: pdf