Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can convert a bitmap into PDF format in android [closed]

**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);**
like image 622
Varun Singh Avatar asked Jan 18 '13 05:01

Varun Singh


2 Answers

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();
    }
 }
 }
like image 88
Mehul Ranpara Avatar answered Oct 18 '22 21:10

Mehul Ranpara


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.

like image 2
Gabe Sechan Avatar answered Oct 18 '22 21:10

Gabe Sechan