0

I am using iText lib to insert text into a pdf file. I want to insert pictures into the same pdf as well, so I created an activity specially for taking pictures (PhotoActivity). It happened to me that the user might need to insert not only one, but multiple pictures, and the code below is kind of static, it is effective in case the user takes only one picture:

import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class imagesPDF
{     
public static void main(String arg[])throws Exception
{                  
    Document document=new Document();
    PdfWriter.getInstance(document,new FileOutputStream("pdfFile.pdf"));
    document.open();
    Image image = Image.getInstance ("TakenPhoto.jpg");
    document.add(new Paragraph("Image Heading"));
    document.add(image);               
    document.close();
}
}

My PhotoActivity code (called by intent from the MainActivity) is as it follows:

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class PhotoActivity extends ActionBarActivity {
Button b1;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo);

    b1 = (Button) findViewById(R.id.button1);

    iv = (ImageView) findViewById(R.id.imageView);

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

          Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(intent, 0);
        }
    });
}

//  Button to go back to the MainActivity
public void onclickButton2(View view) {

    PhotoActivity.this.finish();
 }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bp = (Bitmap) data.getExtras().get("data");
    iv.setImageBitmap(bp);
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

I would like to know if someone could show me a dynamic way to programmatically acknowledge the # of pictures taken during the execution of the PhotoActivity, and once knowing this, put them into the pdf. I appreciate if anyone could help me. Thank you in advance.

Gramowski
  • 1,281
  • 1
  • 8
  • 13
  • I see my name in your code, which means that you're not using the Android version of iText: [iTextG](http://itextpdf.com/product/itextg). This means that you can run into trouble because the "regular" iText depends on `java.awt` classes that are not whitelisted for use on Android. As for your question: whether or not you are using iText is irrelevant. Your question is a pure Android question: how many pictures are taken? Once you know that, you should use this code: http://stackoverflow.com/a/29340807 – Bruno Lowagie Jun 06 '15 at 10:50
  • Hi Bruno. Firstly, sorry to use an example that contains your name without permission, but this is NOT part of my code, just an ordinary example of adding a single picture into a pdf, found right here in stack overflow. Secondly, I must express my appreciation of having yourself commenting my question. Well, now I am trying to understand your other topic's code, but the thing is I don't have any means to guess how many pics the user will take. So the challenge here is: 1- know how many pics the user will take, and 2- take them all into the pdf, regardless the name of the pics and how many. – Gramowski Jun 06 '15 at 17:53
  • Maybe you should split the problem in two separate questions: 1. how can we track how many pictures a user is taking? This question is not an iText question. It's a pure Android question to which I don't know the answer. 2. Take them all into the PDF: that is easy. That question has been answered many times before on StackOverflow. The bottleneck is question 1: remove all references to iText and you might get the attention of pure Android developers (who may now discard the question as: it's about iText and we don't know iText). – Bruno Lowagie Jun 07 '15 at 07:38
  • As for using my name in your code: many people have done that in the past and that's not the problem. To me, it's just an indication that (1.) you're using an obsolete version of iText, and (2.) you're not using a version that was optimized for use on Android (there are slight differences between iText for Java and iTextG for Android). – Bruno Lowagie Jun 07 '15 at 07:39
  • Understood Bruno. Thanks a lot for your comments. The iText libs that I am actually using in my real code are already the ones for Android. Will remove the iText tag and hope that other stack-overflowers can help. Thank you once again. – Gramowski Jun 07 '15 at 12:55

0 Answers0