java - droidtext 添加图像不起作用

标签 java image pdf android-droidtext

我正在拼命尝试使用 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 时,它​​并没有提示,但它真的有效吗?

最佳答案

您获得“简单图像”的原因是因为您将其放在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);

关于java - droidtext 添加图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15951656/

相关文章:

java - PdfBox 从 pdf 中提取具有相同字体系列的文本

java - 授予 Android 应用程序 (.apk) 文件根访问权限

vb.net - 如何旋转保留其原始大小的图像?

php imagecopyresized vs imagecopyresampled vs imagecopy 优点/缺点

opencv中的图像缩放/锐度

image - 如何在 flutter 中以 base64 显示 pdf?

java - 十进制数转十六进制 - Java

java - 向量化梯度下降算法

java - 在java中写单个字符的奇怪行为

xcode - 在Cocoa中打印WebView的全部内容,而不仅仅是显示