java生成pdf表格并插入图片

以下的方法只是一个模板,有些功能并没有完全覆盖。可根据自己的需求查询其他资料。

1.引入jar包或者pom依赖

	    <dependency>  
        	<groupId>com.itextpdf</groupId>  
         	<artifactId>itextpdf</artifactId>  
         	<version>5.5.10</version>  
        </dependency>
        <dependency>  
			<groupId>com.itextpdf</groupId>  
	    	<artifactId>itext-asian</artifactId>  
	    	<version>5.2.0</version>  
	    </dependency>

2.工具类 PDFCommon.java

package com.pdf.common;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;

public class PDFCommon {
	// 生成空表格
	public static PdfPCell getEmptyCell() throws Exception {
		PdfPCell pdfPCell = getBodyPdfPCell_center();
		pdfPCell.setPhrase(new Paragraph("  ", getPdfBodyFont()));
		return pdfPCell;
	}

	// 设置表体的表格样式 字体居右
	public static PdfPCell getBodyPdfPCell_right() {
		PdfPCell pdfPCell = new PdfPCell();
		pdfPCell.setMinimumHeight(12);
		pdfPCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		return pdfPCell;
	}

	// 设置表体的表格样式 字体居左
	public static PdfPCell getBodyPdfPCell_left() {
		PdfPCell pdfPCell = new PdfPCell();
		pdfPCell.setMinimumHeight(12);
		pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		return pdfPCell;
	}

	public static PdfPCell getBodyPdfPCell_image() {
		PdfPCell pdfPCell = new PdfPCell();
		pdfPCell.setMinimumHeight(40);
		pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		return pdfPCell;
	}

	// 设置表体的表格样式 字体居左
	public static PdfPCell getBodyPdfPCell_left_top() {
		PdfPCell pdfPCell = new PdfPCell();
		pdfPCell.setMinimumHeight(12);
		pdfPCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		pdfPCell.setVerticalAlignment(Element.ALIGN_TOP);
		return pdfPCell;
	}

	// 设置表体的表格样式 字体居中
	public static PdfPCell getBodyPdfPCell_center() {
		PdfPCell pdfPCell = new PdfPCell();
		pdfPCell.setMinimumHeight(12);
		pdfPCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		pdfPCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		return pdfPCell;
	}

	// 设置表头
	public static Font getPdfTitleFont() throws Exception {
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese = new Font(bfChinese, 14F, Font.NORMAL);
		fontChinese.setStyle("bold");
		return fontChinese;
	}

	// 设置表体
	public static Font getPdfBodyFont() throws Exception {
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese = new Font(bfChinese, 10F, Font.NORMAL);
		return fontChinese;
	}

	// 设置表体-字体加粗
	public static Font getPdfBodyFont_bold() throws Exception {
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese = new Font(bfChinese, 8.5F, Font.NORMAL);
		fontChinese.setStyle("bold");
		return fontChinese;
	}

	// 设置下划线 -> 内容下方
	public static Chunk setUnderlineBlank(String content, float thickness, float yPosition) throws Exception {
		Chunk chunk = new Chunk(content, PDFCommon.getPdfBodyFont());
		chunk.setUnderline(thickness, yPosition);
		return chunk;
	}

}

3.导出的实现方法

package com.pdf;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.pdf.common.PDFCommon;

