添加依赖

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

源码

// 定义全局的字体静态变量private static Font titlefont; //标题字体样式private static Font headfont;private static Font keyfont;private static Font textfont;// 最大宽度private static int maxWidth = 180;// 静态代码块static {try {// 不同字体(这里定义为同一种字体:包含不同字号、不同style)BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);titlefont = new Font(bfChinese, 8, Font.BOLD);headfont = new Font(bfChinese, 5, Font.NORMAL);keyfont = new Font(bfChinese, 6, Font.BOLD);textfont = new Font(bfChinese, 6, Font.NORMAL);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {try {// 1.新建document对象Document document = new Document(PageSize.B7);// 建立一个Document对象document.setPageCount(3);//设置生成pdf页数// 2.建立一个书写器(Writer)与document对象关联File file = new File("./uploads/PDFDemo.pdf");file.createNewFile();PdfWriter.getInstance(document, new FileOutputStream(file));// 3.打开文档document.open();//4.生成PDFgeneratePDF(document);
//            generatePDF(document);// 5.关闭文档document.close();} catch (Exception e) {e.printStackTrace();}}public static void generatePDF(Document document) throws Exception {//段落Paragraph paragraph = new Paragraph("税务机关(人员)涉嫌违纪违法问题线索移交单", titlefont);paragraph.setAlignment(1); //设置文字居中 0靠左 1,居中 2,靠右
//        paragraph.setIndentationLeft(12); //设置左缩进
//        paragraph.setIndentationRight(12); //设置右缩进
//        paragraph.setFirstLineIndent(24); //设置首行缩进
//        paragraph.setLeading(20f); //行间距
//        paragraph.setSpacingBefore(5f); //设置段落上空白
//        paragraph.setSpacingAfter(10f); //设置段落下空白Calendar cal = Calendar.getInstance();Date date = new Date();//现在的日期cal.setTime(date);Integer year = cal.get(Calendar.YEAR);//获取年Integer month = cal.get(Calendar.MONTH)+1;//获取月(月份从0开始,如果按照中国的习惯,需要加一)Integer day_moneth = cal.get(Calendar.DAY_OF_MONTH);//获取日(月中的某一天)
//        Integer day_week=cal.get(Calendar.DAY_OF_WEEK);//获取一周内的某一天String rq = "填表时间:" + year + "年" + month + "月" + day_moneth + "日";//段落Paragraph paragraph2 = new Paragraph(rq, headfont);paragraph2.setAlignment(2); //设置文字居中 0靠左 1,居中 2,靠右// 直线
//        Paragraph p1 = new Paragraph();
//        p1.add(new Chunk(new LineSeparator()));// 点线
//        Paragraph p2 = new Paragraph();
//        p2.add(new Chunk(new DottedLineSeparator()));//        // 超链接
//        Anchor anchor = new Anchor("baidu");
//        anchor.setReference("www.baidu.com");
//
//        // 定位
//        Anchor gotoP = new Anchor("goto");
//        gotoP.setReference("#top");// 添加图片
//        Image image = Image.getInstance("E:\\img.png");
//        image.setAlignment(Image.ALIGN_CENTER);
//        image.scalePercent(40); //依照比例缩放String nyr = "       年       月       日";// 表格PdfPTable table = createTable(new float[]{120, 120, 120, 120});table.addCell(createCell("案件名称", textfont, Element.ALIGN_CENTER));table.addCell(createCell("士大夫士大夫士大夫士大夫躲闪分撒地方夫士大夫", textfont, Element.ALIGN_LEFT));table.addCell(createCell("纳税人识别号", textfont, Element.ALIGN_CENTER));table.addCell(createCell("34563456345456456456", textfont, Element.ALIGN_LEFT));table.addCell(createCell("税收违法案件简要案情", textfont));table.addCell(createCell("受到了飞机撒地方决定是否时间梵净山佛教吉萨附件哦啊附近飞机撒放假发受到飓风等级沙发撒士大夫士大夫精神的风景哦啊是的房间哦手机发的胜多负少就是建瓯飞机上的飞机是", textfont,Element.ALIGN_LEFT,3));table.addCell(createCell("税务机关(人员)涉嫌违纪违法问题线索", textfont));table.addCell(createCell("受到了飞机撒地方决定是否时间梵净山佛教吉萨附件哦啊附近飞机撒放假发受到飓风等级沙发撒士大夫士大夫精神的风景哦啊是的房间哦手机发的胜多负少就是建瓯飞机上的飞机是", textfont,Element.ALIGN_LEFT,3));table.addCell(createCell("移交部门负责人意见", textfont));table.addCell(createCell(nyr, textfont,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM,3));table.addCell(createCell("分管领导意见", textfont));table.addCell(createCell(nyr, textfont,Element.ALIGN_RIGHT,Element.ALIGN_BOTTOM,3));//段落Paragraph paragraph3 = new Paragraph("注:本移交单一式二份,移交单位、接收单位各保存一份。移交相关资料份。", headfont);paragraph3.setAlignment(0); //设置文字居中 0靠左 1,居中 2,靠右paragraph3.setIndentationLeft(8); //设置左缩进Paragraph paragraph4 = new Paragraph("移交人:" + nyr + "     签收人:" + nyr, headfont);paragraph4.setAlignment(2); //设置文字居中 0靠左 1,居中 2,靠右//        Integer totalQuantity = 0;
//        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));
//            totalQuantity++;
//        }
//        table.addCell(createCell("总计", keyfont));
//        table.addCell(createCell("", textfont));
//        table.addCell(createCell("", textfont));
//        table.addCell(createCell("", textfont));
//        table.addCell(createCell(String.valueOf(totalQuantity) + "件事", textfont));
//        table.addCell(createCell("", textfont));document.add(paragraph);document.add(paragraph2);document.add(table);document.add(paragraph3);document.add(paragraph4);
//        document.add(p1);
//        document.add(p2);
//        document.add(image);}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;}/*** 创建单元格(指定字体、水平..)** @param value* @param font* @param align* @return*/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));cell.setMinimumHeight(26);return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并)** @param value* @param font* @param align* @param colspan* @return*/public static PdfPCell createCell(String value, Font font, int align, int colspan) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));cell.setMinimumHeight(50);return cell;}public static PdfPCell createCell(String value, Font font, int align, int align1, int colspan) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(align1);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));cell.setMinimumHeight(50);return cell;}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)** @param value* @param font* @param align* @param colspan* @param boderFlag* @return*/public static PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setColspan(colspan);cell.setPhrase(new Phrase(value, font));cell.setPadding(3.0f);if (!boderFlag) {cell.setBorder(0);cell.setPaddingTop(15.0f);cell.setPaddingBottom(8.0f);} else if (boderFlag) {cell.setBorder(0);cell.setPaddingTop(0.0f);cell.setPaddingBottom(15.0f);}return cell;}/*** 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距)** @param value* @param font* @param align* @param borderWidth* @param paddingSize* @param flag* @return*/public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {PdfPCell cell = new PdfPCell();cell.setVerticalAlignment(Element.ALIGN_MIDDLE);cell.setHorizontalAlignment(align);cell.setPhrase(new Phrase(value, font));cell.setBorderWidthLeft(borderWidth[0]);cell.setBorderWidthRight(borderWidth[1]);cell.setBorderWidthTop(borderWidth[2]);cell.setBorderWidthBottom(borderWidth[3]);cell.setPaddingTop(paddingSize[0]);cell.setPaddingBottom(paddingSize[1]);if (flag) {cell.setColspan(2);}return cell;}public PdfPTable createTable(int colNumber, int align) {PdfPTable table = new PdfPTable(colNumber);try {table.setTotalWidth(maxWidth);table.setLockedWidth(true);table.setHorizontalAlignment(align);table.getDefaultCell().setBorder(1);} catch (Exception e) {e.printStackTrace();}return table;}/*** 创建指定列宽、列数的表格** @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;}/*** 创建空白的表格** @return*/public PdfPTable createBlankTable() {PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(0);table.addCell(createCell("", keyfont));table.setSpacingAfter(20.0f);table.setSpacingBefore(20.0f);return table;}

