这是我将图像添加到 PDF 的代码片段:
PdfContentByte cbLogo = writer.getDirectContent();
if (instanceSettings.getDocumentHeaderImageLocation() != null) {
try {
String encodedString = instanceSettings.getDocumentHeaderImageLocation();
byte[] decodedBytes = Base64
.getDecoder()
.decode(encodedString);
Image imgLogo = Image.getInstance(decodedBytes);
imgLogo.scaleToFit(220f, 150f);
imgLogo.setAbsolutePosition((writer.getPageSize().getWidth() / 4) - (imgLogo.getScaledWidth() / 2),
writer.getPageSize().getHeight() - imgLogo.getScaledHeight() - 30);
cbLogo.addImage(imgLogo);
} catch (Exception e){
ErrorHandler.handle(e);
}
}
这是另一个:
Image qr = Image.getInstance(file.getAbsolutePath());
qr.scaleToFit(70f, 70f);
qr.setSpacingBefore(0);
qr.setSpacingAfter(0);
qr.setPaddingTop(0);
PdfPCell cell = new PdfPCell();
cell.setPadding(0f);
cell.setUseAscender(true);
cell.setUseDescender(true);
cell.addElement(qr);
图像类型:
import com.itextpdf.text.Image;
您可以使用此代码尝试一下。对我来说,两者都按预期工作,但正如我在评论中所说,我有时很难在 PDF 应用程序中查看它。据我所知,itext与它无关。