java itext 生成pdf

引入pom 

         <!-- pdf -->
        <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>
        <!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <!-- pdf end-->

测试

package com.hyway.test;

import com.hyway.bean.InventoryDif;
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Table;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class PdfTest2 {
    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;
    // 静态代码块
    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, 7, Font.BOLD);
            keyfont = new Font(bfChinese, 10, Font.BOLD);
            textfont = new Font(bfChinese, 9, Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        generatePDF("test",null);
    }

    public static void generatePDF( String fileName, List<InventoryDif> list){
        try {
            // 1.新建document对象
            Document document = new Document(PageSize.A4,-60.0F,-60.0F,20.0F,0.0F);
            // 2.建立一个书写器(Writer)与document对象关联
            File file = new File("c:\\棚卸差分\\"+ fileName + ".pdf");
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            // 3.打开文档
            document.open();
            //12列 设置各个相对列宽
            PdfPTable table = new PdfPTable(new float[]{20f, 20f, 20f, 25f,15f, 15f, 15f, 15f,15f, 15f, 15f, 8f,});

            //创建表头
            table.addCell(createHeaderCell("品番反番"));
            table.addCell(createHeaderCell("銘柄コード"));
            table.addCell(createHeaderCell("銘柄"));
            table.addCell(createHeaderCell("棚卸置場コード"));
            table.addCell(createHeaderCell("棚卸置場"));
            table.addCell(createHeaderCell("棚卸単価"));
            table.addCell(createHeaderCell("商品区分"));
            table.addCell(createHeaderCell("IPOS在庫"));
            table.addCell(createHeaderCell("理論在庫"));
            table.addCell(createHeaderCell("実棚卸数量"));
            table.addCell(createHeaderCell("棚卸指示日"));
            table.addCell(createHeaderCell("dif"));
            table.setHeaderRows(1);//每页都显示表头

            for (int i = 0; i < 605; i++) {
                table.addCell(createRowCellTextAtLeft("1206"));
                table.addCell(createRowCellTextAtLeft("261601"));
                table.addCell(createRowCellTextAtLeft("ベアリング"));
                table.addCell(createRowCellTextAtLeft("3001"));
                table.addCell(createRowCellTextAtLeft("工作2F"));
                table.addCell(createRowCellTextAtRight("486.00"));
                table.addCell(createRowCellTextAtLeft("1"));
                table.addCell(createRowCellTextAtRight("1"));
                table.addCell(createRowCellTextAtRight("0"));
                table.addCell(createRowCellTextAtRight("0"));
                table.addCell(createRowCellTextAtLeft("2020/9/25"));
                table.addCell(createRowCellTextAtRight("し"));
            }

            document.add(table);
            // 5.一定要关闭文档 否则生成错误
            document.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    public static PdfPCell createHeaderCell(String value) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, headfont));
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);//标题头背景灰色
        cell.setMinimumHeight(20F);
        return cell;
    }
    public static PdfPCell createRowCellTextAtLeft(String value) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setPhrase(new Phrase(value, textfont));
        return cell;
    }
    public static PdfPCell createRowCellTextAtRight(String value) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setPhrase(new Phrase(value, textfont));
        return cell;
    }

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值