Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用itextPdf将字节数组转换为图像并以pdf格式显示_Java_Pdf_Itext - Fatal编程技术网

Java 如何使用itextPdf将字节数组转换为图像并以pdf格式显示

Java 如何使用itextPdf将字节数组转换为图像并以pdf格式显示,java,pdf,itext,Java,Pdf,Itext,如何在itext pdf中将字节数组转换为图像并在pdf中显示。 这就是我正在做的,但我得到了空指针异常 Base64 decoder = new Base64(); byte[] imageByte = decoder.decode(imageInBase64EncodedString); Image image = null; try { image = Image.getInstance(imageByte);

如何在itext pdf中将字节数组转换为图像并在pdf中显示。 这就是我正在做的,但我得到了空指针异常

    Base64 decoder = new Base64();
    byte[] imageByte = decoder.decode(imageInBase64EncodedString);
        Image image = null;
        try {
            image = Image.getInstance(imageByte);
        } catch (BadElementException e1) {
            e1.printStackTrace();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            image.scalePercent(15f);  <-----Here i am getting NullPointer Exception
            image.scaleAbsoluteWidth(520f);
            image.setAbsolutePosition(40f,725f);
            document.add(image);
       }catch(Exception e){
            e.printStackTrace();
       }
Base64解码器=新的Base64();
byte[]imageByte=解码器.decode(imageInBase64EncodedString);
图像=空;
试一试{
image=image.getInstance(imageByte);
}捕获(BadElementException e1){
e1.printStackTrace();
}捕获(格式错误的异常e1){
e1.printStackTrace();
}捕获(IOE1异常){
e1.printStackTrace();
}
试一试{

scalePercent(15f);问题很可能是您传入的字符串值,因为很可能是因为值中的格式错误而遇到异常

我已经尝试了您的示例,使用了PasteBin()中的BASE64字符串示例,有人非常友好地发布了该示例,并且根据您提供的代码成功地运行了脚本

ItextPdf示例

屏幕截图1

屏幕截图2(图像对象不为空)

org.apache.commons.codec.binary.base64示例

问题很可能是您传入的字符串值,因为很可能是因为值中的格式错误而遇到异常

我已经尝试了您的示例,使用了PasteBin()中的BASE64字符串示例,有人非常友好地发布了该示例,并且根据您提供的代码成功地运行了脚本

ItextPdf示例

屏幕截图1

屏幕截图2(图像对象不为空)

org.apache.commons.codec.binary.base64示例

上面的
printStackTrace
调用是否触发了?可能重复Ty用于回复guys@mkl no@DanCornilescu Ty用于评论,但我不是要求nullpointer异常。我的意思是,当我传递字节数组时,为什么图像为null。请共享
imageInBase64EncodedString
的值;否则我们无法回答您r问题。上面的
printStackTrace
调用是否触发了?可能是Ty的重复,用于回复guys@mkl no.@DanCornilescu Ty进行评论,但我不是要求nullpointer异常。我的意思是,当我传递字节数组时,为什么图像为null。请共享
imageInBase64EncodedString
的值;否则我们无法拒绝回答你的问题。你使用ItextPdf图像类。我使用import org.apache.commons.codec.binary.Base64解码并获取字节值。但是在Image.scalePercent(15f)我正在获取null…我已修改示例以包含您正在使用的库,并且我的测试仍然通过。您可以向我提供您尝试传递的字符串值吗?您使用的是ItextPdf图像类。我正在使用import org.apache.commons.codec.binary.Base64进行解码并获取字节值。但是在Image.scalePercent(15f)我得到空值…我修改了示例以包含您正在使用的库,并且我的测试仍然通过。您能提供您尝试传递的字符串值吗?