Create PDF in android

Hello guys Today i am going to show you how to create PDF file in android also you can put the image in the PDF file it is very simple. ...


Hello guys Today i am going to show you how to create PDF file in android also you can put the image in the PDF file it is very simple.

Step 1 

First of all you have to download DroidText.jar. Download here for 
DroidText.jar ==> View raw file   

Android studio  ===>  goto your project and find libs folder inside app folder. Then this DroidText.jar file copy and past it. 

Eclipse  ==> the goto the libs folder and past in it and right click to to add configbuild path then clean the project.


Step 2 

Then create the project and copy this method into your button click or wherever you want  java file and past it .

YourJavaFile.java

public void createPDF() {
String path;

ArrayList<Integer> number;

Document doc;

      doc = new Document();

      try {

        // Where you want to store pdf file?
        // I saved in ADUREC folder into the phone storage  

         path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ADUREC";

         File dir = new File(path);

         if (!dir.exists())

            dir.mkdirs();

          //Check Logcat for message

         Log.d("PDFCreator", "PDF Path: " + path);

         //This is for the unique name of the pdf file

         number = new ArrayList<Integer>();

         for (int i = 1; i <= 10; ++i) number.add(i);

         Collections.shuffle(number);

         File file = new File(dir, "Document" + number + ".pdf");

         FileOutputStream fOut = new FileOutputStream(file);

         PdfWriter.getInstance(doc, fOut);

         //open the document

         doc.open();


          //Whatever you want to print in here pragraph  

         //add paragraph to document

            Paragraph p1 = new Paragraph("textview 1");

            Font paraFont1 = new Font(Font.COURIER, 14.0f, Color.GREEN);

            p1.setAlignment(Paragraph.ALIGN_JUSTIFIED);

            p1.setFont(paraFont1);

            doc.add(p1);

            Paragraph p2 = new Paragraph("textview 2");
            Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
            p2.setAlignment(Paragraph.ALIGN_JUSTIFIED);
            p2.setFont(paraFont2);

            doc.add(p2);

            Paragraph p3 = new Paragraph("textview 3");
            Font paraFont3 = new Font(Font.COURIER, 14.0f, Color.GREEN);
            p3.setAlignment(Paragraph.ALIGN_JUSTIFIED);
            p3.setFont(paraFont3);
            doc.add(p3);

         ByteArrayOutputStream stream = new ByteArrayOutputStream();

//get your logo into the drawable folder
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.logo);

         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
         Image myImg = Image.getInstance(stream.toByteArray());
         myImg.setAlignment(Image.MIDDLE);

//            add image to document
         doc.add(myImg);

         //set footer
         Phrase footerText = new Phrase("ADUREC DOCUMENT");
         HeaderFooter pdfFooter = new HeaderFooter(footerText, true);
         doc.setFooter(pdfFooter);

           Toast.makeText(getApplicationContext(), "success pdf", Toast

                   .LENGTH_LONG).show();

      } catch (DocumentException de) {

         Log.e("PDFCreator", "DocumentException:" + de);

      } catch (IOException e) {

         Log.e("PDFCreator", "ioException:" + e);

      } finally {

         doc.close();

      }

   }



Now Run the project and see the magic. 

COMMENTS

Name

Alert Android Chat Expandable Expandable Listview Expandable Listview with Json Facebook GCM JSON Login Notification PDF SDK 4.0
false
ltr
item
Android Knowledge: Create PDF in android
Create PDF in android
Android Knowledge
http://androidknowledgeblog.blogspot.com/2015/12/create-pdf-in-android.html
http://androidknowledgeblog.blogspot.com/
http://androidknowledgeblog.blogspot.com/
http://androidknowledgeblog.blogspot.com/2015/12/create-pdf-in-android.html
true
2625512956379495182
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy