我正在用相机拍照,并将照片值保存到位图中。我想在itext中使用该照片来生成pdf。
这是我到目前为止的代码。
Bitmap bitmap;
public void Picture()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bitmap=(Bitmap)data.getExtras().get("data");
PDF();
}
public void PDF()
{
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Image img = Image.getInstance(bitmap);
Document document = new Document();
PdfWriter.getInstance(document, out);
document.open();
document.add(new Paragraph("Example"));
document.close();
}
答案 0 :(得分:0)
bitmap=(Bitmap)data.getExtras().get("data");
ByteArrayOutputStream stream3 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream3);
Image maimg = Image.getInstance(stream3.toByteArray());
maimg.setAbsolutePosition(490, 745);
maimg.scalePercent(40);
document.add(maimg);
答案 1 :(得分:0)
您应该下载itextpdf-5.3.2.jar文件并将其附加到您的项目中。
您可以使用它作为示例:
public class WritePdfActivity extends Activity
{
private static String FILE = "mnt/sdcard/FirstPdf.pdf";
static Image image;
static ImageView img;
Bitmap bmp;
static Bitmap bt;
static byte[] bArray;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.imageView1);
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addImage(document);
document.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void addImage(Document document)
{
try
{
image = Image.getInstance(bArray); ///Here i set byte array..you can do bitmap to byte array and set in image...
}
catch (BadElementException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try
{
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里是参考,请检查 enter link description here
使用PDFBox库将图像转换为PDF更好的一件重要事情