9

**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);**
Foggzie
  • 9,691
  • 1
  • 31
  • 48
Varun Singh
  • 153
  • 1
  • 2
  • 5

3 Answers3

6

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();
    }
 }
 }
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • 1
    If you're going to recommend something, please atleast mention what library you are using. That is not in the standard android SDK. @Mehul – JoxTraex Jan 18 '13 at 06:53
  • @JoxTraex..you right..i forgot to mention the itextpdf-5.3.2.jar file. – Mehul Ranpara Jan 18 '13 at 06:56
  • Thanx For help. Its working But Problem is that when I click on Convert to PDF the file is saved on specified path "mnt/sdcard/FirstPdf.pdf"; and the old pdf file is replaced with new one as every time I convert an image it is saved as FirstPdf.pdf. – Varun Singh Jan 21 '13 at 09:55
  • you can give any name..inplace of Firstpdf.pdf to img1.pdf/img2.pdf – Mehul Ranpara Jan 21 '13 at 10:15
  • @MehulRanpara Actually I'm capturing that image from camera and then click on Convert to pdf button. So Every time there is a new image and if I give any name then every image is saved with that name replacing previous. – Varun Singh Jan 21 '13 at 10:40
  • Welcome.. Bro..if you satisfy then upvote my answer – Mehul Ranpara Jan 21 '13 at 12:55
  • @MehulRanpara Actually voting up requires min. 15 reputation and I have made a/c on stack overflow 2 days ago so currently I have 8 reputation. When I will have more than 15 I will vote up. – Varun Singh Jan 22 '13 at 04:50
  • @MehulRanpara is this jar free or does it require a fee if i have to use it for commercial purpose..any idea? – Snehal Poyrekar Dec 23 '13 at 07:43
  • You can use for commercial purpose..you have not to pay anything for this jar file.. – Mehul Ranpara Dec 23 '13 at 12:46
  • it's ok build in eclipse,but not ok build in Android source.i get this: ImageService.java:167: cannot access java.awt.Image class file for java.awt.Image not found image = Image.getInstance(bArray); // /Here i set byte array..you – kangear Apr 10 '14 at 01:50
  • @MehulRanpara, I have implemented in the same way as you suggested. The thing is that when I am creating Bitmap and setting it to an Imageview its getting full image of all the contents present in my ScrollView. But while generating PDF for the same bitmap its showing only some part of the full image. Can you help me to sort out this. – Salman Khan Jan 05 '15 at 09:34
  • @MehulRanpara iTextPDF can *not* be used for commercial purposes unless you buy a commercial license. This library is otherwise published under the AGPL copyleft license so anyone using it *must* publish their source code too! (see https://github.com/itext/itextpdf/blob/master/LICENSE.md) – TBieniek Sep 30 '15 at 08:46
  • hello @SalmanKhan i have same problem i am also unable to store pdf of full bitmap have you solved your problem? – Jeetu Jan 27 '16 at 07:34
2

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
-4

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

  1. It is not possible as per this answer on SO

  2. It is possible as per this answer on SO

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

Community
  • 1
  • 1
baldguy
  • 2,090
  • 1
  • 16
  • 25