使用itextpdf 将 前台 数据 转成pdf !

2023-10-10 10:30

本文主要是介绍使用itextpdf 将 前台 数据 转成pdf !,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

iText介绍

  iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

  项目要使用iText,必须引入jar包。才能使用,maven依赖如下:

1 <dependency>
2     <groupId>com.itextpdf</groupId>
3     <artifactId>itextpdf</artifactId>
4     <version>5.5.10</version>
5 </dependency>

  输出中文,还要引入下面itext-asian.jar包:

1 <dependency>
2     <groupId>com.itextpdf</groupId>
3     <artifactId>itext-asian</artifactId>
4     <version>5.2.0</version>
5 </dependency>

      设置pdf文件密码,还要引入下面bcprov-jdk15on.jar包:

1 <dependency>
2     <groupId>org.bouncycastle</groupId>
3     <artifactId>bcprov-jdk15on</artifactId>
4     <version>1.54</version>
5 </dependency>

 

iText常用类

  • com.itextpdf.text.Document:这是iText库中最常用的类,它代表了一个pdf实例。如果你需要从零开始生成一个PDF文件,你需要使用这个Document类。首先创建(new)该实例,然后打开(open)它,并添加(add)内容,最后关闭(close)该实例,即可生成一个pdf文件。
  • com.itextpdf.text.Paragraph:表示一个缩进的文本段落,在段落中,你可以设置对齐方式,缩进,段落前后间隔等。
  • com.itextpdf.text.Chapter:表示PDF的一个章节,他通过一个Paragraph类型的标题和整形章数创建。
  • com.itextpdf.text.Font:这个类包含了所有规范好的字体,包括family of font,大小,样式和颜色,所有这些字体都被声明为静态常量。
  • com.itextpdf.text.List:表示一个列表;
  • cocom.itextpdf.text.List:表示一个列表;
  • com.itextpdf.text.Anchor:表示一个锚,类似于HTML页面的链接。
  • com.itextpdf.text.pdf.PdfWriter:当这个PdfWriter被添加到PdfDocument后,所有添加到Document的内容将会写入到与文件或网络关联的输出流中。
  • com.itextpdf.text.pdf.PdfReader:用于读取PDF文件;

