1

I am creating PDF generating application from user input in android. I want to add an image in inside of table field. How can i add image inside of a table column? Please explain me.

public class MainActivity extends Activity {

    EditText et1,et2,et3,et4;
    Button Save;
    String edit1,edit2,edit3,edit4;
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell1, cell2,cell3,cell4, cell5,cell6,cell7, cell8,cell9,cell10;
    File cacheDir;
    final Context context = this;
    ImageView img;

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

         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Thiru");



            else
                cacheDir=context.getCacheDir();
            if(!cacheDir.exists())
                cacheDir.mkdirs();

        et1=(EditText)findViewById(R.id.editText1);
        et2=(EditText)findViewById(R.id.editText2);
        et3=(EditText)findViewById(R.id.editText3);
        et4=(EditText)findViewById(R.id.editText4);
        img = (ImageView) findViewById(R.id.imageView);

        Save=(Button)findViewById(R.id.button1);
        Save.setOnClickListener(reportClickListener);


    }

OnClickListener reportClickListener= new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            edit1=et1.getText().toString();
            edit2=et2.getText().toString();
            edit3=et3.getText().toString();
            edit4=et4.getText().toString();

            String FILE = Environment.getExternalStorageDirectory().toString() + "/Thiru/" + "report.pdf";

            // Create New Blank Document
            Document document = new Document(PageSize.A4);

            // Create Pdf Writer for Writting into New Created Document
            try {
                PdfWriter.getInstance(document, new FileOutputStream(FILE));
                // Open Document for Writting into document
                document.open();
                // User Define Method


                addTitlePage(document);
            } catch (FileNotFoundException e) {

                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Close Document after writting all content
            document.close();

            Toast.makeText(MainActivity.this, "PDF File is Created."+FILE, Toast.LENGTH_LONG).show();
        }
    };

PdfPTable table = new PdfPTable(3);

// t.setBorderColor(BaseColor.GRAY);
// t.setPadding(4);
// t.setSpacing(4);
// t.setBorderWidth(1);

            PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Table Header 2"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);

            c1 = new PdfPCell(new Phrase("Table Header 3"));
            c1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(c1);
            table.setHeaderRows(1);

            table.addCell("1.0");
            table.addCell("1.1");
            table.addCell("1.2");
            table.addCell("2.1");
            table.addCell("2.2");
            table.addCell("2.3");
            table.addCell(image);

            document.add(table);

document.newPage();
            //Toast.makeText(this, "PDF File is Created.", Toast.LENGTH_LONG).show();
        }


}

I already know how to create PDF file from user input. What I need is how to add image in table cell. How to add image? And i want to know how to add image from URL in PDF file.

PulkitG
  • 612
  • 4
  • 12

2 Answers2

0

Here is code in which you can add logo or header Image in Document. You have to change as per your requirement.

here is snippet code

public void addLogo(Document document) throws DocumentException {
    try { // Get user Settings GeneralSettings getUserSettings =

        Rectangle rectDoc = document.getPageSize();
        float width = rectDoc.getWidth();
        float height = rectDoc.getHeight();
        float imageStartX = width - document.rightMargin() - 315f;
        float imageStartY = height - document.topMargin() - 80f;

        System.gc();

        InputStream ims = getAssets().open("image.jpg"); // image from assets folder
        Bitmap bmp = BitmapFactory.decodeStream(ims);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);

        byte[] byteArray = stream.toByteArray();
        // PdfImage img = new PdfImage(arg0, arg1, arg2)

        // Converting byte array into image Image img =
        Image img = Image.getInstance(byteArray); // img.scalePercent(50);
        img.setAlignment(Image.TEXTWRAP);
        img.scaleAbsolute(200f, 50f);
        img.setAbsolutePosition(imageStartX, imageStartY); // Adding Image


        PdfPCell cellImage= new PdfPCell(img, true);  
        mTable.addCell(cellImage); //add image cell in table
        document.add(mTable);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
0

You can import image from a URL: Add image to PDF from a URL?

And you can add the image in table cell: http://developers.itextpdf.com/examples/tables-itext5/adding-images-table

You can mix both and change your table.addCell(image); like below

String imageUrl = "https://s.yimg.com/os/mit/ape/w/d8f6e02/dark/partly_cloudy_day.png";
Image imageFromWeb = Image.getInstance(new URL(imageUrl));
PdfPCell cellImageInTable = new PdfPCell(imageFromWeb , true);
table.addCell(cellImageInTable);
Community
  • 1
  • 1
Yoonian
  • 547
  • 9
  • 13