[生成 pdf 详解]

目录

前言:

pom需要的依赖:

测试类:

效果:

生成表格PDF:

其他复杂的格式就去研究那个   如何生成吧

测试类代码:


前言:

   摸鱼来的

pom需要的依赖:

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

测试类:

生成一个helloword

package com.example.demo;



import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

 
public class PDFutilOut {

    public static void main(String[] args) {
        createPDF("C:/Users/本机用户姓名路径/Desktop/test1.pdf");
        System.out.println("打印完成");
    }

    public static void createPDF(String filename) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            document.add(new Paragraph("Hello World!"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }


}

效果:


生成表格PDF:

其他复杂的格式就去研究那个   如何生成吧

 

测试类代码:

package com.example.demo;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class PDFUtil2 {


    //pdf创建表格
    public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
        PdfPTable table = new PdfPTable(2);//生成一个两列的表格
        PdfPCell cell;
        //有中文文字的话需要设置字体
//        Font font = new Font(BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED));
        Font font = new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
        int size = 30;
        cell = new PdfPCell(new Phrase("您好--1", font));
        cell.setFixedHeight(size);//设置高度
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("2"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("3"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("4"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        //合并单元格
        cell = new PdfPCell(new Phrase("5"));
        cell.setColspan(1);//设置所占列数
        cell.setRowspan(2);//设置所占行数
        cell.setFixedHeight(size * 2);//设置高度为标准高度的两倍
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("6"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("77777"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        return table;
    }

    //创建pdf文件
    public static void createPDF(String filename) throws IOException {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            //document.add(new Paragraph("Hello World!"));
            PdfPTable table = createTable(writer);
            document.add(table);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }

    public static void main(String[] args) throws IOException {
        createPDF("C:/Users/本机用户姓名/Desktop/test1.pdf");
        System.out.println("打印完成");
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是汤圆丫

怎么 给1分?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值