iText使用

  1. 创建一个简单的pdf文件,如下:
    复制代码
     1 package com.hd.pdf;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 
     6 import com.itextpdf.text.Document;
     7 import com.itextpdf.text.DocumentException;
     8 import com.itextpdf.text.Paragraph;
     9 import com.itextpdf.text.pdf.PdfWriter;
    10 
    11 public class TestPDFDemo1 {
    12 
    13     public static void main(String[] args) throws FileNotFoundException, DocumentException {
    14 
    15         // 1.新建document对象
    16         Document document = new Document();
    17 
    18         // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
    19         // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
    20         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test.pdf"));
    21 
    22         // 3.打开文档
    23         document.open();
    24         
    25         // 4.添加一个内容段落
    26         document.add(new Paragraph("Hello World!"));
    27 
    28         // 5.关闭文档
    29         document.close();
    30 
    31     }
    32 
    33 }
    复制代码

    打开文件

  2. 给PDF文件设置文件属性,例如:

    复制代码
     1 public static void main(String[] args) throws FileNotFoundException, DocumentException {
     2         
     3         //创建文件
     4         Document document = new Document();
     5         //建立一个书写器
     6         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test2.pdf"));
     7         //打开文件
     8         document.open();
     9         //添加内容
    10         document.add(new Paragraph("Some content here"));
    11      
    12         //设置属性
    13         //标题
    14         document.addTitle("this is a title");
    15         //作者
    16         document.addAuthor("H__D");
    17         //主题
    18         document.addSubject("this is subject");
    19         //关键字
    20         document.addKeywords("Keywords");
    21         //创建时间
    22         document.addCreationDate();
    23         //应用程序
    24         document.addCreator("hd.com");
    25         
    26         //关闭文档
    27         document.close();
    28         //关闭书写器
    29         writer.close();
    30     }
    复制代码

    打开文件

  3. PDF中添加图片

    复制代码
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         
     3         //创建文件
     4         Document document = new Document();
     5         //建立一个书写器
     6         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test3.pdf"));
     7         //打开文件
     8         document.open();
     9         //添加内容
    10         document.add(new Paragraph("HD content here"));
    11      
    12         //图片1
    13         Image image1 = Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");
    14         //设置图片位置的x轴和y周
    15         image1.setAbsolutePosition(100f, 550f);
    16         //设置图片的宽度和高度
    17         image1.scaleAbsolute(200, 200);
    18         //将图片1添加到pdf文件中
    19         document.add(image1);
    20      
    21         //图片2
    22         Image image2 = Image.getInstance(new URL("http://static.cnblogs.com/images/adminlogo.gif"));
    23         //将图片2添加到pdf文件中
    24         document.add(image2);
    25         
    26         //关闭文档
    27         document.close();
    28         //关闭书写器
    29         writer.close();
    30     }
    复制代码

    打开文件
     

  4. PDF中创建表格
     

    按 Ctrl+C 复制代码
    按 Ctrl+C 复制代码

    打开文件

     

  5.  PDF中创建列表

    复制代码
     1 public static void main(String[] args) throws DocumentException, FileNotFoundException {
     2         //创建文件
     3         Document document = new Document();
     4         //建立一个书写器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test5.pdf"));
     6         //打开文件
     7         document.open();
     8         //添加内容
     9         document.add(new Paragraph("HD content here"));
    10      
    11         //添加有序列表
    12         List orderedList = new List(List.ORDERED);
    13         orderedList.add(new ListItem("Item one"));
    14         orderedList.add(new ListItem("Item two"));
    15         orderedList.add(new ListItem("Item three"));
    16         document.add(orderedList);
    17 
    18         //关闭文档
    19         document.close();
    20         //关闭书写器
    21         writer.close();
    22     }
    复制代码

    打开文件

     

  6.  

    PDF中设置样式/格式化输出,输出中文内容,必须引入itext-asian.jar

    复制代码
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         //创建文件
     3         Document document = new Document();
     4         //建立一个书写器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test6.pdf"));
     6         //打开文件
     7         document.open();
     8         
     9         //中文字体,解决中文不能显示问题
    10         BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    11         
    12         //蓝色字体
    13         Font blueFont = new Font(bfChinese);
    14         blueFont.setColor(BaseColor.BLUE);
    15         //段落文本
    16         Paragraph paragraphBlue = new Paragraph("paragraphOne blue front", blueFont);
    17         document.add(paragraphBlue);
    18      
    19         //绿色字体
    20         Font greenFont = new Font(bfChinese);
    21         greenFont.setColor(BaseColor.GREEN);
    22         //创建章节
    23         Paragraph chapterTitle = new Paragraph("段落标题xxxx", greenFont);
    24         Chapter chapter1 = new Chapter(chapterTitle, 1);
    25         chapter1.setNumberDepth(0);
    26        
    27         Paragraph sectionTitle = new Paragraph("部分标题", greenFont);
    28         Section section1 = chapter1.addSection(sectionTitle);
    29      
    30         Paragraph sectionContent = new Paragraph("部分内容", blueFont);
    31         section1.add(sectionContent);
    32         
    33         //将章节添加到文章中
    34         document.add(chapter1);
    35         
    36         //关闭文档
    37         document.close();
    38         //关闭书写器
    39         writer.close();
    40     }
    复制代码

    打开文件

     

  7.  给PDF文件设置密码,需要引入bcprov-jdk15on.jar包:

    复制代码
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         // 创建文件
     3         Document document = new Document();
     4         // 建立一个书写器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test8.pdf"));
     6 
     7         //用户密码
     8         String userPassword = "123456";
     9         //拥有者密码
    10         String ownerPassword = "hd";
    11         writer.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING,
    12                 PdfWriter.ENCRYPTION_AES_128);
    13 
    14         // 打开文件
    15         document.open();
    16         
    17         //添加内容
    18         document.add(new Paragraph("password !!!!"));
    19 
    20         // 关闭文档
    21         document.close();
    22         // 关闭书写器
    23         writer.close();
    24     }
    复制代码

    打开文件

  8. 给PDF文件设置权限

    复制代码
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         // 创建文件
     3         Document document = new Document();
     4         // 建立一个书写器
     5         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/H__D/Desktop/test9.pdf"));
     6 
     7         // 只读权限
     8         writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
     9 
    10         // 打开文件
    11         document.open();
    12 
    13         // 添加内容
    14         document.add(new Paragraph("password !!!!"));
    15 
    16         // 关闭文档
    17         document.close();
    18         // 关闭书写器
    19         writer.close();
    20     }
    复制代码

     

  9. 读取/修改已有的PDF文件

    复制代码
     1 public static void main(String[] args) throws DocumentException, IOException {
     2         
     3         //读取pdf文件
     4         PdfReader pdfReader = new PdfReader("C:/Users/H__D/Desktop/test1.pdf");
     5      
     6         //修改器
     7         PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("C:/Users/H__D/Desktop/test10.pdf"));
     8      
     9         Image image = Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");
    10         image.scaleAbsolute(50, 50);
    11         image.setAbsolutePosition(0, 700);
    12      
    13         for(int i=1; i<= pdfReader.getNumberOfPages(); i++)
    14         {
    15             PdfContentByte content = pdfStamper.getUnderContent(i);
    16             content.addImage(image);
    17         }
    18      
    19         pdfStamper.close();
    20     }
    复制代码

    打开文件

这篇关于使用itextpdf 将 前台 数据 转成pdf !的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/179855

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1