package test;
import java.io.File;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.poi.hssf.model.Workbook;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfTest {
Document document = new Document();// 建立一个Document对象
private static Font headfont;// 设置字体大小
private static Font keyfont;// 设置字体大小
private static Font textfont;// 设置字体大小
static {
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font chineseFont = new Font(bfChinese, 12, Font.NORMAL);
PdfPCell cellReportSummary = new PdfPCell(new Phrase("支持中文",
chineseFont));
headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
} catch (Exception e) {
e.printStackTrace();
}
}
public PdfTest(File file) {
document.setPageSize(PageSize.A4);// 设置页面大小
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
} catch (Exception e) {
e.printStackTrace();
}
}
int maxWidth = 520;
public PdfPCell createCell(String value, Font font, int align) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPCell createCell(String value, Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPCell createCell(String value, Font font, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPCell createCell(String value, Font font, int align, int colspan,
boolean boderFlag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(3.0f);
if (!boderFlag) {
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
}
return cell;
}
public PdfPTable createTable(int colNumber) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public PdfPTable createBlankTable() {
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public void generatePDF() throws Exception {
PdfPTable table = createTable(4);
table.addCell(createCell("学生信息列表:", keyfont, Element.ALIGN_LEFT, 4,
false));
table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));
for (int i = 0; i < 5; i++) {
table.addCell(createCell("姓名" + i, textfont));
table.addCell(createCell(i + 15 + "", textfont));
table.addCell(createCell((i % 2 == 0) ? "男" : "女", textfont));
table.addCell(createCell("地址" + i, textfont));
}
document.add(table);
document.close();
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\text.pdf");
file.createNewFile();
new PdfTest(file).generatePDF();
System.out.println("创建成功!");
}
}
java 生成PDF 实例
需积分: 9 123 浏览量
2014-08-18
14:59:04
上传
评论
收藏 9.09MB ZIP 举报
WJ5yy
- 粉丝: 1
- 资源: 10
最新资源
- GM-T 108-2021 诱骗态84量子密钥分配产品技术规范
- baiduwenku_AndroidPhone_1027470h-1718616594228.apk
- 飞飞CMS仿零零七影院网站模板
- GM-T 0109-2021 基于云计算的电子签名服务技术要求
- content_1718616449016.docx
- GM-T 0118-2022 浏览器数字证书应用接口规范
- 基于微信考试答题小程序设计与实现
- jdk-8u191-linux-x64.tar.gz jdk-8u191-linux-x64.tar.gz
- 输入文件备份位置自动备份测量程序操作方法
- GM-T 0122-2022 区块链密码检测规范
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