droidtext添加图像不起作用 [英] droidtext adding image doesn't work

查看:162
本文介绍了droidtext添加图像不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am desperately trying to insert an image into an existing pdf with droidtext.

此项目的原始版本是使用iText制作的。
所以代码已经存在并被修改为适合Android。

The original version of this project was made with iText. So the code already exists and was modified to fit for Android.

我所做的是将现有的PDF作为背景。
在此pdf中的指定位置插入文本和十字。
喜欢填写表格。
到目前为止,这种方法效果很好而且没有大幅改变代码。

What I do is I take an existing PDF as background. Insert text and crosses at specified positions within this pdf. Like filling out a form. This works quite well so far without changing the code drastically.

现在我想将图像设置到页面底部以对表单进行签名。
我使用了原始代码并对其进行了调整。
这根本不起作用。
我试图将图像设置在特定位置。也许这就是错误。

Now I want to set an image to the bottom of the page to sign the form. I used my original code and adapted it a little. Which doesn't work at all. I tried to set the image at a specific position. Maybe that was the error.

所以我试图以官方的方式做到这一点。
Pengiuns.jpg图片位于SD卡上。

So i tried to do it the "official" way. The Pengiuns.jpg image is located on the sd-card.

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()得到了正确的文件大小。
没有抛出异常。

But still no image at all. What I get is an PDF with the words "Simple Image" on it and nothing else. I can access the picture. I get the correct filesize by the if(). No exceptions are thrown.

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

So my questions are, how do I get an Image located on the SD-Card into my pdf? What is the mistake here? But most importantly how do I set the image to a specific location with size within the pdf? In my original code i use setAbsolutePosition( x, y ) for that. Eclipse is not complaining when I use it in the code but is it really working?

推荐答案

你之所以这样做得到简单图像是因为你在段落中有它。要添加图像,请使用:

The reason why you are getting the "Simple Image" is because you have it in Paragraph. In order to add an image, use:

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

如果你想在最后一页中将它作为页脚,你可以使用以下代码,但它适用于文字。您可以尝试操作它以获取图像:

If you want to have it as a footer in the last page you can use the following code but it works with text. You can try to manipulate it for image:

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

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

Here is sample code. Here I have uploaded an image to Imageview and then added to pdf. Hope this helps.

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

这篇关于droidtext添加图像不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