public class ExportPDF {
	public void export(HttpServletResponse response, HttpServletRequest request) throws Exception {

		// 创建文件
		Document document = new Document();
		document.setPageSize(PageSize.A4); // 设置A4

		// 导出到浏览器
		// response.reset();
		// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		// String pdfFileName = "导出pdf-" + PDFCommon.getYYYYMMDD(nowDate);//文件的名称
		// response.setHeader("Content-Disposition",
		// "attachment;filename=" + URLEncoder.encode(pdfFileName, "UTF-8") + ".pdf");
		// // 为了解决在IE浏览器中汉字乱码的问题
		// response.setHeader("Last-Modified", new
		// SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		//
		// PdfWriter writer = PdfWriter.getInstance(document,
		// response.getOutputStream());
		// 导出到本地
		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File("D:/导出pdf.pdf")));
		// 设置页面布局
		writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
		// 打开文件
		document.open();

		float[] widths = { 125, 125, 125, 170, 125, 160, 125 };// 有几个表格
		PdfPTable table = new PdfPTable(widths);
		table.setWidthPercentage(95); // 设置表格占A4纸的95%

		// 设置表头
		PdfPCell total_1 = new PdfPCell();
		total_1.setRowspan(1);
		total_1.setColspan(10);
		total_1.setHorizontalAlignment(Element.ALIGN_CENTER);
		total_1.setVerticalAlignment(Element.ALIGN_MIDDLE);
		total_1.disableBorderSide(13);//消除周围的表格线
		// total_1.setBackgroundColor(BaseColor.YELLOW);
		Paragraph paragraph = new Paragraph("导出pdf的表格 \r\n", PDFCommon.getPdfTitleFont());
		total_1.setPhrase(paragraph);
		table.addCell(total_1);

		// 第1行
		PdfPCell total_1_1 = PDFCommon.getBodyPdfPCell_center();
		total_1_1.setRowspan(1);
		total_1_1.setColspan(1);
		total_1_1.setPhrase(new Paragraph("人员姓名", PDFCommon.getPdfBodyFont()));
		table.addCell(total_1_1);

		PdfPCell total_1_2 = PDFCommon.getBodyPdfPCell_center();
		total_1_2.setRowspan(1);
		total_1_2.setColspan(1);
		total_1_2.setPhrase(new Paragraph("张三", PDFCommon.getPdfBodyFont()));
		table.addCell(total_1_2);

		PdfPCell total_1_3 = PDFCommon.getBodyPdfPCell_center();
		total_1_3.setRowspan(1);
		total_1_3.setColspan(1);
		total_1_3.setPhrase(new Paragraph("身份证号码", PDFCommon.getPdfBodyFont()));
		table.addCell(total_1_3);

		PdfPCell total_1_4 = PDFCommon.getBodyPdfPCell_center();
		total_1_4.setRowspan(1);
		total_1_4.setColspan(1);
		total_1_4.setPhrase(new Paragraph("100002100110121211", PDFCommon.getPdfBodyFont()));
		table.addCell(total_1_4);

		PdfPCell total_1_5 = PDFCommon.getBodyPdfPCell_center();
		total_1_5.setRowspan(1);
		total_1_5.setColspan(1);
		total_1_5.setPhrase(new Paragraph("户籍地址", PDFCommon.getPdfBodyFont()));
		table.addCell(total_1_5);

		PdfPCell total_1_67 = PDFCommon.getBodyPdfPCell_center();
		total_1_67.setRowspan(1);
		total_1_67.setColspan(2);
		total_1_67.setPhrase(new Paragraph("北京市金融街", PDFCommon.getPdfBodyFont()));
		table.addCell(total_1_67);

		for (int q = 0; q < 8; q++) {// 画8个大表格

			PdfPCell total_Q = PDFCommon.getBodyPdfPCell_center();
			total_Q.setRowspan(3);// 每一个大表格里有三个小表格
			total_Q.setColspan(2);
			total_Q.setPhrase(new Paragraph("大表格" + (q + 1), PDFCommon.getPdfBodyFont()));
			table.addCell(total_Q);

			for (int c = 0; c < 3; c++) {
				PdfPCell total_Q_d = PDFCommon.getBodyPdfPCell_left();
				total_Q_d.setRowspan(1);
				total_Q_d.setColspan(4);
				total_Q_d.setPhrase(new Paragraph("小表格" + (c + 1), PDFCommon.getPdfBodyFont()));
				table.addCell(total_Q_d);
				if (c == 0) {//第一列进行合并
					PdfPCell total_Q_score = PDFCommon.getBodyPdfPCell_center();
					total_Q_score.setRowspan(3);
					total_Q_score.setColspan(1);
					Chunk sigUnderline = new Chunk("  " + 5 + "  ");
					sigUnderline.setUnderline(0.1f, -2f);// 设置下划线
					Paragraph pp = new Paragraph("", PDFCommon.getPdfBodyFont());
					pp.add(sigUnderline);
					pp.add("分");
					total_Q_score.setPhrase(pp);
					table.addCell(total_Q_score);
				}
			}
		}
		// 总分
		PdfPCell total_ht = PDFCommon.getBodyPdfPCell_center();
		total_ht.setRowspan(1);
		total_ht.setColspan(6);
		total_ht.setPhrase(new Paragraph("合计", PDFCommon.getPdfBodyFont()));
		total_ht.setMinimumHeight(16F);
		table.addCell(total_ht);

		PdfPCell total_h = PDFCommon.getBodyPdfPCell_center();
		total_h.setRowspan(1);
		total_h.setColspan(1);
		Chunk sigUnderline = new Chunk("  " + 100 + "  ");
		sigUnderline.setUnderline(0.1f, -2f);// 设置下划线
		Paragraph pp = new Paragraph("", PDFCommon.getPdfBodyFont());
		pp.add(sigUnderline);
		pp.add("分");
		total_h.setPhrase(pp);
		table.addCell(total_h);

		// 大专家意见
		PdfPCell total_kb = PDFCommon.getBodyPdfPCell_center();
		total_kb.setRowspan(1);
		total_kb.setColspan(7);
		total_kb.disableBorderSide(2);
		total_kb.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
		table.addCell(total_kb);

		PdfPCell total_zj = PDFCommon.getBodyPdfPCell_left_top();
		total_zj.setRowspan(3);
		total_zj.setColspan(2);
		total_zj.disableBorderSide(9);
		total_zj.setPhrase(new Paragraph(" 大专家:", PDFCommon.getPdfBodyFont()));
		table.addCell(total_zj);

		PdfPCell total_zj_1 = PDFCommon.getBodyPdfPCell_left();
		total_zj_1.setRowspan(1);
		total_zj_1.setColspan(5);
		total_zj_1.disableBorderSide(7);
		total_zj_1.setPhrase(new Paragraph("  ", PDFCommon.getPdfBodyFont()));
		table.addCell(total_zj_1);
		// 专家签字图片
		Image zjImage = loadingPicture();
		PdfPCell imCell = PDFCommon.getBodyPdfPCell_image();
		imCell.setPhrase(new Paragraph(new Chunk(zjImage, 0, 0)));
		imCell.setHorizontalAlignment(Element.ALIGN_LEFT);
		imCell.setRowspan(1);
		imCell.setColspan(5);
		imCell.disableBorderSide(7);
		table.addCell(imCell);

		// 专家评估日期
		PdfPCell total_zjdate = PDFCommon.getBodyPdfPCell_right();
		total_zjdate.setRowspan(1);
		total_zjdate.setColspan(5);
		total_zjdate.disableBorderSide(5);
		total_zjdate.setPhrase(
				new Paragraph("第一个时间:   " + PDFCommon.getYYYYMMDD_f(new Date()), PDFCommon.getPdfBodyFont()));
		table.addCell(total_zjdate);

		// 集体评审
		PdfPCell total_jtyj_1 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_1.setRowspan(1);
		total_jtyj_1.setColspan(7);
		total_jtyj_1.disableBorderSide(2);
		total_jtyj_1.setPhrase(new Paragraph("小专家意见:   ", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_1);

		PdfPCell total_jtyj_2 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_2.setRowspan(1);
		total_jtyj_2.setColspan(7);
		total_jtyj_2.disableBorderSide(3);
		;
		total_jtyj_2.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_2);

		PdfPCell total_jtyj_3_1 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_3_1.setRowspan(1);
		total_jtyj_3_1.setColspan(2);
		total_jtyj_3_1.disableBorderSide(11);
		total_jtyj_3_1.setPhrase(new Paragraph(" ", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_3_1);

		PdfPCell total_jtyj_3_2 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_3_2.setRowspan(1);
		total_jtyj_3_2.setColspan(2);
		total_jtyj_3_2.disableBorderSide(15);
		total_jtyj_3_2.setPhrase(new Paragraph("通过  [ √ ]", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_3_2);

		PdfPCell total_jtyj_3_3 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_3_3.setRowspan(1);
		total_jtyj_3_3.setColspan(3);
		total_jtyj_3_3.disableBorderSide(7);
		total_jtyj_3_3.setPhrase(new Paragraph("不通过  [  ]", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_3_3);

		// 补充意见 头
		PdfPCell total_jtyj_4_1 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_4_1.setRowspan(1);
		total_jtyj_4_1.setColspan(7);
		total_jtyj_4_1.disableBorderSide(3);
		total_jtyj_4_1.setPhrase(new Paragraph("补充意见:", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_4_1);
		// 补充意见 体
		PdfPCell total_jtyj_5_1 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_5_1.setRowspan(1);
		total_jtyj_5_1.setColspan(7);
		total_jtyj_5_1.disableBorderSide(3);
		total_jtyj_5_1.setPhrase(
				new Paragraph(new Paragraph("             专家的意见是通过! \r\n \r\n ", PDFCommon.getPdfBodyFont())));
		table.addCell(total_jtyj_5_1);

		// 专家
		PdfPCell total_jtyj_6_1 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_6_1.setRowspan(1);
		total_jtyj_6_1.setColspan(2);
		total_jtyj_6_1.disableBorderSide(11);
		total_jtyj_6_1.setPhrase(new Paragraph("专家1: ", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_6_1);

		// 专家
		PdfPCell total_jtyj_6_2 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_6_2.setRowspan(1);
		total_jtyj_6_2.setColspan(2);
		total_jtyj_6_2.disableBorderSide(15);
		total_jtyj_6_2.setPhrase(new Paragraph("    专家2:", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_6_2);

		// 专家
		PdfPCell total_jtyj_6_3 = PDFCommon.getBodyPdfPCell_left();
		total_jtyj_6_3.setRowspan(1);
		total_jtyj_6_3.setColspan(3);
		total_jtyj_6_3.disableBorderSide(7);
		total_jtyj_6_3.setPhrase(new Paragraph("专家3:", PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtyj_6_3);

		// 图片1
		Image image_1 = loadingPicture();
		PdfPCell imCell_1 = PDFCommon.getBodyPdfPCell_image();
		imCell_1.setRowspan(1);
		imCell_1.setColspan(2);
		imCell_1.setPhrase(new Paragraph(new Chunk(image_1, 0, 0)));
		imCell_1.disableBorderSide(11);
		table.addCell(imCell_1);
		// 图片2
		Image image_2 = loadingPicture();
		PdfPCell imCell_2 = PDFCommon.getBodyPdfPCell_image();
		imCell_2.setRowspan(1);
		imCell_2.setColspan(2);
		imCell_2.setPhrase(new Paragraph(new Chunk(image_2, 0, 0)));
		imCell_2.disableBorderSide(15);
		table.addCell(imCell_2);
		// 图片3
		Image image_3 = loadingPicture();
		PdfPCell imCell_3 = PDFCommon.getBodyPdfPCell_image();
		imCell_3.setRowspan(1);
		imCell_3.setColspan(3);
		imCell_3.setPhrase(new Paragraph(new Chunk(image_3, 0, 0)));
		imCell_3.disableBorderSide(7);
		table.addCell(imCell_3);

		// 专家评估日期
		PdfPCell total_jtdate = PDFCommon.getBodyPdfPCell_right();
		total_jtdate.setRowspan(1);
		total_jtdate.setColspan(7);
		total_jtdate.disableBorderSide(1);
		total_jtdate
				.setPhrase(new Paragraph("第二个时间:   " + PDFCommon.getYYYYMMDD_f(new Date()), PDFCommon.getPdfBodyFont()));
		table.addCell(total_jtdate);

		document.add(table);
		document.close();
	}

	// 查询图片组装image
	private Image loadingPicture() throws BadElementException, MalformedURLException, IOException {
		File file = new File("E:/aa.png");
		byte[] by = File2byte(file);
		Image image = Image.getInstance(by);
		image.scaleAbsolute(60, 20);// 调整图片大小(宽度 高度)
		return image;
	}

	private byte[] File2byte(File tradeFile) {
		byte[] buffer = null;
		try {
			FileInputStream fis = new FileInputStream(tradeFile);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}


}

4.测试方法

	public static void main(String[] args) {
		ExportPDF exportPDF = new ExportPDF();
		try {
			exportPDF.export(null, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

5.导出的效果

 

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java中使用iText生成PDF并在页眉中插入图片的方法如下: 首先,你可以创建一个页眉页脚类,根据实际情况使用,如果不需要页眉页脚可以忽略这一步。通过参考中的链接可以了解更多关于页眉页脚带图片的详细信息。 接下来,你需要使用iText生成PDF。你可以通过参考中的链接找到一个示例代码,并在生成PDF的过程中插入图片。具体来说,在创建表格的过程中,你可以在表格的某一行中插入图片,例如在createHardwarePDF方法的第九行。 最后,你可以参考中的链接来了解更多关于JavaiText生成PDF插入图片的信息和效果演示。 这样,你就可以在Java中使用iText生成PDF并在页眉中插入图片了。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [itextpdf 使用之 html 转 pdf 页眉页脚带图片](https://download.csdn.net/download/z1353095373/85382971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [JavaItext生成Pdf,并给PdfCell添加图片](https://blog.csdn.net/qq_17847881/article/details/130180328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Itextpdf添加页眉页脚页码,页眉中需要添加logo图片](https://blog.csdn.net/qb170217/article/details/118718685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值