java itext 生成pdf
程序员文章站
2022-04-19 20:58:19
...
引入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;
}
}
上一篇: Android 实现地理定位功能
下一篇: IDEA插件(通俗易懂)