5

I am generating pdf using droidText liberary

I have the following code

public void createPDF()
{
    Document doc = new Document();


     try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";

            File dir = new File(path);
                if(!dir.exists()){
                     System.out.println("directory not exists");
                     dir.mkdirs();
             }else{
                 System.out.println("directory exirsts");
             }
             System.out.println("path="+path);

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


            File file = new File(dir, "sample.pdf");
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();


            Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText");
            Font paraFont= new Font(Font.COURIER);
            p1.setAlignment(Paragraph.ALIGN_CENTER);
            p1.setFont(paraFont);

             //add paragraph to document    
             doc.add(p1);

             Paragraph p2 = new Paragraph("This is an example of a simple paragraph");
             Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN);
             p2.setAlignment(Paragraph.ALIGN_CENTER);
             p2.setFont(paraFont2);

             doc.add(p2);

//

             //set footer
             Phrase footerText = new Phrase("This is an example of a footer");
             HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
             doc.setFooter(pdfFooter);




             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setDataAndType(Uri.fromFile(file),"application/pdf");
             intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
             startActivity(intent);

     } catch (DocumentException de) {
             Log.e("PDFCreator", "DocumentException:" + de);
     } catch (IOException e) {
             Log.e("PDFCreator", "ioException:" + e);
     } 
     finally
     {
             doc.close();
     }

}  

it works.. but when I add Image to it using following lines, it says "The method getInstance(byte[]) is undefined for the type Document"

   ByteArrayOutputStream stream = new ByteArrayOutputStream();
             Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.ic_launcher);
             bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
             Image myImg = Image.getInstance(stream.toByteArray());
             //myImg.setAlignment(Image.MIDDLE);                 
             add image to document
             doc.add(myImg);

help me in adding images to the document

Naveed Ali
  • 2,609
  • 1
  • 22
  • 37
  • Possible duplicate of [droidtext adding image doesn't work](http://stackoverflow.com/questions/15951656/droidtext-adding-image-doesnt-work) – Appleman1234 Apr 04 '14 at 05:27
  • 1
    Do check the imports, quite often happens default package get included, as there is android.media.Image class. – Engit Apr 11 '14 at 04:52

1 Answers1

4

As per Document.java, that class doesn't have a getInstance method. You want Image.getInstance as per this StackOverflow question,

E.g.

Image myImg = Image.getInstance(stream.toByteArray());
doc.add(myImg);

which isn't a duplicate question but does have an answer which answers your statement of "help me in adding images to the document"

Community
  • 1
  • 1
Appleman1234
  • 15,946
  • 45
  • 67
  • Please update your original question, with what you have done, and the results of what you have done. You say it doesn't work but you don't describe whether it still has the same error message or if the error message is gone, but the image still doesn't appear, etc. – Appleman1234 Apr 04 '14 at 05:59