Java - 如何将包含图像的 JLabel 添加到 iText 中的 pdf 文件
Java - How to add a JLabel that contains an image to a pdf file in iText
我想添加从 mysql 数据库中检索到的图像,并将其打印在 iText java 中的 pdf 文件中。从数据库中检索到的图像存储在 lblimg 中。我如何在 java 中实现这一目标?
这是我的部分代码:
String filename = null;
int s = 0;
byte[] person_img = null;
uploadbtn = new JButton("Upload a Photo");
uploadbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
try{
File img = new File(filename);
FileInputStream fis = new FileInputStream(img);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum = fis.read(buf)) != -1;){
bos.write(buf, 0, readNum);
}
person_img = bos.toByteArray();
fis.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
});
// Partial code for adding image to db
stt.setBytes(8, person_img);
// Partial codes for retrieving image from db
byte[] imageData = rs.getBytes("Image");
format = new ImageIcon(imageData);
lblimg.setIcon(format);
//Creating the document and adding the lblimg (which contains the image retrieved from the db). PLEASE HELP HERE. I CANNOT ADD THE IMAGE TO PDF document.
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("Report.pdf"));
doc.open();
doc.add(new Paragraph( // img to be added here ));
Bruno Lowagie 更新 1
摘自完整代码的片段:
try {
Image i = Image.getInstance((PdfTemplate) lblimg.getIcon());
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("Report.pdf"));
doc.open();
Image img = Image.getInstance("ja.png");
doc.add(img);
doc.add(i);
doc.add(new Paragraph("Employee Information", FontFactory.getFont(FontFactory.TIMES_BOLD,18, Font.BOLD, BaseColor.RED)));
doc.add(new Paragraph("______________________________________________________________________________"));
doc.add(new Paragraph("Employee ID is " + val1));
doc.add(new Paragraph("First Name is " + val2 + "\t\t" + " Last Name is " + val3));
doc.add(new Paragraph("Job Position " + val4));
doc.add(new Paragraph("Allowances allowed " + val5));
doc.add(new Paragraph("Salary " + val10));
JOptionPane.showMessageDialog(null, "Report Saved");
doc.close();
} catch(Exception e1) {
e1.printStackTrace();
}
As getIcon
returns a javax.swing
class and as PdfTemplate
is an iText class extending the PdfContentByte
class 包含 ByteBuffer
的 PDF 语法,此处抛出 ClassCastException
:(PdfTemplate) lblimg.getIcon()
Bruno Lowagie 更新 2
实际问题已作为评论发布:如何检索 JLabel 中的图像并将其添加到 PDF 中?这个问题在我的答案的更新 3 中得到了回答。
假设这包含一张图片:
byte[] imageData = rs.getBytes("Image");
换句话说:假设 imageData
是有效的 JPEG、JPEG2000、GIF、PNG、BMP、WMF、TIFF 或 JBIG2 图像,那么您可以像这样创建一个 com.itextpdf.text.Image
对象:
Image img = Image.getInstance(imageData);
有了这个 img
实例后,您可以像这样将它添加到文档中:
document.add(img);
我不明白你为什么要创建一个 ImageIcon
实例。也不清楚您为什么引用 Paragraph
对象。
更新 1:
现在我看到了你的完整代码,我看到了一个非常奇怪的行:
Image i = Image.getInstance((PdfTemplate) lblimg.getIcon());
您正在将 javax.swing
对象转换为 iText 对象。这永远行不通。此时您的代码中应该会得到一个 ClassCastException
。
我还看到您知道如何从文件添加图像:
Image img = Image.getInstance("ja.png");
doc.add(img);
当您没有文件路径时,找到替代 getInstance()
方法的最快方法是查阅 Javadoc API 文档:http://api.itextpdf.com/itext/com/itextpdf/text/Image.html#getInstance(byte[])
更新二:
我已更新问题,使其包含相关代码。正如我在回答中所解释的(不幸的是没有被接受),以下行抛出一个 ClassCastException
:
Image i = Image.getInstance((PdfTemplate) lblimg.getIcon());
这个异常是这样捕获的:
} catch(Exception e1) {
e1.printStackTrace();
}
因此跳过所有以下行开头的代码:
Document doc = new Document();
因此,没有创建文档。这不是 iText 问题。这是异常处理不当的情况。
更新 3:
最后,真正的问题在评论中提出:简单来说:如何检索 JLabel 中的图像并将其添加到 PDF 中?
再次证明我已经回答了那个问题。我参考了 Javadoc API 文档 Image class. We find the following getInstance()
method: http://api.itextpdf.com/itext/com/itextpdf/text/Image.html#getInstance(java.awt.Image, java.awt.Color)
换句话说,我们可以使用 Java Image
对象创建一个 iText Image
对象。您的代码中有以下行:
ImageIcon format = new ImageIcon(imageData);
或者,对于您的情况,您可以尝试类似的操作:
ImageIcon format = (ImageIcon)lblimg.getIcon();
你可以从这个 ImageIcon
like this:
得到一个 java.awt.Image
对象
java.awt.Image awtImage = format.getImage();
根据 iText API 文档,您可以像这样创建 iText 图像:
Image img = Image.getInstance(awtImage, null);
我想添加从 mysql 数据库中检索到的图像,并将其打印在 iText java 中的 pdf 文件中。从数据库中检索到的图像存储在 lblimg 中。我如何在 java 中实现这一目标? 这是我的部分代码:
String filename = null;
int s = 0;
byte[] person_img = null;
uploadbtn = new JButton("Upload a Photo");
uploadbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
try{
File img = new File(filename);
FileInputStream fis = new FileInputStream(img);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for(int readNum; (readNum = fis.read(buf)) != -1;){
bos.write(buf, 0, readNum);
}
person_img = bos.toByteArray();
fis.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
});
// Partial code for adding image to db
stt.setBytes(8, person_img);
// Partial codes for retrieving image from db
byte[] imageData = rs.getBytes("Image");
format = new ImageIcon(imageData);
lblimg.setIcon(format);
//Creating the document and adding the lblimg (which contains the image retrieved from the db). PLEASE HELP HERE. I CANNOT ADD THE IMAGE TO PDF document.
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("Report.pdf"));
doc.open();
doc.add(new Paragraph( // img to be added here ));
Bruno Lowagie 更新 1
摘自完整代码的片段:
try {
Image i = Image.getInstance((PdfTemplate) lblimg.getIcon());
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("Report.pdf"));
doc.open();
Image img = Image.getInstance("ja.png");
doc.add(img);
doc.add(i);
doc.add(new Paragraph("Employee Information", FontFactory.getFont(FontFactory.TIMES_BOLD,18, Font.BOLD, BaseColor.RED)));
doc.add(new Paragraph("______________________________________________________________________________"));
doc.add(new Paragraph("Employee ID is " + val1));
doc.add(new Paragraph("First Name is " + val2 + "\t\t" + " Last Name is " + val3));
doc.add(new Paragraph("Job Position " + val4));
doc.add(new Paragraph("Allowances allowed " + val5));
doc.add(new Paragraph("Salary " + val10));
JOptionPane.showMessageDialog(null, "Report Saved");
doc.close();
} catch(Exception e1) {
e1.printStackTrace();
}
As getIcon
returns a javax.swing
class and as PdfTemplate
is an iText class extending the PdfContentByte
class 包含 ByteBuffer
的 PDF 语法,此处抛出 ClassCastException
:(PdfTemplate) lblimg.getIcon()
Bruno Lowagie 更新 2
实际问题已作为评论发布:如何检索 JLabel 中的图像并将其添加到 PDF 中?这个问题在我的答案的更新 3 中得到了回答。
假设这包含一张图片:
byte[] imageData = rs.getBytes("Image");
换句话说:假设 imageData
是有效的 JPEG、JPEG2000、GIF、PNG、BMP、WMF、TIFF 或 JBIG2 图像,那么您可以像这样创建一个 com.itextpdf.text.Image
对象:
Image img = Image.getInstance(imageData);
有了这个 img
实例后,您可以像这样将它添加到文档中:
document.add(img);
我不明白你为什么要创建一个 ImageIcon
实例。也不清楚您为什么引用 Paragraph
对象。
更新 1:
现在我看到了你的完整代码,我看到了一个非常奇怪的行:
Image i = Image.getInstance((PdfTemplate) lblimg.getIcon());
您正在将 javax.swing
对象转换为 iText 对象。这永远行不通。此时您的代码中应该会得到一个 ClassCastException
。
我还看到您知道如何从文件添加图像:
Image img = Image.getInstance("ja.png");
doc.add(img);
当您没有文件路径时,找到替代 getInstance()
方法的最快方法是查阅 Javadoc API 文档:http://api.itextpdf.com/itext/com/itextpdf/text/Image.html#getInstance(byte[])
更新二:
我已更新问题,使其包含相关代码。正如我在回答中所解释的(不幸的是没有被接受),以下行抛出一个 ClassCastException
:
Image i = Image.getInstance((PdfTemplate) lblimg.getIcon());
这个异常是这样捕获的:
} catch(Exception e1) {
e1.printStackTrace();
}
因此跳过所有以下行开头的代码:
Document doc = new Document();
因此,没有创建文档。这不是 iText 问题。这是异常处理不当的情况。
更新 3:
最后,真正的问题在评论中提出:简单来说:如何检索 JLabel 中的图像并将其添加到 PDF 中?
再次证明我已经回答了那个问题。我参考了 Javadoc API 文档 Image class. We find the following getInstance()
method: http://api.itextpdf.com/itext/com/itextpdf/text/Image.html#getInstance(java.awt.Image, java.awt.Color)
换句话说,我们可以使用 Java Image
对象创建一个 iText Image
对象。您的代码中有以下行:
ImageIcon format = new ImageIcon(imageData);
或者,对于您的情况,您可以尝试类似的操作:
ImageIcon format = (ImageIcon)lblimg.getIcon();
你可以从这个 ImageIcon
like this:
java.awt.Image
对象
java.awt.Image awtImage = format.getImage();
根据 iText API 文档,您可以像这样创建 iText 图像:
Image img = Image.getInstance(awtImage, null);