springbootプロジェクト:pdfファイルを生成します

1、単純なPDFを生成します

  • springbootプロジェクトの作成、あまりここで言うために
  • 輸入依存度
<!-- 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>
  • hellowordのPDFファイルを生成します
package com.example.demo;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @ClassName TestPdf
 * @Description 生成hello world
 * @Author 戴某某
 * @Date 2019/11/19 20:29
 * @Version 1.0
 **/
@Slf4j
public class TestPdf {
    
    private static final String FILE_NAME = "F:\\pdf\\helloPdf.pdf";

    public static void main(String[] args) throws IOException {
        TestPdf pdf = new TestPdf();
        pdf.createPdf(FILE_NAME);
        log.info("打印完成");
    }

    /**
     * 生成一个hello world
     */
    public void createPdf(String filename)throws IOException {
        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();
        }
    }
}

2、輸出平板状のpdf

/**
     * 生成带表格的pdf
     */
    public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
        //设置中文字体
        BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
        Font fontChinese5 = new Font(bf,8);

        PdfPTable table = new PdfPTable(2);//生成一个两列的表格
        //表格垂直居中
        table.setHorizontalAlignment(Element.ALIGN_CENTER);

        PdfPCell cell;
        int size = 15;
        cell = new PdfPCell(new Paragraph("哈哈哈",fontChinese5));
        cell.setFixedHeight(size);//设置高度

        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("two",fontChinese5));
        cell.setFixedHeight(size);

        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("three",fontChinese5));
        cell.setFixedHeight(size);

        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("four",fontChinese5));
        cell.setFixedHeight(size);
        table.addCell(cell);
        return table;
    }

    /**
     * 创建表
     * @throws IOException
     */
    public 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();
            PdfPTable table = createTable(writer);
            document.add(table);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }

    }

ここに画像を挿入説明

3、例を見て

ここに画像を挿入説明

public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
   //设置中文字体
    BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
    Font fontChinese5 = new Font(bf,8);

    PdfPTable table = new PdfPTable(2);//生成一个两列的表格
    //表格垂直居中
    table.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cell;
    int size = 15;
    cell = new PdfPCell(new Paragraph("one",fontChinese5));
    cell.setFixedHeight(size);//设置高度

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("two",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("three",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("four",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("five",fontChinese5));
    cell.setColspan(1);//设置所占列数
    cell.setRowspan(2);//合并行
    cell.setFixedHeight(size*2);//设置高度
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
    cell.setFixedHeight(size);

    table.addCell(cell);

    cell = new PdfPCell(new Paragraph("six",fontChinese5));
    cell.setFixedHeight(size);

    table.addCell(cell);
    cell = new PdfPCell(new Paragraph("seven",fontChinese5));
    cell.setFixedHeight(size);
    table.addCell(cell);
    return table;
}
公開された134元の記事 ウォン称賛91 ビュー160 000 +

おすすめ

転載: blog.csdn.net/weixin_44588495/article/details/103150754