END~

itextpdf创建pdf,开箱即用相关推荐

  1. java + jfreechart + itextpdf创建折线图饼图并导出为pdf

    一.添加需要的maven依赖 <!--用于生成pdf--> <dependency><groupId>com.itextpdf</groupId>< ...

  2. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  3. 使用itextpdf操作pdf

    使用maven引入jar <dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf ...

  4. 【Java中级篇】使用itextpdf生成PDF

    我们可以发现很多求职网站都会将我们录入的信息来生成一个PDF简历文件.所以我这里提供了用itextpdf生成的PDF的代码. 一.步骤 1.1.使用Adobe Acrobat Pro工具编辑PDF模板 ...

  5. java使用itextpdf生成PDF批量打印荣誉证书(指定位置输出文字)

    最近公司项目有个需求,批量打印荣誉证书,一开始尝试过传统的网络打印,控件打印,JS调用浏览器打印方法,遇到各种问题,比如定位不准,分页问题,​​缩放问题等.然后就自己研究,整理了一套打印方案,项目已测 ...

  6. Java 创建pdf

    一.常用根据模板创建pdf两种方式 (创建**.ftl模板**)文本复制到word -> 转html -> 写好模板参数 -> 转成.ftl (使用模板)获取.ftl文件 -> ...

  7. 创建pdf java 字体_如何使用自定义字体从servlet使用iText XMLWorker创建PDF?

    使用iText / XMLWorker示例(大多数是 this one),我可以轻松编写能够使用我自己的额外字体从HTML代码创建PDF文件的简单应用程序,但是只要尝试在某些Web服务代码中使用我的东 ...

  8. 使用itext创建PDF模板

    1.在pom.xml文件中添加itextpdf依赖 <dependency> <groupId>com.itextpdf</groupId> <artifac ...

  9. itextpdf代码生成pdf直接下载

    1.pom文件中引入 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> ...

