从图像路径获取图像并转换为PDF

时间:2017-11-02 07:02:06

标签: android

我正在将文本和图像导出为pdf。它将保存在设备内存中。 文本正在成功导出。 我将图像的路径保存在数据库中。因此,如果我将图像导出为pdf,它将显示图像的路径。 如何从图像路径获取图像并将其添加到pdf文档? 我在适配器中执行pdf功能。请帮帮我们。

这是我的代码:

        //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//

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

此代码有效..

private boolean taxFree;