依赖

    <!-- 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>

代码

package com.example.pdfDemo;

import com.itextpdf.text.;
import com.itextpdf.text.pdf.
;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;

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

/**

  • @Description: 生成pdf

  • @Author ajin

  • @Version 1.0

  • @Tool: IntelliJ IDEA
    */
    public class GeneratePDF {
    // 定义全局的字体静态变量
    private static Font titlefont; //标题字体样式
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;
    // 最大宽度
    private static int maxWidth = 520;

    // 静态代码块
    static {
    try {
    // 不同字体(这里定义为同一种字体:包含不同字号、不同style)
    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();
    }
    }

    public static void main(String[] args) throws Exception {
    try {
    // 1.新建document对象
    Document document = new Document(PageSize.B7);// 建立一个Document对象
    document.setPageCount(5);//设置生成pdf页数
    // 2.建立一个书写器(Writer)与document对象关联
    File file = new File(“E:\PDFDemo.pdf”);
    file.createNewFile();
    PdfWriter.getInstance(document, new FileOutputStream(file));
    // 3.打开文档
    document.open();
    //4.生成PDF
    new GeneratePDF().generatePDF(document);
    new GeneratePDF().generatePDF(document);
    // 5.关闭文档
    document.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    // 生成PDF文件
    public 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); //设置段落下空白
    // 直线
    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); //依照比例缩放// 表格PdfPTable table = createTable(new float[]{40, 120, 120, 120, 80, 80});table.addCell(createCell("美好的一天", headfont, Element.ALIGN_LEFT, 6, false));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));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(p1);document.add(image);
    

    }

/------------------------创建表格单元格的方法start----------------------------*/
/

* 创建单元格(指定字体)
*
* @param value
* @param font
* @return
*/
public 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 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;
}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并)** @param value* @param font* @param align* @param colspan* @return*/
public 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));return cell;
}/*** 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)** @param value* @param font* @param align* @param colspan* @param boderFlag* @return*/
public 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;
}

/**------------------------创建表格单元格的方法end----------------------------*/

/--------------------------创建表格的方法start------------------- ---------*/
/

* 创建默认列宽,指定列数、水平(居中、右、左)的表格
*
* @param colNumber
* @param align
* @return
*/
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 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------------------- ---------*/
}

如何在java中生成pdf相关推荐

  1. 如何在Java中生成比特币钱包地址

    让我们通过学习比特币(Bitcoin)如何实施该技术的各个方面来工作,好吗?该技术包括以下几个方面: 比特币地址bitcoin address是用来发送和接收比特币的. 交易transaction是比 ...

  2. java中生成pdf,插入图片,页眉、页脚、表格

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java中生成pdf,插入图片,页眉.页脚.表格 import com.lowagie.text.*; import com.lowagie.te ...

  3. java 合并pdf,如何在Java中合并PDF

    如何在Java中合并PDF 为小型企业和大型企业,保持 您的 重要 文件, 组织将提高您的工作流程,并成倍增长你的组织的工作效率. PDF文档由于其接受的输入格式的类型的安全性和灵活性,通常是共享大量 ...

  4. 如何在Java中生成特定范围内的随机整数?

    如何生成特定范围内的随机int数值? 我已经尝试了以下方法,但是这些方法不起作用: 尝试1: randomNum = minimum + (int)(Math.random() * maximum); ...

  5. java中生成pdf文件,java 中生成pdf 文件

    用的是 itext library , 生成一个hello world pdf 文档 /* * This code is part of the 'iText Tutorial'. * You can ...

  6. integer java 随机_如何在Java中生成随机BigInteger值?

    我需要生成0(包含)到n(包含)范围内任意大的随机整数.我最初的想法是调用nextDouble并乘以n,但一旦n大于253,结果将不再是均匀分布的. BigInteger具有以下构造函数: publi ...

  7. java地图代码_如何在Java中生成地图?

    为了在不使用矩阵的情况下生成这样的地图,我建议从中心图块开始,然后使用修改后的广度优先搜索算法向外填充地图.首先,我们需要一些比相邻瓷砖列表更好的东西.您可以简单地拥有四个变量,一个用于存储下一个图块 ...

  8. 只需3个步骤,轻松解决程序员在Java中生成、扫描二维码难题

    条形码包含有关产品或公司的信息,以机器可读的形式直观地表示.条码广泛用于跟踪货物和库存管理.我们可以在 WPF 应用程序中轻松生成各种类型的条码.二维码广泛用于分享重要信息.对于不同的要求,您可能希望 ...

  9. 条码控件Aspose.BarCode入门教程(7):如何在Java 中的 GS1-128 条码生成器

    Aspose.BarCode for .NET 是一个功能强大的API,可以从任意角度生成和识别多种图像类型的一维和二维条形码.开发人员可以轻松添加条形码生成和识别功能,以及在.NET应用程序中将生成 ...

最新文章

  1. 物联网安全只有最薄弱的环节才有保障
  2. Stixel_World(single)学习笔记
  3. python面向对象(2)—— 继承(3)
  4. powerdesigner 同步mysql 报错_Win7下PowerDesigner连接mysql,反向工程,导出差异脚本,或直接同步...
  5. Redis:12---有序集合对象
  6. linux CentOS7 erlang安装
  7. antd vue关闭模态对话框_Vue.extend 登录注册模态框
  8. (转)淘淘商城系列——初始SolrCloud
  9. Pytorch transforms.Resize()的简单用法
  10. 业务与技术相结合,双活体系支付架构建设
  11. C++杂记之this指针
  12. 队列服务(Queue)
  13. 【转】基于nginx + lua实现的反向代理动态更新
  14. Ajax中的 “success” 与 “error ”回调函数何时调用 ?
  15. 【MM系列】SAP MM物料账在制品承担差异功能及配置
  16. 华为防火墙应用层过滤--URL
  17. Deep learning based multi-scale channel compression feature surface defect detection system
  18. html表单点击变色如何实现,表单特效 鼠标经过或选中input变色
  19. java台球击球角度,台球瞄准方法--角度的计算----转
  20. 计算机职业资格证书的权威部门是哪个部门?

热门文章

  1. python 3d图形控件 可交互_python – 在PySide中嵌入交互式3D绘图
  2. 真正的从零开始学编程①——自己未来的计划
  3. 使用大恒水星相机利用OpenCV+ Zbar 解QR码在ROS下定位的实现
  4. 4点小诀窍,帮助你设计企业在线学习课程,让学员轻松记忆
  5. Git,简直就是高端大气上档次,屌!对于我来说,真的是太好用了。大赞ing
  6. Android Studio中虚拟机显示比例不正确(已解决)
  7. CSS-应用方式、选择器、常用属性
  8. 基于JAVA护肤品购物系统计算机毕业设计源码+数据库+lw文档+系统+部署
  9. 网络基础设施管理的未来
  10. 网页打印,强制分页并为每一页添加标题,为每一页添加表头