Java生成PDF文件

更新时间:2023-07-31 06:08:06 阅读: 评论:0

Java⽣成PDF⽂件
PDF作为⽇常常见的⽂件格式之⼀,有时候经常需要使⽤到。在Java中⽣成PDF的⽅式⼀般有:itext-PdStamper pdfStamper(根据模板⽣成PDF),itext-Document document(使⽤代码编写)等。
幼儿园实习目的使⽤IText⽣成PDF:IText是⼀个能够快速⽣成PDF⽂件的Java类库,IText的Java类对于那些要⽣成包括⽂本,表格,图形的只读⽂档是很好⽤的。
IText依赖jar包 (百度云下载)
在java项⽬导⼊依赖jar包,如下
itext-asian.jar 中⽂⽀持
Document对象构造⽅法
Document(Rectangle pageeSize,float marginLeft,float marginTop,float marginBottom) 分别代表PDF页⾯⼤⼩,内容距离⽂档边的距离。默认 Document() ⽆参默认为 A4 ,36,36,36,36
属性
版本(PdfVersionImp)、标题(Title)、作者(Author)、主题(Subject)、关键字(Keywords)、创建者(Creator)等
⽅法
add() 向⽂档添加内容 newPage()添加下⼀页 getPageSize()页⾯⼤⼩等等⽅法
Rectangle对象 (页⾯对象)
常⽤的构造⽅法 Rectangle(PageSize.A4)
属性:NO_BORDER (⽆边框) ,
⽅法:rotate()横向打印 tBackgroundColor()设置背景⾊ tBorder()设置边框 tBorderColor()设置边框颜⾊
Element接⼝
内容对象差不多都实现了该接⼝,可以设置⼀些⽅位参数,如居中对齐。
Paragraph段落对象⽅法 add(Element)添加以及⼀些设置段落⽂字格式的⽅法
PDF⽂档⽣成(不使⽤模板)
FileOutputStream out;
@Test
public void test() throws DocumentException, MalformedURLException, IOException {
out = new FileOutputStream("D:\\Demo\\春.pdf");
// 设置页⾯的属性
Rectangle tRectangle = new Rectangle(PageSize.A4); // 页⾯⼤⼩
tRectangle.tBackgroundColor(BaColor.WHITE); // 页⾯背景⾊
tRectangle.tBorder(1220);// 边框
tRectangle.tBorderColor(BaColor.BLUE);// 边框颜⾊
tRectangle.tBorderWidth(244.2f);// 边框宽度
Document doc = new Document(tRectangle);// 定义⽂档
doc = new ate());// 横向打印
PdfWriter writer = Instance(doc, out);// 书写器
writer.tPdfVersion(PdfWriter.PDF_VERSION_1_2);//版本(默认1.4)
//设置PDF⽂档属性
doc.addTitle("春");// 标题
doc.addAuthor("朱⾃清");// 作者
头饰怎么做doc.addSubject("春天的脚步近了");// 主题
doc.addKeywords("春");// 关键字
无锡特色
doc.addCreator("⽥野上的风筝");// 创建者
Paragraph tParagraph = new Paragraph("春" , getChineFont());
tParagraph.tAlignment(Element.ALIGN_JUSTIFIED);// 对齐⽅式
Paragraph tParagraphs = new Paragraph("盼望着,盼望着,东风来了春天的脚步近了。⼀切都像刚睡醒的样⼦,欣欣然张开了眼。⼭朗润起来了,⽔涨起来了,太阳的脸红起来了", getChineFont());
tParagraph.tAlignment(Element.ALIGN_CENTER);// 居中
tParagraph.tIndentationLeft(12);// 左缩进
tParagraph.tIndentationRight(12);// 右缩进
tParagraph.tFirstLineIndent(24);// ⾸⾏缩进
tParagraph.tLeading(20f);// ⾏间距
tParagraph.tSpacingBefore(5f);// 设置上空⽩
tParagraph.tSpacingAfter(10f);// 设置段落下空⽩
doc.tMargins(10, 20, 30, 40);// 页边空⽩
Image img = Instance("D:\\Demo\\spring.png");
img.tAlignment(Image.MIDDLE); //设置图⽚居中
img.tBorder(Image.BOX);
img.tBorderWidth(10);
img.tBorderColor(BaColor.WHITE);
img.scaleToFit(900, 350);// 设置图⽚⼤⼩
doc.open();// 打开⽂档
/// doc.add(tRectangle);
doc.add(tParagraph); //添加段落
扛旗者
doc.add(tParagraphs);扬州市花
doc.add(img); //添加img
// wPage(); //添加下⼀页
/
/ writer.tPageEmpty(true);// fasle-显⽰空内容的页;true-不会显⽰
doc.clo(); //记得关闭document
}
//⽀持中⽂设置字体,字体颜⾊、⼤⼩等
public Font getChineFont() {
BaFont simpChine;
Font ChineFont = null;
try {
simpChine = ateFont("STSongStd-Light", "UniGB-UCS2-H", BaFont.NOT_EMBEDDED);
ChineFont = new Font(simpChine, 12, Font.NORMAL, BaColor.BLUE);
} catch (DocumentException e) {
e.printStackTrace();
地中海风格图片} catch (IOException e) {
e.printStackTrace();
}
return ChineFont;
}
使⽤IText⽣成PDF⽂件(⽆模板),仔细看官⽅API⽂档即可轻松创建PDF⽂档。
运⾏
使⽤模板⽣成PDF⽂件(准备⼀个PDF模板)
模板的制作;使⽤word⽂档新建⼀个内容⽂档,如在⽂档放⼀个表格,将⽂档转为PDF格式,转为PDF⽂件后,使⽤PDF编辑器打开(⼯具可以使⽤Adobe Acrobat Pro),在PDF编辑器⾥添加表单域,⼀般如果是内容是表格的话,该⼯具会⾃动识别并添加域。也可以⾃⼰添加域,建议⾃⼰添加⽂本域,
(这样⽐⾃动添加要稳)并设置字体属性,属性包括字体⼤⼩,样式。对齐等。
如下,⾼亮部分代表表单域。
下⾯直接上代码
public void fillTemplate() {
try {
FileOutputStream out = new FileOutputStream("D:\\is\\夏季.pdf");// 输出流论友谊
PdfReader reader = new PdfReader("F:\\Java\\PDF模板\\夏季.pdf");//读取pdf模板
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, bos);
AcroFields form = AcroFields();
String[] str = { "星⽕⼩学", "2019-8-1", "8:00-20:00"};//定义数组,⽤于赋值
BaFont bf = ateFont("STSong-Light", "UniGB-UCS2-H", BaFont.NOT_EMBEDDED); //设置中⽂
ArrayList<BaFont> fontList = new ArrayList<BaFont>();
fontList.add(bf); //添加字体
form.tSubstitutionFonts(fontList); //设置替换字体
Iterator<String> Fields().keySet().iterator();//得到字段迭代器
while (it.hasNext()) {//遍历输出表单域名与值
String name = it.next().toString();
form.tField(name, str[i]);
System.out.println("name="+name+";str="+str[i++]);
}
stamper.tFormFlattening(true);// 设置为fasle,⽣成PDF将不能编辑
stamper.clo();//关闭PdfStamper
Document doc = new Document();//实例化Document对象
PdfCopy copy = new PdfCopy(doc, out);
doc.open();//打开⽂档
PdfImportedPage importPage = ImportedPage(new ByteArray()), 1); copy.addPage(importPage);
doc.clo();
} catch (Exception e) {
对母亲的祝福e.printStackTrace();
}
}
运⾏上⾯⽅法

本文发布于:2023-07-31 06:08:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102774.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   字体   添加   模板   属性
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图