java 操作com.itextpdf.text.pdf.PdfPTable问题记录

需求: 生成的 pdf 要插入图片信息

1、插入图片的大小 是由单元格的高度决定,跟图片本身大小无关

	/**
	 * @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;
	}

 

2、 pdf 跨列 问题

正常单元格

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;
	}

3、 图片文件流如何转 com.itextpdf.text.Image对象

生成二维码如下:


	public  BufferedImage creatCode(String contents, int width, int height) {
		BufferedImage image = null;
		Hashtable<EncodeHintType, java.io.Serializable> 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;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个使用 iTextPDF 5.5.10 导出 PDF 表格的示例工具类,其中包含支持单元格合并的方法: ```java import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class PDFExporter { public static void exportTableToPDF(String filePath, String[] header, String[][] data) { Document document = new Document(PageSize.A4.rotate()); try { PdfWriter.getInstance(document, new FileOutputStream(filePath)); document.open(); PdfPTable table = new PdfPTable(header.length); table.setWidthPercentage(100); for (String column : header) { PdfPCell cell = new PdfPCell(); cell.setPhrase(new com.itextpdf.text.Paragraph(column)); table.addCell(cell); } for (int row = 0; row < data.length; row++) { for (int column = 0; column < data[row].length; column++) { PdfPCell cell = new PdfPCell(); cell.setPhrase(new com.itextpdf.text.Paragraph(data[row][column])); table.addCell(cell); } } document.add(table); } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { document.close(); } } public static void mergeCells(PdfPTable table, int row1, int col1, int row2, int col2) { for (int row = row1; row <= row2; row++) { for (int col = col1; col <= col2; col++) { if (row == row1 && col == col1) { continue; } PdfPCell cell = table.getRow(row).getCells()[col]; cell.setPhrase(null); cell.setPadding(0); cell.setBorder(PdfPCell.NO_BORDER); } } PdfPCell cell = table.getRow(row1).getCells()[col1]; cell.setRowspan(row2 - row1 + 1); cell.setColspan(col2 - col1 + 1); } } ``` 调用 `exportTableToPDF` 方法可以将一个二维字符串数组导出为一个 PDF 表格文件,其中第一个参数是文件路径,第二个参数是表头数组,第三个参数是数据数组。 调用 `mergeCells` 方法可以将表格中的多个单元格合并为一个单元格,其中第一个参数是目标表格,第二个参数是起始行号,第三个参数是起始列号,第四个参数是结束行号,第五个参数是结束列号。 例如,如果要将第 2 行第 3、4、5 列合并为一个单元格,可以这样调用 `mergeCells` 方法: ```java PDFExporter.mergeCells(table, 1, 2, 1, 4); ``` 其中 `table` 是导出 PDF 表格的 `PdfPTable` 对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值