最新文章

  1. SCOM 2012知识分享-21:无代理管理
  2. 【大白话系列】深入浅出Cleaner+虚引用完成堆外内存的回收
  3. Angular ngOnChanges hook学习笔记
  4. mac下的svn服务器建立
  5. 杭电2112(SPFA)
  6. java调用c 返回值_推荐--Linux平台下Java调用C函数(补充关于String类型函数返回值)...
  7. python机器学习库sklearn——集成方法(Bagging、Boosting、随机森林RF、AdaBoost、GBDT)
  8. 【OCP题库-12c】最新CUUG OCP 071考试题库(71题)
  9. 构建LALR(1)项目集族
  10. Windows UWF 实现系统重启还原(2021.11.02)
  11. SSN3SL16A 华为SDH全新原包装STM-16光接口板
  12. jstl.jar和standar.jar包下载
  13. workgroup无法访问.您可能没有权限 完美问答解
  14. macd底背离的python_java尝试编写macd,试验顶背离底背离
  15. ✨❤️CSDN标题党❤️,创意无极限,那不直接全网站都花的飞起?
  16. 用Python告诉你深圳房租有多高,做程序员真的能买得起嘛
  17. matlab画微分方程的矢量场图_一维波动方程数值解 Matlab 教程(从入门到出图)——3数值计算的Matlab实现...
  18. [CF850E]Random Elections
  19. vb.net操作数据库之ACCESS(2)
  20. Vue + RTP 视频实时监控

热门文章

  1. Thresh — 基于JS的Flutter动态化方案
  2. 史上最简单最全最基础入门到精通的opencv图像处理 第七课:图像阈值
  3. CSS3条件判断——@supports
  4. centos 7.9 yum 安装最新版docker与dnf安装docker compose v2
  5. 程序员千万别找同行做女朋友,因为一吵架代码就会被删除,痛苦啊
  6. UED使命召唤——沟通
  7. 不打扰、是我给你最后的温柔。
  8. golang win10 命令行 go get 安装包到src目录
  9. Lightroom 大使系列:用 Meg Loeks 捕捉怀旧之情
  10. 为硬件保留内存解决办法