【问题标题】:droidtext adding image doesn't workdroidtext 添加图像不起作用
【发布时间】:2013-04-11 14:38:47
【问题描述】:

我正在拼命尝试使用 droidtext 将图像插入到现有的 pdf 中。

这个项目的原始版本是用 iText 制作的。 所以代码已经存在并被修改以适应 Android。

我所做的是将现有的 PDF 作为背景。 在此 pdf 中的指定位置插入文本和十字。 就像填写表格一样。 到目前为止,这工作得很好,无需大幅更改代码。

现在我想在页面底部设置一张图片来签署表格。 我使用了我的原始代码并对其进行了一些调整。 这根本不起作用。 我试图将图像设置在特定位置。也许这就是错误。

所以我试着用“官方”的方式来做。 Pengiuns.jpg 图像位于 sd 卡上。

try {
Document document = new Document();
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf");
PdfWriter.getInstance(document,new FileOutputStream(f));
document.open();
document.add(new Paragraph("Simple Image"));
String path = Environment.getExternalStorageDirectory()+"/Penguins.jpg";

if (new File(path).exists()) {
    System.out.println("filesize: " + path + " = " + new File(path).length());
}

Image image =Image.getInstance(path);
document.add(image);
document.close();
} catch (Exception ex) {
    System.out.println("narf");
}

但仍然没有图像。 我得到的是一个PDF,上面写着“简单图像”,没有别的。 我可以访问图片。我通过 if() 获得了正确的文件大小。 不会抛出异常。

所以我的问题是,如何将 SD 卡上的图像放入我的 pdf 文件中? 这里有什么错误? 但最重要的是,如何将图像设置到 pdf 中具有大小的特定位置? 在我的原始代码中,我使用 setAbsolutePosition( x, y ) 。 当我在代码中使用 Eclipse 时,它​​没有抱怨,但它真的有效吗?

【问题讨论】:

  • Droidtext 是 iText 过时版本的一个分支。 iText 的作者(我是最初的 iText 开发人员)不认可此分叉,因此不受支持。您是否尝试过使用 iText 的官方 Android 端口? itextsupport.com/download/android.html

标签: java image pdf android-droidtext


【解决方案1】:

您获得“简单图像”的原因是因为您在Paragraph 中有它。要添加图像,请使用:

Image myImg1 = Image.getInstance(stream1.toByteArray());

如果您想将其作为最后一页的页脚,您可以使用以下代码,但它适用于文本。您可以尝试为图像操作它:

Phrase footerText = new Phrase("THIS REPORT HAS BEEN GENERATED USING INSPECTIONREPORT APP");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);

这里是示例代码。在这里,我已将图像上传到 Imageview,然后添加到 pdf。希望这会有所帮助。

private String NameOfFolder = "/InspectionReport"; 

Document doc = new Document();

try {   //Path to look for App folder 
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
    String CurrentDateAndTime= getCurrentDateAndTime();   

    // If App folder is not there then create one
    File dir = new File(path);
    if(!dir.exists())
        dir.mkdirs();


    //Creating new file and naming it
    int i = 1;  

    File file = new File(dir, "Inspection"+Integer.toString(i)+"-" + CurrentDateAndTime+".pdf");
    while(file.exists()) {
        file = new File(dir, "Inspection"+Integer.toString(i++)+"-" + CurrentDateAndTime+".pdf");}


        String filep= file.toString();
        FileOutputStream fOut = new FileOutputStream(file);

        Log.d("PDFCreator", "PDF Path: " + path);
        PdfWriter.getInstance(doc, fOut);
        Toast.makeText(getApplicationContext(),filep , Toast.LENGTH_LONG).show();

        //open the document
        doc.open();

        ImageView img1 = (ImageView)findViewById(R.id.img1);
        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        Bitmap bitmap1 = ((BitmapDrawable)img1.getDrawable()).getBitmap();
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100 , stream1);
        Image myImg1 = Image.getInstance(stream1.toByteArray());
        myImg1.setAlignment(Image.MIDDLE);

        doc.add(myImg1);

【讨论】:

    【解决方案2】:

    试试下面的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多