0

I am taking a photo with the camera and saving the photo value into a bitmap. I would like to use that photo in itext to generate an pdf.

This is the code I have so far.

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();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
L.Lieb
  • 21
  • 2
  • 5

3 Answers3

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);  
0

you should download itextpdf-5.3.2.jar file and attach in your project.

You can use it as an example:

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

Here is the reference, Please check enter link description here

One Important thing better to use PDFBox library for convert Image to PDF

  • Why do you propose downloading iText 5.3.2? That version is outdated and cannot even claim any license "advantages" compared to the current 5.5.x versions of iText 5. Furthermore, if you don't intend to handle any exception in `addImage`, why do you catch them? – mkl Oct 03 '18 at 13:19
0

In iText7 I have to create my own solution:

//an auxiliary function
fun convertFromBitMapToByteArray(bitmap: Bitmap) : ByteArray {
            val byteArrayOutputStream = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream)
            return byteArrayOutputStream.toByteArray()
        }

//then inside your table creator...
 val table = Table(
            UnitValue.createPointArray(
                floatArrayOf(
                    200f,
                )
            )
        )

        // headers
        table.addCell(Paragraph("Photo").setBold())
        

        // items
        for (productModel in productModels) {
            val images: List<Image> = productModel.photoModelList.map {
                val byteArray = BitmapDAOImpl.convertFromBitMapToByteArray(it.image)
                Image(ImageDataFactory.create(byteArray)).apply {
                    setAutoScale(true)
                }
            }
            val imageCell: Cell = Cell()
            images.forEach(imageCell::add)
            table.addCell(imageCell)
        }
starball
  • 20,030
  • 7
  • 43
  • 238