当前位置: 首页 > news >正文

java生成pdf

1.pdf预览

2.maven

        <!--pdf--><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.9</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

3.代码

package mis.shared.file;import com.bestvike.linq.Linq;
import com.itextpdf.text.*;
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.LineSeparator;import java.io.FileOutputStream;/*** pdf工具类*/
public class PdfUtil {public static void main(String[] args) throws Exception {Demo();}public static void Demo() throws Exception {Document document = new Document(PageSize.A4, 50, 50, 30, 30); // 指定页面大小为A4,且自定义页边距(marginLeft、marginRight、marginTop、marginBottom)PdfWriter.getInstance(document, new FileOutputStream("E:\\develop\\tmp\\test" + System.currentTimeMillis() + ".pdf"));document.open();BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);Font keyfont = new Font(bfChinese, 10, Font.BOLD);Font textfont = new Font(bfChinese, 10, Font.NORMAL);Paragraph wrap = new Paragraph("\r\n", textfont);// 段落Paragraph paragraph = new Paragraph("知之愈明,则行之愈笃;行之愈笃,则知之益明。——朱熹(宋)", textfont);// 直线Paragraph line = new Paragraph();line.add(new Chunk(new LineSeparator()));// 超链接Anchor link = new Anchor("点击跳转到百度", textfont);link.setReference("www.baidu.com");// 添加图片Image image = Image.getInstance("E:\\develop\\tmp\\install.png");image.setAlignment(Image.ALIGN_CENTER);image.scalePercent(40); //依照比例缩放// 表格PdfPTable table = createTable(new float[]{80, 80, 80, 80, 80, 80});table.addCell(createCell("早上9:00", keyfont, Element.ALIGN_CENTER));table.addCell(createCell("中午11:00", keyfont, Element.ALIGN_CENTER));table.addCell(createCell("中午13:00", keyfont, Element.ALIGN_CENTER));table.addCell(createCell("下午15:00", keyfont, Element.ALIGN_CENTER));table.addCell(createCell("下午17:00", keyfont, Element.ALIGN_CENTER));table.addCell(createCell("晚上19:00", keyfont, Element.ALIGN_CENTER));for (int i = 0; i < 5; i++) {table.addCell(createCell("起床", textfont));table.addCell(createCell("吃午饭", textfont));table.addCell(createCell("午休", textfont));table.addCell(createCell("下午茶", textfont));table.addCell(createCell("回家", textfont));table.addCell(createCell("吃晚饭", textfont));}//最后一行table.addCell(createCell("总计", textfont, Element.ALIGN_CENTER));// 处理最后一行边框合并PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setBorderWidthTop(0);cell.setBorderWidthLeft(0);cell.setColspan(5);cell.setPhrase(new Phrase("5天", textfont));table.addCell(cell);// 开始从内存写入到磁盘 pdf文件中document.add(paragraph);document.add(line);document.add(link);document.add(wrap);document.add(table);document.add(image);document.close();}public static PdfPTable createTable(float[] widths) {PdfPTable table = new PdfPTable(widths);try {table.setTotalWidth(Linq.of(widths).sumFloat());table.setLockedWidth(true);table.setHorizontalAlignment(Element.ALIGN_CENTER);table.getDefaultCell().setBorder(1);} catch (Exception e) {e.printStackTrace();}return table;}public static PdfPCell createCell(String value, Font font) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(Element.ALIGN_CENTER);cell.setPhrase(new Phrase(value, font));return cell;}public static PdfPCell createCell(String value, Font font, int align) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value, font));return cell;}}

http://www.lryc.cn/news/301916.html

相关文章:

  • 鸿蒙应用/元服务开发-窗口概述
  • 引入成熟的Pytest自动化测试框架
  • 学习总结18
  • SpringMVC的执行流程
  • Vue项目启动过程全记录(node.js运行环境搭建)
  • Linux下如何配置环境变量
  • PyCharm 主题和字体 (Scheme Editor Font)
  • 二叉树相关OJ题
  • 文物保护系统守护历史岁月,成都青铜展科技闪耀
  • [计算机网络]---Http协议
  • Hexo删除主题
  • RK3399平台开发系列讲解(USB篇)U盘等存储类设备
  • 一个页面需要加载大量的图片,如何提升用户体验?
  • JRT监听-PDF-Excel-Img
  • Pulsar-架构与设计
  • LeetCode每日一题589. N-ary Tree Preorder Traversal
  • html5移动端适配;检测浏览器信息函数
  • go依赖注入库samber/do使用
  • JMeter 配置元件之按条件读取CSV Data Set Config
  • MySQL跨服务器关联查询
  • 分库分表浅析
  • java 宠物医院系统Myeclipse开发mysql数据库web结构jsp编程计算机网页项目
  • XMall 开源商城 SQL注入漏洞复现(CVE-2024-24112)
  • Docker原理及概念相关
  • Vim相关配置
  • ARMv8-AArch64 的异常处理模型详解之异常处理详解(进入异常以及异常路由)
  • unity学习(19)——客户端与服务器合力完成注册功能(1)入门准备
  • 论文精读--对比学习论文综述
  • 文章复现 | 差异分析和PPI网络构建
  • 入门级10寸加固行业平板—EM-I10J