java 如何在android中将位图转换为PDF格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14392896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:07:14  来源:igfitidea点击:

How I can convert a bitmap into PDF format in android

javaandroidpdfbitmap

提问by Varun Singh

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

**我在“thepic”变量中有位图,它是位图类型..

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

采纳答案by Mehul Ranpara

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

你可以这样做...你必须下载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();
    }
 }
 }

回答by Gabe Sechan

You need to use a 3rd party library, there's no built in ability. I know a few libraries that do the reverse (Qoppa, PDFTron, Reade) but they all cost a lot of money. I've heard iText works well for writing to bitmaps, but haven't used it myself.

您需要使用第 3 方库,没有内置功能。我知道一些相反的库(Qoppa、PDFTron、Reade),但它们都花了很多钱。我听说 iText 可以很好地写入位图,但我自己没有使用过。

回答by Rahil2952

I haven't tried this but looking on stackoverflow I am giving you answer.

我没有试过这个,但在 stackoverflow 上我给你答案。

  1. It is not possible as per this answeron SO

  2. It is possible as per this answeron SO

  1. 根据 SO 上的这个答案,这是不可能的

  2. 根据 SO 上的这个答案是可能的

So you check both answers study them and see whether it hepls you or not.

所以你检查两个答案研究它们,看看它是否对你有帮助。