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.