【问题标题】:getting image from image's path and converting to PDF从图像的路径中获取图像并转换为 PDF
【发布时间】:2017-11-02 07:02:05
【问题描述】:

我正在将文本和图像导出为 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//

【问题讨论】:

标签: android


【解决方案1】:
                    // 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);

【讨论】:

  • 是的,试过了。它显示无法解析“Image im = Image.getInstance(byteArray)”行中的方法“convertImageToBitmap(?)”和“未处理的异常:java.io.IO.Exception”
  • 其实我做了这个和它的工作.. Image img =Image.getInstance(picture); img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); prPersinalInfo.add(img);如果列表不包含图片,则显示空指针异常。没有图片怎么解决?
  • 那个怎么做?
  • 检查图片路径是否为空。
  • if(picture!=null) { Image img =Image.getInstance(picture); img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); prPersinalInfo.add(img); }
【解决方案2】:

此代码有效..

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();
}

【讨论】:

    猜你喜欢
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2013-03-28
    • 2011-07-20
    • 1970-01-01
    • 2011-01-14
    相关资源
    最近更新 更多