如何在android中将位图转换为PDF格式 [英] How I can convert a bitmap into PDF format in android

查看:76
本文介绍了如何在android中将位图转换为PDF格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**我在位图类型的thepic"变量中有位图..

**I have bitmap in "thepic" variable which is of Bitmap type..

imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 
String realpath=getRealPathFromURI(imageUri); 
thepic = BitmapFactory.decodeFile(realpath);**

推荐答案

你可以这样做...你必须下载 itextpdf-5.3.2.jar 文件并附加到您的项目中..

you can do by this way...you have to download itextpdf-5.3.2.jar file and attach in your project..

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

这篇关于如何在android中将位图转换为PDF格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