0

I am exporting the text and images to pdf. And it will be saved in the device memory. The text is exporting successfully. Am saving the image's path in database.So, if i export the images to pdf, it is showing the image's path. How to fetch image from image path and adding it to pdf document? Am doing the pdf function in the adapter. Please help me guys.

Here is my code:

        //pdf//
        pdf = (TextView)convertView.findViewById(R.id.pdf);
        pdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

/////
                String FILE = Environment.getExternalStorageDirectory().toString()
                        + "/PDF/" + list.get(position).getTitle()+".pdf";

                // Add Permission into Manifest.xml
                // <uses-permission
                // android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

                // Create Directory in External Storage
                String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/PDF");
                myDir.mkdirs();

                // 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
                 //document.add(list.get(position).getDate());
                addMetaData(document);
                addTitlePage(document,list.get(position).getDate(),list.get(position).getTitle(),list.get(position).getContent(),list.get(position).getPicture());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // Close Document after writting all content
                document.close();

                //Toast.makeText(this, "PDF File is Created. Location : " + FILE,
                  //      Toast.LENGTH_LONG).show();

                //////
            }
        });
        //pdf//





//pdf//
// Set PDF document Properties
public void addMetaData(Document document) {
document.addTitle("All memories");
}
public void addTitlePage(Document document,String date, String title,     String content, String picture) throws DocumentException, IOException {
    // Font Style for Document
    Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
            | Font.UNDERLINE, BaseColor.GRAY);
    Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);

    // Start New Paragraph
    Paragraph prHead = new Paragraph();
    // Set Font in this Paragraph
    prHead.setFont(titleFont);
    // Add item into Paragraph
    prHead.add("All Memories");
    //prHead.add(date + "\n");
    // Create Table into Document with 1 Row
    PdfPTable myTable = new PdfPTable(1);
    // 100.0f mean width of table is same as Document size
    myTable.setWidthPercentage(100.0f);

    // Create New Cell into Table
    PdfPCell myCell = new PdfPCell(new Paragraph(""));
    myCell.setBorder(Rectangle.BOTTOM);

    // Add Cell into Table
    myTable.addCell(myCell);

    prHead.setFont(catFont);
    //prHead.add("\nName1 Name2\n");
    prHead.setAlignment(Element.ALIGN_CENTER);

    // Add all above details into Document
    document.add(prHead);
    document.add(myTable);

    document.add(myTable);

    // Now Start another New Paragraph
    Paragraph prPersinalInfo = new Paragraph();
    prPersinalInfo.setFont(smallBold);
    prPersinalInfo.add(date+"\n");
    prPersinalInfo.add(title+"\n");
    prPersinalInfo.add(content+"\n");
    prPersinalInfo.add(picture+"\n");

// Convert the Image to Bitmap


    Image img =Image.getInstance(picture);

        img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM);

        prPersinalInfo.add(img);

        // Convert the Image to Bitmap

// Convert the Image to Bitmap

    prPersinalInfo.setAlignment(Element.ALIGN_CENTER);

    document.add(prPersinalInfo);
    document.add(myTable);

    document.add(myTable);

    // Create new Page in PDF
    document.newPage();
}


//pdf//
teddy
  • 21
  • 4

2 Answers2

0
                    // Convert the Image to Bitmap
                    Bitmap bitmap = convertImageToBitmap(img.getPath());

                    ByteArrayOutputStream st = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, st);
                    byte[] byteArray = st.toByteArray();
                    Image im = Image.getInstance(byteArray);
                    document.add(im);
Sardar Khan
  • 845
  • 6
  • 16
  • yeah, tried. it is showing cannot resolve method "convertImageToBitmap(?)" and "unhandled exception:java.io.IO.Exception" in the line "Image im = Image.getInstance(byteArray)" – teddy Nov 02 '17 at 08:49
  • Actually i did this and its working .. Image img =Image.getInstance(picture); img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); prPersinalInfo.add(img); and if the list doesnt contains picture, its showing null pointer exception. how to resolve this for not having picture? – teddy Nov 02 '17 at 09:19
  • How to do that one? – teddy Nov 02 '17 at 10:40
  • check if the picture path is not null. – Sardar Khan Nov 02 '17 at 10:55
  • if(picture!=null) { Image img =Image.getInstance(picture); img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); prPersinalInfo.add(img); } – Sardar Khan Nov 02 '17 at 11:47
  • if(!picture.contains("")){Image img =Image.getInstance(picture); img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); prPersinalInfo.add(img); } – Sardar Khan Nov 02 '17 at 11:53
  • these are the checks.Test it and tell me if its working or not.? @teddy. – Sardar Khan Nov 02 '17 at 11:54
0

This code works..

pdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

/////
                String FILE = Environment.getExternalStorageDirectory().toString()
                        + "/PDF/" + list.get(position).getTitle()+".pdf";

                // Add Permission into Manifest.xml
                // <uses-permission
                // android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

                // Create Directory in External Storage
                String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/PDF");
                myDir.mkdirs();

                // 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
                 //document.add(list.get(position).getDate());
                addMetaData(document);
                addTitlePage(document,list.get(position).getDate(),list.get(position).getTitle(),list.get(position).getContent(),list.get(position).getPicture());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // Close Document after writting all content
                document.close();

                //Toast.makeText(this, "PDF File is Created. Location : " + FILE,
                  //      Toast.LENGTH_LONG).show();

                //////
            }
        });



// Set PDF document Properties
public void addMetaData(Document document) {
document.addTitle("All memories");
}
public void addTitlePage(Document document,String date, String title, String content, String picture) throws DocumentException, IOException {
    // Font Style for Document
    Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
            | Font.UNDERLINE, BaseColor.GRAY);
    Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
    Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);

    // Start New Paragraph
    Paragraph prHead = new Paragraph();
    // Set Font in this Paragraph
    prHead.setFont(titleFont);
    // Add item into Paragraph
    prHead.add("All Memories");
    //prHead.add(date + "\n");
    // Create Table into Document with 1 Row
    PdfPTable myTable = new PdfPTable(1);
    // 100.0f mean width of table is same as Document size
    myTable.setWidthPercentage(100.0f);

    // Create New Cell into Table
    PdfPCell myCell = new PdfPCell(new Paragraph(""));
    myCell.setBorder(Rectangle.BOTTOM);

    // Add Cell into Table
    myTable.addCell(myCell);

    prHead.setFont(catFont);
    //prHead.add("\nName1 Name2\n");
    prHead.setAlignment(Element.ALIGN_CENTER);

    // Add all above details into Document
    document.add(prHead);
    document.add(myTable);

    document.add(myTable);

    // Now Start another New Paragraph
    Paragraph prPersinalInfo = new Paragraph();
    prPersinalInfo.setFont(smallBold);
    prPersinalInfo.add(date+"\n");
    prPersinalInfo.add(title+"\n");
    prPersinalInfo.add(content+"\n");


// Convert the Image to Bitmap

if(picture!=null) {
Image img = Image.getInstance(picture);


img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM);

prPersinalInfo.add(img);
}
        // Convert the Image to Bitmap

// Convert the Image to Bitmap

    prPersinalInfo.setAlignment(Element.ALIGN_CENTER);

    document.add(prPersinalInfo);
    document.add(myTable);

    document.add(myTable);

    // Create new Page in PDF
    document.newPage();
}
teddy
  • 21
  • 4