Pages

how to file write pdf file in andriod sdcard



Very simple Method And easy

But before you start this application you must download iTextpdf related jar(s).

you need to use jar files Click Here  

The code use in your activity of where you need at position.

try {
              
                OutputStream file = new FileOutputStream(new File("storage/sdcard/file.pdf"));
                Document document = new Document();
                PdfWriter.getInstance(document, file);

                //Inserting Image in PDF
//                Image image = Image.getInstance ("D:/dog.png");
//                image.scaleAbsolute(120f, 60f);//image width,height  

                //Inserting Table in PDF
                ArrayList al = new ArrayList();
                al.add("Name");
                al.add("Address");
          
                PdfPTable table=new PdfPTable(2);

                PdfPCell cell = new PdfPCell (new Paragraph ("Beautyton"));

                cell.setColspan (3);
                cell.setHorizontalAlignment (Element.ALIGN_CENTER);
                cell.setPadding (10.0f);
                cell.setBackgroundColor (new BaseColor (140, 221, 8));                                

                table.addCell(cell);                                    

                table.addCell("Problems");
                table.addCell("Solutions");
              
                for(int i=0;i                {
                    table.addCell(finallist1.get(i));// filallist1 is arraylist
                    table.addCell(finallist.get(i));// filallist is arraylist
                  
                }
                table.setSpacingBefore(50.0f);       // Space Before table starts, like margin-top in CSS
                table.setSpacingAfter(50.0f);        // Space After table starts, like margin-Bottom in CSS                                        

                //Now Insert Every Thing Into PDF Document
                document.open();//PDF document opened........                

                document.add(Chunk.NEWLINE);   //Something like in HTML :-)

                document.add(new Paragraph("Welcome"));
                document.add(new Paragraph("Document Generated On - "+new Date(mImageCount).toString()));  

                document.add(table);

                document.add(Chunk.NEWLINE);   //Something like in HTML :-)                              
                document.close();
                file.close();
              
                Toast.makeText (getApplicationContext(), "Saved :", Toast.LENGTH_LONG).show ();
                System.out.println("Pdf created successfully..");

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


i hope its working fine.

No comments:

Post a Comment