简体   繁体   English

无法将图像从 mysql 数据库添加到 pdf 文件

[英]Unable to add Image to pdf file from mysql database

Document myDocument = new Document();
                    PdfWriter myWriter = PdfWriter.getInstance(myDocument, 
new FileOutputStream(filePath));
                    myDocument.open();
                    Blob imageBlob = rs.getBlob("Signature");
                    byte[] imageBytes = imageBlob.getBytes(1, (int) 
imageBlob.length());
                    Image image = Image.getInstance(imageBytes);
                    image.scaleAbsolute(300,300);
                    myDocument.add(image);

I am trying to get Image from the database stored in the data type of medium blob.我试图从存储在中等 blob 数据类型中的数据库中获取图像。 I have tried everything and checked various codes on stackoverflow but not bale to resolve an error.我已经尝试了所有方法并检查了 stackoverflow 上的各种代码,但无法解决错误。 Each time it is showing that getInstance() method not found.每次都显示找不到 getInstance() 方法。

For 对于

byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());
Image image = Image.getInstance(imageBytes);

you get a "getInstance() method not found" error. 您收到“找不到getInstance()方法”错误。

As the iText Image class clearly does have a static getInstance method with a single byte[] argument, this indicates that the Image class here does not refer to the iText Image class but instead to a different one, probably the awt Image class. 由于iText Image类显然具有带有单个byte[]参数的静态getInstance方法,因此表明此处的Image类未引用iText Image类,而是引用了另一个类,可能是awt Image类。

Thus, look at your imports, probably you import some whole packages which may contain their own Image class and so mask the iText Image class. 因此,请查看您的导入,可能是您导入了一些可能包含其自身Image类的整个程序包,因此屏蔽了iText Image类。

Blob imageBlob1 = rs.getBlob("Image");
                      byte[] imageBytes1 = imageBlob1.getBytes(1, (int) imageBlob1.length());
                     com.itextpdf.text.Image image1 = com.itextpdf.text.Image.getInstance(imageBytes1);
                     image1.scaleToFit(80f, 350f);
                     image1.setAbsolutePosition(430,680);
                      image1.scaleAbsoluteWidth(70);
                     image1.scaleAbsoluteHeight(80);
                     myDocument.add(image1);

The above code worked for me.. 上面的代码为我工作。

This code is working for me这段代码对我有用

try {
    String sql1 = " Select foto from ishirahamwe.abanywanyi where numero = \'" + strId + "\'";
    Statement statement = conn.prepareStatement(sql1);
    rs = statement.executeQuery(sql1);
    if (rs.next()) {
                    
        Blob imageBlob1 = rs.getBlob("foto");
        byte[] imageBytes1 = imageBlob1.getBytes(1, (int) imageBlob1.length());
        com.lowagie.text.Image image1 = com.lowagie.text.Image.getInstance(imageBytes1);
        image1.scaleToFit(80f, 350f);
        image1.setAbsolutePosition(430,680);
        image1.scaleAbsoluteWidth(70);
        image1.scaleAbsoluteHeight(80);
        document.add(image1);

    }

} catch (Exception e1) {
    e1.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM