好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java使用iTextPDF生成PDF文件的实现方法

iText介绍和说明

因为项目需要生成PDF文件,所以去找了一下能够生成PDF的Java工具,看到了iText可以说好评如潮。

如果你想通过java操作PDF文件,那么 iText 绝对是你的首选。

引入依赖

这里使用的是iText5

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

使用步骤简单介绍

快速入手iText拢共需要5步:

创建文档实例 Document 获取PdfWriter实例 (需要指定Document实例 和pdf 生成的磁盘路径) 打开文档 添加段落内容 关闭操作文档实例 (操作完成后必须执行文档关闭操作)

创建工具类

public class PdfUtil { // 标准字体 public static Font NORMALFONT ; // 加粗字体 public static Font BOLDFONT ; //固定高 public static float fixedHeight = 27f ; //间距 public static int spacing = 5 ;   static { try { BaseFont bfChinese = BaseFont . createFont ( "STSong-Light" , "UniGB-UCS2-H" , BaseFont . NOT_EMBEDDED ); NORMALFONT = new Font ( bfChinese , 10 , Font . NORMAL ); BOLDFONT = new Font ( bfChinese , 14 , Font . BOLD ); } catch ( Exception e ) { e . printStackTrace (); }   }   public static Document createDocument () { //生成pdf Document document = new Document (); // 页面大小 Rectangle rectangle = new Rectangle ( PageSize . A4 ); // 页面背景颜色 rectangle . setBackgroundColor ( BaseColor . WHITE ); document . setPageSize ( rectangle ); // 页边距 左,右,上,下 document . setMargins ( 20 , 20 , 20 , 20 ); return document ; }   /** * @param text 段落内容 * @return */ public static Paragraph createParagraph ( String text , Font font ) { Paragraph elements = new Paragraph ( text , font ); elements . setSpacingBefore ( 5 ); elements . setSpacingAfter ( 5 ); elements . setSpacingAfter ( spacing ); return elements ; }   public static Font createFont ( int fontNumber , int fontSize , BaseColor fontColor ) { //中文字体 ----不然中文会乱码 BaseFont bf = null ; try { bf = BaseFont . createFont ( "STSong-Light" , "UniGB-UCS2-H" , BaseFont . NOT_EMBEDDED ); return new Font ( bf , fontNumber , fontSize , fontColor ); } catch ( Exception e ) { e . printStackTrace (); } return new Font ( bf , Font . DEFAULTSIZE , Font . NORMAL , BaseColor . BLACK ); }   /** * 隐藏表格边框线 * * @param cell 单元格 */ public static void disableBorderSide ( PdfPCell cell ) { if ( cell != null ) { cell . disableBorderSide ( 1 ); cell . disableBorderSide ( 2 ); cell . disableBorderSide ( 4 ); cell . disableBorderSide ( 8 ); } }   /** * 创建居中得单元格 * * @return */ public static PdfPCell createCenterPdfPCell () { PdfPCell cell = new PdfPCell (); cell . setVerticalAlignment ( Element . ALIGN_MIDDLE ); cell . setHorizontalAlignment ( Element . ALIGN_CENTER ); cell . setFixedHeight ( fixedHeight ); return cell ; }   /** * 创建指定文字得单元格 * * @param text * @return */ public static PdfPCell createCenterPdfPCell ( String text , int rowSpan , int colSpan , Font font ) { PdfPCell cell = new PdfPCell ( new Paragraph ( text , font )); cell . setVerticalAlignment ( Element . ALIGN_MIDDLE ); cell . setHorizontalAlignment ( Element . ALIGN_LEFT ); cell . setFixedHeight ( fixedHeight ); cell . setRowspan ( rowSpan ); cell . setColspan ( colSpan ); return cell ; }   /** * @param len 表格列数 * @return */ public static PdfPTable createPdfPTable ( int len ) { PdfPTable pdfPTable = new PdfPTable ( len ); pdfPTable . setSpacingBefore ( 5 ); pdfPTable . setHorizontalAlignment ( Element . ALIGN_CENTER ); return pdfPTable ; } }

创建controller进行测试

/** * @author Wang Guolong * @version 1.0 * @date 2020/6/28 3:17 下午 */ @RestController @RequestMapping ( "/pdf" ) public class PdfController {   @RequestMapping ( "/generate" ) public Response generatePDF ( HttpServletResponse response ) throws Exception { String filename = "测试pdf" ; // 设置下载格式为pdf response . setContentType ( "application/x-download" ); response . addHeader ( "Content-Disposition" , "attachment;filename=" + URLEncoder . encode ( filename , "UTF-8" ) + ".pdf" ); OutputStream os = new BufferedOutputStream ( response . getOutputStream ());   // 1. Document document = new Document(); Document document = PdfUtil . createDocument (); // 2. 获取writer PdfWriter . getInstance ( document , os ); // 3. open() document . open ();   //设置字体 Font blackFont = PdfUtil . createFont ( 10 , Font . NORMAL , BaseColor . BLACK ); Font blueFont = PdfUtil . createFont ( 10 , Font . NORMAL , BaseColor . BLUE ); Font bigFont = PdfUtil . createFont ( 14 , Font . NORMAL , BaseColor . BLACK ); Font littleFont = PdfUtil . createFont ( 10 , Font . NORMAL , BaseColor . BLACK );   Paragraph title = PdfUtil . createParagraph ( "测试pdf" , bigFont ); title . setAlignment ( Element . ALIGN_CENTER ); // 4. 添加段落内容 document . add ( title ); // 5. close() document . close (); os . close (); return new Response (). setContent ( "success" ); } }

运行结果

下载页面:

下载的文件效果:

到此这篇关于Java使用iTextPDF生成PDF文件的实现方法的文章就介绍到这了,更多相关Java使用iTextPDF生成PDF内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/MCmango/article/details/113933761

查看更多关于Java使用iTextPDF生成PDF文件的实现方法的详细内容...

  阅读:25次