需求: 生成的 pdf 要插入图片信息
/**
* @param image
* @param align_v 垂直 ALIGN_LEFT = 0; ALIGN_CENTER = 1;ALIGN_RIGHT = 2; ALIGN_TOP = 4;ALIGN_MIDDLE = 5;ALIGN_BOTTOM = 6;
* @param align_h 水平 ALIGN_LEFT = 0 ALIGN_CENTER = 1; ALIGN_RIGHT = 2;
* @param colspan 跨列
* @param rowspan 跨行
* @return
*/
public PdfPCell createCellNotBorder(BufferedImage bufferedImage, int align, int colspan, int rowspan) throws BadElementException, IOException {
Image image = Image.getInstance(bufferedImage, null);
image.setAlignment(Image.MIDDLE);
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
if (colspan !=0) {
cell.setColspan(colspan);
}
if (rowspan !=0) {
cell.setRowspan(rowspan);
}
cell.setBorder(0);
int height = (int) Math.ceil(bufferedImage.getHeight());
// 设置单元格高度
cell.setFixedHeight(height);
cell.setImage(image);
return cell;
}
正常单元格
com.itextpdf.text.pdf.PdfPTable
// 示例:
PdfPTable table = createTable(new float[] { 50,50});
table.addCell(createCellNotBorder(" 第一个单元格 ", textfont_H, 1));
table.addCell(createCellNotBorder(" 第二个单元格 ", textfont_H, 1));
table.addCell(createCellNotBorder(" 第三个单元格 ", textfont_H, 1));
table.addCell(createCellNotBorder(" 第四个单元格 ", textfont_H, 1));
/**
* 无边框
* @param value
* @param font
* @param align 水平 ALIGN_LEFT = 0 ALIGN_CENTER = 1; ALIGN_RIGHT = 2;
* @return
*/
public static PdfPCell createCellNotBorder(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
// cell.setPaddingTop(6.0F);
cell.setBorder(0);
return cell;
}
跨列示例
PdfPTable table = createTable(new float[] { 50,50});
table.addCell(createCellNotBorder("第一个单元格", textfont_H, 1));
// 跨一列
table.addCell(createCellNotBorder("第二个单元格", title1Font, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER , 1, 0));
table.addCell(createCellNotBorder("第三个单元格 ", textfont_H, 1));
/**
* 无边框
* @param value
* @param font
* @param align 水平 ALIGN_LEFT = 0 ALIGN_CENTER = 1; ALIGN_RIGHT = 2;
* @return
*/
public static PdfPCell createCellNotBorder(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
// cell.setPaddingTop(6.0F);
cell.setBorder(0);
return cell;
}
/**
* 无边框
* @param value
* @param font
* @param align_v 垂直 ALIGN_LEFT = 0; ALIGN_CENTER = 1;ALIGN_RIGHT = 2; ALIGN_TOP = 4;ALIGN_MIDDLE = 5;ALIGN_BOTTOM = 6;
* @param align_h 水平 ALIGN_LEFT = 0 ALIGN_CENTER = 1; ALIGN_RIGHT = 2;
* @param colspan 跨列
* @param rowspan 跨行
* @return
*/
public static PdfPCell createCellNotBorder(String value, Font font, int align_v,int align_h, int colspan, int rowspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(align_v);
cell.setHorizontalAlignment(align_h);
if (colspan !=0) {
cell.setColspan(colspan);
}
if (rowspan !=0) {
cell.setRowspan(rowspan);
}
cell.setPhrase(new Phrase(value, font));
cell.setBorder(0);
return cell;
}
生成二维码如下:
public BufferedImage creatCode(String contents, int width, int height) {
BufferedImage image = null;
Hashtable hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); //设置字符编码
hints.put(EncodeHintType.MARGIN, 1); //二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); // 1、读取文件转换为字节数组
image = MatrixToImageWriter.toBufferedImage(bitMatrix);
//转换成png格式的IO流
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
BufferedImage 转 com.itextpdf.text.Image
public PdfPCell createCellNotBorder(BufferedImage bufferedImage, int align, int colspan, int rowspan) throws BadElementException, IOException {
Image image = Image.getInstance(bufferedImage, null);
image.setAlignment(Image.MIDDLE);
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
if (colspan !=0) {
cell.setColspan(colspan);
}
if (rowspan !=0) {
cell.setRowspan(rowspan);
}
cell.setBorder(0);
int height = (int) Math.ceil(bufferedImage.getHeight());
// 设置单元格高度
cell.setFixedHeight(height);
cell.setImage(image);
return cell;
}