java编写pdf

java编写pdf


关于java通过加载pom文件生成特定pdf文件,博主是遇到了一个通过富文本编辑框输入内容,生成*红头文件的这样一个需求,通过document和write来书写pdf,话不多说,上代码:

public void writeCharpter(String categoryName, String agriculturalBureau, String weatherBureau, String frequency,
String userName, String title, String agriculturalContent, String weatherContent, String publishTimeString,
String path, HttpServletResponse response) throws Exception {
// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 24, 24);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“F:\” + title + “.pdf”));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
// 打开文件
document.open();
// 标题类型
Paragraph pt = new Paragraph(categoryName, headfont);// 将标题写进去
pt.setAlignment(1);// 设置文字居中 0靠左 1,居中 2,靠右
document.add(pt);
// 发布单位
// Phrase director2 = new Phrase();
// Chunk agr = new Chunk(agriculturalBureau, keyfont);
// agr.setUnderline(0.2f, -2f);
// director2.add(agr);
if (ValidateUtil.validateStringIsNullOrEmpty(weatherBureau)) {
Phrase director2 = new Phrase();
Chunk name2 = new Chunk("\n\n" + agriculturalBureau, keyfont);
director2.add(name2);
document.add(director2);
Phrase director3 = new Phrase();
director3.add(new Chunk(frequency, keyfont).setTextRise(16f));
// director3.setLeading(24);//左缩进
Paragraph p3 = new Paragraph(director3);
p3.setAlignment(1);
document.add(p3);
Phrase director4 = new Phrase();
director4.add(new Chunk(“签发人:” + userName, keyfont).setTextRise(32f));
// director3.setLeading(24);//左缩进
Paragraph p4 = new Paragraph(director4);
p4.setAlignment(2);
document.add(p4);
} else {
Phrase director2 = new Phrase();
Chunk name2 = new Chunk("\n\n" + agriculturalBureau, keyfont);
director2.add(name2);
document.add(director2);
Phrase director22 = new Phrase();
Chunk name22 = new Chunk("\n\n" + weatherBureau, keyfont);
director22.add(name22);
document.add(director22);
Phrase director3 = new Phrase();
director3.add(new Chunk(frequency, keyfont).setTextRise(32f));
// director3.setLeading(24);//左缩进
Paragraph p3 = new Paragraph(director3);
p3.setAlignment(1);
document.add(p3);
Phrase director4 = new Phrase();
director4.add(new Chunk(“签发人:” + userName, keyfont).setTextRise(48f));
// director3.setLeading(24);//左缩进
Paragraph p4 = new Paragraph(director4);
p4.setAlignment(2);
document.add(p4);
}
// Paragraph agr = new Paragraph("\n"+agriculturalBureau, keyfont);// 将标题写进去
// agr.setAlignment(0);// 设置文字居中 0靠左 1,居中 2,靠右
// document.add(agr);
//
// Paragraph fr = new Paragraph(frequency, keyfont);// 将标题写进去
// fr.setAlignment(1);// 设置文字居中 0靠左 1,居中 2,靠右
// document.add(fr);
//
// Paragraph us = new Paragraph(userName, keyfont);// 将标题写进去
// us.setAlignment(2);// 设置文字居中 0靠左 1,居中 2,靠右
// document.add(us);
Paragraph t = new Paragraph(
“----------------------------------------------------------------------------------”, keyfont);// 将标题写进去
t.setAlignment(1);// 设置文字居中 0靠左 1,居中 2,靠右
document.add(t);
// 标题
Paragraph ti = new Paragraph(title, titlefont);// 将标题写进去
ti.setAlignment(1);// 设置文字居中 0靠左 1,居中 2,靠右
document.add(ti);
// 正文
// Paragraph w = new Paragraph("\n" + weatherContent, textfont);
// ti.setAlignment(0);// 设置文字居中 0靠左 1,居中 2,靠右
// w.setFirstLineIndent(32f);
// document.add(w);
// 在下面,body中设置了style,设置了默认字体为宋体,这样导出时的html语言就默认带有了字体,汉字才会导出成功
// String content="<body style=“font-family: 宋体, SimHei;”>" +
// “<p style=“text-align: center;”><span style=“font-family: 黑体, SimHei; font-size: 24px;”>”
// + title + “

” + agriculturalContent + “”;
String content = “<body style=“font-family: 宋体, SimHei;”>” + weatherContent + agriculturalContent
+ “”;
byte b[] = content.getBytes(“utf-8”); // 这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);// 将字节数组包装到流中
XMLWorkerHelper.getInstance().parseXHtml(writer, document, bais, Charset.forName(“UTF-8”));
bais.close();
// Paragraph a = new Paragraph(agriculturalContent, textfont);
// w.setFirstLineIndent(32f);
// document.add(a);
Paragraph time = new Paragraph("\n\n" + publishTimeString, timefont);
time.setAlignment(2);// 设置文字居中 0靠左 1,居中 2,靠右
time.setFirstLineIndent(32);
document.add(time);
// document.addTitle(“Hello mingri example”);
// 作者
// document.addAuthor(“wolf”);
// 主题
// document.addSubject(“This example explains how to add metadata.”);
// document.addKeywords(“iText, Hello mingri”);
// document.addCreator(“My program using iText”);
// document.newPage();
// 向文档中添加内容
// document.add(new Paragraph(“Some more text on the first page with different color and font type.”,
// FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new BaseColor(0, 0, 0))));
// Paragraph title1 = new Paragraph(“Chapter 1”,
// FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new BaseColor(0, 0, 255)));
// // 新建章节
// Chapter chapter1 = new Chapter(title1, 1);
// chapter1.setNumberDepth(0);
// Paragraph title11 = new Paragraph(“This is Section 1 in Chapter 1”,
// FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new BaseColor(255, 0, 0)));
// Section section1 = chapter1.addSection(title11);
// Paragraph someSectionText = new Paragraph(“This text comes as part of section 1 of chapter 1.”);
// section1.add(someSectionText);
// someSectionText = new Paragraph(“Following is a 3 X 2 table.”);
// section1.add(someSectionText);
// document.add(chapter1);
// 关闭文档
document.close();
}
上一篇:k8s安装calico插件时的错误


下一篇:calico更换地址池