首页 > 其他分享 >itextpdf导出

itextpdf导出

时间:2024-08-29 23:15:01浏览次数:5  
标签:import ALIGN 导出 itextpdf Element new table document

   <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
import com.itextpdf.text.Anchor;
		import com.itextpdf.text.Chunk;
		import com.itextpdf.text.Document;
		import com.itextpdf.text.Element;
		import com.itextpdf.text.Font;
		import com.itextpdf.text.Image;
		import com.itextpdf.text.PageSize;
		import com.itextpdf.text.Paragraph;
		import com.itextpdf.text.Phrase;
		import com.itextpdf.text.pdf.BaseFont;
		import com.itextpdf.text.pdf.PdfPCell;
		import com.itextpdf.text.pdf.PdfPTable;
		import com.itextpdf.text.pdf.PdfWriter;
		import com.itextpdf.text.pdf.draw.DottedLineSeparator;
		import com.itextpdf.text.pdf.draw.LineSeparator;

		import java.io.File;
		import java.io.FileOutputStream;

		/**
		 * 生成pdf
		 * <p>
		 * 参考: https://blog.csdn.net/weixin_37848710/article/details/89522862?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
		 * https://blog.csdn.net/qq_39207908/article/details/82423388
		 */
		public class PdfExport {

			public static void main(String[] args) {
				try {
					// document
					Document document = new Document(PageSize.A4);

					// OutputStream
					File file = new File("D:\\Test.pdf");
					file.createNewFile();
					FileOutputStream fos = new FileOutputStream(file);

					PdfWriter writer = PdfWriter.getInstance(document, fos);

					// 水印
					writer.setPageEvent(new Watermark("HELLO ITEXTPDF"));

					// 页眉 和 页脚
					writer.setPageEvent(new MyHeaderFooter());

					// 打开 pdf
					document.open();

					/** 向 pdf 中写入内容 *** start ******************************************************/
					// 1. 段落
					Paragraph paragraph_1 = new Paragraph("HELLO PDF!", titlefont);
					// 文字位置(靠左,居中,靠右 等)
					paragraph_1.setAlignment(Element.ALIGN_CENTER);
					// 左缩进
					paragraph_1.setIndentationLeft(12);
					// 右缩进
					paragraph_1.setIndentationRight(12);
					// 首行缩进
					paragraph_1.setFirstLineIndent(24);
					// 行间距
					paragraph_1.setLeading(20f);
					// 段落上空白
					paragraph_1.setSpacingBefore(5f);
					// 段落下空白
					paragraph_1.setSpacingAfter(10f);

					// 2. 直线
					Paragraph paragraph_2 = new Paragraph();
					paragraph_2.add(new Chunk(new LineSeparator()));

					// 3. 点线
					Paragraph paragraph_3 = new Paragraph();
					paragraph_3.add(new Chunk(new DottedLineSeparator()));

					// 4. 超链接
					Anchor link = new Anchor("baidu");
					link.setReference("www.baidu.com");

					// 5. 定位,如: 跳转到文档头部位置(测试发现: 使用浏览器时可以正常跳转,使用 wps 时不可以)
					Anchor goTop = new Anchor("goTop");
					goTop.setReference("#top");

					// 6. 图片(图片地址若是以 http 开头而非 https 开头,可能会报错)
					Image image = Image.getInstance("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1200524016,3386333024&fm=26&gp=0.jpg");
					image.setAlignment(Image.ALIGN_CENTER);
					image.scalePercent(40);

					// 表格
					PdfPTable table_1 = createTable(new float[]{50, 50});

					table_1.addCell(createCell("TEST", textfont, Element.ALIGN_LEFT));
					table_1.addCell(createCell("TEST", textfont, Element.ALIGN_LEFT));

					table_1.addCell(createCell("测试", textfont, Element.ALIGN_LEFT));
					table_1.addCell(createCell("测试", textfont, Element.ALIGN_LEFT));

					PdfPTable table_2 = createTable(new float[]{100});

					table_2.addCell(createCell("TEST", textfont, Element.ALIGN_LEFT));

					table_2.addCell(createCell("测试", textfont, Element.ALIGN_LEFT));


					// 直线
					Paragraph paragraph_4 = new Paragraph();
					paragraph_4.add(new Chunk(new LineSeparator()));
					// 行间距
					paragraph_4.setLeading(20f);
					// 段落上空白
					paragraph_4.setSpacingBefore(50f);
					// 段落下空白
					paragraph_4.setSpacingAfter(50f);

					// 合并单元格
					PdfPTable table_3 = createTable(new float[]{50, 50, 50, 50});

					table_3.addCell(createCellWithSpan("HEAD", textfont, Element.ALIGN_CENTER, 1, 4));

					table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("AA", textfont, Element.ALIGN_LEFT));

					table_3.addCell(createCellWithSpan("SPAN", textfont, Element.ALIGN_LEFT, 2, 2));
					table_3.addCell(createCell("BB", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("BB", textfont, Element.ALIGN_LEFT));

					table_3.addCell(createCell("CC", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("CC", textfont, Element.ALIGN_LEFT));

					table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("EE", textfont, Element.ALIGN_LEFT));

					table_3.addCell(createCell("FF", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCellForImage(image, textfont, Element.ALIGN_LEFT, 2, 2));
					table_3.addCell(createCell("FF", textfont, Element.ALIGN_LEFT));

					table_3.addCell(createCell("GG", textfont, Element.ALIGN_LEFT));
					table_3.addCell(createCell("GG", textfont, Element.ALIGN_LEFT));
					
					// 补充: table.setHeaderRows(2); 指定前 2 行作为标题行,作用: 使得当 table 由于分页被分割时,可以在每页都显示标题行

					document.add(paragraph_1);
					document.add(paragraph_2);
					document.add(paragraph_3);
					document.add(link);
					document.add(image);
					document.add(table_1);
					document.add(table_2);
					document.add(paragraph_4);
					document.add(table_3);
					document.add(goTop);

					/** 向 pdf 中写入内容 *** end ******************************************************/

					// 关闭 pdf
					document.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			// 定义全局的字体静态变量
			private static Font titlefont;
			private static Font headfont;
			private static Font keyfont;
			private static Font textfont;

			// 最大宽度
			private static int maxWidth = 520;

			// 字体
			static {
				try {
					BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
					titlefont = new Font(bfChinese, 16, Font.BOLD);
					headfont = new Font(bfChinese, 14, Font.BOLD);
					keyfont = new Font(bfChinese, 10, Font.BOLD);
					textfont = new Font(bfChinese, 10, Font.NORMAL);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}


			/**
			 * 创建指定 列宽、列数 的表格
			 *
			 * @param widths (列数: 数组元素的个数)
			 * @return
			 */
			public static PdfPTable createTable(float[] widths) {
				PdfPTable table = new PdfPTable(widths);
				try {
					table.setTotalWidth(maxWidth);
					table.setLockedWidth(true);
					table.setHorizontalAlignment(Element.ALIGN_CENTER);
					table.getDefaultCell().setBorder(1);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return table;
			}

			/**
			 * 创建单元格
			 *
			 * @param value               值
			 * @param font                字体
			 * @param horizontalAlignment 水平位置
			 * @return
			 */
			public static PdfPCell createCell(String value, Font font, int horizontalAlignment) {
				PdfPCell cell = new PdfPCell();
				cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cell.setHorizontalAlignment(horizontalAlignment);
				// 建议不设置单元格固定高度,因为这会导致单元格中的内容超出单元格的所能容纳的字数时,使得部分内容丢失
				//cell.setFixedHeight(30);
				cell.setPhrase(new Phrase(value, font));
				return cell;
			}

			/**
			 * 创建单元格
			 *
			 * @param value               值
			 * @param font                字体
			 * @param horizontalAlignment 水平位置
			 * @param rowspan             合并行数
			 * @param colspan             合并列数
			 * @return
			 */
			public static PdfPCell createCellWithSpan(String value, Font font, int horizontalAlignment, int rowspan, int colspan) {
				PdfPCell cell = new PdfPCell();
				cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cell.setHorizontalAlignment(horizontalAlignment);
				cell.setRowspan(rowspan);
				cell.setColspan(colspan);
				cell.setPhrase(new Phrase(value, font));
				return cell;
			}

			/**
			 * 创建单元格(含图片)
			 *
			 * @param image
			 * @param font
			 * @param horizontalAlignment
			 * @param colspan
			 * @param rowspan
			 * @return
			 */
			public static PdfPCell createCellForImage(Image image, Font font, int horizontalAlignment, int colspan, int rowspan) {
				PdfPCell cell = new PdfPCell();
				cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cell.setHorizontalAlignment(horizontalAlignment);
				cell.setColspan(colspan);
				cell.setRowspan(rowspan);
				cell.setImage(image);
				return cell;
			}

		}
点击查看代码
import com.itextpdf.text.*;
		import com.itextpdf.text.pdf.*;

		import java.io.IOException;

		public class MyHeaderFooter extends PdfPageEventHelper {
			/**
			 * 总页数
			 */
			PdfTemplate totalPage;
			Font hfFont;

			{
				try {
					hfFont = new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.NORMAL);
				} catch (DocumentException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			/**
			 * 打开文档时,创建一个总页数的模版
			 */
			@Override
			public void onOpenDocument(PdfWriter writer, Document document) {
				PdfContentByte cb = writer.getDirectContent();
				totalPage = cb.createTemplate(30, 15);
			}

			/**
			 * 一页加载完成触发,写入页眉或页脚
			 */
			@Override
			public void onEndPage(PdfWriter writer, Document document) {
				PdfPTable table = new PdfPTable(2);
				try {
					table.setTotalWidth(PageSize.A4.getWidth());
					table.setWidths(new int[]{(int) (PageSize.A4.getWidth() / 2), (int) (PageSize.A4.getWidth() / 2)});
					table.setLockedWidth(true);

					// 第几页
					PdfPCell cell_1 = new PdfPCell(new Paragraph("第" + writer.getPageNumber() + "页/", hfFont));
					cell_1.setHorizontalAlignment(Element.ALIGN_RIGHT);
					cell_1.setBorder(Rectangle.NO_BORDER);
					table.addCell(cell_1);

					// 总页数
					PdfPCell cell_2 = new PdfPCell(Image.getInstance(totalPage));
					cell_2.setBorder(Rectangle.NO_BORDER);
					table.addCell(cell_2);

					// 将 页眉/页脚 写到指定位置,指定到上面就是页眉,指定到下面就是页脚
					table.writeSelectedRows(0, -1, 0, 20, writer.getDirectContent());

				} catch (Exception de) {
					throw new ExceptionConverter(de);
				}
			}

			/**
			 * 全部完成后,将总页数的 pdf 模版写到指定位置
			 */
			@Override
			public void onCloseDocument(PdfWriter writer, Document document) {
				String text = "总" + (writer.getPageNumber()) + "页";
				ColumnText.showTextAligned(totalPage, Element.ALIGN_LEFT, new Paragraph(text, hfFont), 0, 5, 0);
			}

		}
点击查看代码
import com.itextpdf.text.Document;
		import com.itextpdf.text.PageSize;
		import com.itextpdf.text.Paragraph;
		import com.itextpdf.text.pdf.PdfWriter;
		import org.springframework.stereotype.Controller;
		import org.springframework.web.bind.annotation.GetMapping;

		import javax.servlet.http.HttpServletResponse;
		import java.io.ByteArrayOutputStream;
		import java.io.OutputStream;

		/**
		 * pdf 下载
		 */
		@Controller
		public class PdfController {

			@GetMapping("/downloadPdf")
			public void downloadPdf(HttpServletResponse response) {
				try {
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					Document document = new Document(PageSize.A4);

					PdfWriter.getInstance(document, baos);

					// 打开 pdf
					document.open();

					// 向 pdf 写入内容
					Paragraph paragraph = new Paragraph("hello world");
					document.add(paragraph);

					// 关闭 pdf
					document.close();

					String fileName = "MyPdfTest";
					response.setContentType("application/pdf");
					response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
					response.setContentLength(baos.size());
					OutputStream out = response.getOutputStream();
					baos.writeTo(out);
					out.flush();
					out.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
点击查看代码

			^
			|
			|
			Y
			|
			+----------+
			|          |
			|   pdf    |
			|          |
			|  文 件   |
			|          |
			|          |
		 ---+----------+----X-->
		   O|
			|
			|

		// 测试
			public static void main(String[] args) {
			
				Document document = new Document(PageSize.A4);

				System.err.println("rectangle:" + document.getPageSize());
				// 顶部 y 坐标
				System.err.println("top:" + document.getPageSize().getTop());
				// 底部 y 坐标
				System.err.println("bottom:" + document.getPageSize().getBottom());
				// 最左侧 x 坐标
				System.err.println("left:" + document.getPageSize().getLeft());
				// 最右侧 x 坐标
				System.err.println("right:" + document.getPageSize().getRight());
			}

标签:import,ALIGN,导出,itextpdf,Element,new,table,document
From: https://www.cnblogs.com/SetariaViridis/p/18387700

相关文章

  • Dell服务器导出日志
    iDRAC接口为网口准备工作:注:此文档适用于iDRAC接口为网口且与电源接口同侧的Dell服务器,如:R740带网口的笔记本电脑网线跳线操作方法:网线一端连接笔记本电脑,另一端连接服务器的iDRAC接口;查看笔记本电脑是否自动获取到192.168.0.*的ip地址,未获取到需设置同段的固定ip地址,如:1......
  • blender导出pmx文件导入UE5中无法展示贴图解决方案
    如题目所示,在进行pmx文件导入时出现贴图材质为白色材质球的情况,为解决此类问题,我将提供我的解决方案,供大家参考环境:blender3.6,Unrealengine5.4.2人物模型:宵宫 链接:https://www.aplaybox.com/details/model/Z0Im7XrRgmfYblender插件:cat-blender-plugin https......
  • java导出内存dump
    使用jhsdb的步骤1.查找进程ID(PID)首先,你需要获取目标Java进程的PID。如果你已经有了PID,就可以跳过这一步。如果没有,可以使用以下命令来查找:jps-l2.使用jhsdb查看堆信息例如,如果你的Java进程PID是2364,你可以使用以下命令来查看堆信息:jhsdbjmap--heap--pid2364......
  • 【Azure Developer】如何在Azure门户上把当前账号下的所有资源信息列举并导出呢?
    问题描述是否可以在Azure上,一次性把当前账号所有能查看的资源都导出来呢?包含资源名称,类型,定价层SKU信息呢? 问题解答是的,通过AzureResourceGraphExplorer服务可以实现。AzureResourceGraphExplorer是一个强大的工具,旨在提供跨订阅的资源查询和治理。它允许用户在任何......
  • 怎么快速把多个文件夹的照片批量导出到一个文件夹里?超级好用的三个方法
    在日常工作与生活中,我们经常会遇到需要整理大量照片的情况,尤其是当这些照片分散在多个文件夹中时,手动逐一复制粘贴不仅费时费力,还容易出错。幸运的是,现代科技提供了多种高效便捷的方法来应对这一问题。下面,我将详细介绍三种超级好用的方法,帮助你快速将多个文件夹的照片批量导出......
  • 关于java中Excel的导入导出
    前言提示:注意看文字的提醒:例如:提示:就这几个表别搞乱了就行其实很简单ExcelClassField------Excel标题栏工具类–不用管ExcelExport------导出配置工具类—用于对象表的配置上ExcelImport----导入配置工具类—用于对象表的配置上ExcelUtils-----用于接口调用上 一、配置pom......
  • Microsoft Word使用公式字体Latin Modern Math时导出pdf显示异常
    MicrosoftWord使用公式字体LatinModernMath时导出pdf显示异常参考资料1问题描述将Word公式字体修改为LatinModernMath,另存为pdf,导出的pdf文件中公式字体为位图而非矢量图,且部分符号可能缺失。2问题原因安装的字体LatinModernMath为otf文件而非ttf文件,Word无法将字体......
  • Java将数据导出为Excel文件
    使用ApachePOI生成基本ExcelApachePOI是一个强大的Java库,用来处理MicrosoftOffice文件。对于Excel文件(.xls和.xlsx)处理,提供有HSSF(.xls)和XSSF(.xlsx)等API。importorg.apache.poi.ss.usermodel.*;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importjavax.serv......
  • Java使用EasyExcel导出图片(原比例大小)到Excel中
    EasyExcel导出图片又开始写Excel导出的需求了,哈哈哈……目前的需求是将图表分析对的饼图和折线图,也就是一张完整的图片单独导出到Excel中为了方便客户在业务报告时,可以使用数据分析图片,从而更清晰准确地展示数据趋势因此关键点是将图片原比例尺寸大小导出,不能进行压缩原数......
  • GO语言从前端传来的表格数据和表格标题导出到Excel表格中
    提示:使用Go将前端表格数据和标题导出到Excel表格中,你可以使用第三方库,如"xlsx"。数据的操作流程遵循:分析数据格式→确定数据对象→解析→构建表格→生成文件。以下是一个简单的示例,展示了如何在Gin框架中实现该功能:文章目录一、表格样式二、使用步骤1.引入库2.创建路......