java 调用pdf_【Java & PDF】使用Java生成PDF

@Testpublic void sortOfSettings3() throwsException {final String pdfDocName = "quick-start.pdf";final String systemPath = "C:\\Users\\User-Dai\\Desktop\\Temp-File\\";

Rectangle rectangle= newRectangle(PageSize.A4);

Document document= newDocument(rectangle);

FileOutputStream fileOutputStream= new FileOutputStream(systemPath +pdfDocName);

PdfWriter pdfWriter=PdfWriter.getInstance(document, fileOutputStream);

pdfWriter.setPdfVersion(PdfWriter.VERSION_1_4);

document.addAuthor("Author@Echo42Zone");

document.addCreationDate();

document.addCreator("Creator@Echo42Zone");

document.setMargins(10,10,10,10);

document.open();

document.add(new Paragraph("Hello Itext PDF !!!"));//Chunk对象: a String, a Font, and some attributes

document.add(new Chunk("China"));

document.add(new Chunk(" "));

Font font= new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);

Chunk id= new Chunk("chinese", font);

id.setBackground(BaseColor.BLACK, 1f,0.5f, 1f, 1.5f);

id.setTextRise(6);

document.add(id);

document.add(Chunk.NEWLINE);

document.add(new Chunk("Japan"));

document.add(new Chunk(" "));

Font font2= new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);

Chunk id2= new Chunk("japanese", font2);

id2.setBackground(BaseColor.BLACK, 1f,0.5f, 1f, 1.5f);

id2.setTextRise(6);

id2.setUnderline(0.2f, -2f);

document.add(id2);

document.add(Chunk.NEWLINE);//Phrase对象: a List of Chunks with leading

document.newPage();

document.add(new Phrase("Phrase page"));

Phrase director= newPhrase();

Chunk name= new Chunk("China");

name.setUnderline(0.2f, -2f);

director.add(name);

director.add(new Chunk(","));

director.add(new Chunk(" "));

director.add(new Chunk("chinese"));

director.setLeading(24);

document.add(director);

Phrase director2= newPhrase();

Chunk name2= new Chunk("Japan");

name2.setUnderline(0.2f, -2f);

director2.add(name2);

director2.add(new Chunk(","));

director2.add(new Chunk(" "));

director2.add(new Chunk("japanese"));

director2.setLeading(24);

document.add(director2);//Paragraph对象: a Phrase with extra properties and a newline

document.newPage();

document.add(new Paragraph("Paragraph page"));

Paragraph info= newParagraph();

info.add(new Chunk("China "));

info.add(new Chunk("chinese"));

info.add(Chunk.NEWLINE);

info.add(new Phrase("Japan "));

info.add(new Phrase("japanese"));

document.add(info);//List对象: a sequence of Paragraphs called ListItem

document.newPage();

List list= newList(List.ORDERED);for (int i = 0; i < 10; i++) {

ListItem item= new ListItem(String.format("%s: %d movies","country" + (i + 1), (i + 1) * 100), newFont(

Font.FontFamily.HELVETICA,6, Font.BOLD, BaseColor.WHITE));

List movielist= newList(List.ORDERED, List.ALPHABETICAL);

movielist.setLowercase(List.LOWERCASE);for (int j = 0; j < 5; j++) {

ListItem movieitem= new ListItem("Title" + (j + 1));

List directorlist= newList(List.UNORDERED);for (int k = 0; k < 3; k++) {

directorlist.add(String.format("%s, %s", "Name1" + (k + 1),"Name2" + (k + 1)));

}

movieitem.add(directorlist);

movielist.add(movieitem);

}

item.add(movielist);

list.add(item);

}

document.add(list);

document.newPage();

document.close();

fileOutputStream.close();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值