package com.jll.demo;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfOutline;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfTransition;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;
/**
* 参考http://blog.csdn.net/tec_feng/article/details/7733576
* @author Administrator
*
*/
public class MyFirstPDF2 {
/**
* 插入Chunk(文本块), Phrase(短语), Paragraph(段落), List
*
*/
@SuppressWarnings("null")
public static void pdf1(){
Document doc = new Document(PageSize.B5);
PdfReader reader = null;
PdfStamper stamp = null;
BaseFont bf = null;
PdfWriter writer =null;
ZipOutputStream zip = null;
try {
//中文设置
bf = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",false);
//Font f = new Font (bf,12,Font.NORMAL,BaseColor.BLUE);
//设置下划线
Font bold_underlined = new Font(bf,12,Font.BOLD|Font.UNDERLINE,BaseColor.PINK);
//设置字体颜色
Font normal = new Font(bf,12,Font.NORMAL,BaseColor.BLUE);
writer = PdfWriter.getInstance(doc, new FileOutputStream("d://1.pdf"));
/**
* 调用PDFBuilder类的setFooter方法显示页眉页脚
*/
PDFBuilder.setFooter(writer);
//设置行间距(如果不设置行间距,两个Chunk对象会写在同一行。)
writer.setInitialLeading(30);
doc.open();
doc.add(new Chunk("第一章",normal).setLocalDestination("1"));
doc.add(Chunk.NEWLINE);
//Chunk对象(文本块):
doc.add(new Chunk("China"));
doc.add(new Chunk(" ,"));
//创建一个Font对象,使用COURIER字体,6号,黑体,红色。
Font font = new Font(Font.FontFamily.COURIER,6,Font.BOLD,BaseColor.RED);
Chunk id = new Chunk("chinese",font);
//设置字体的背景色为蓝色
id.setBackground(BaseColor.BLUE,1f,0.5f,1f,1.5f);
//设置块Chunk的上标/下标
id.setTextRise(6);
doc.add(id);
doc.add(Chunk.NEWLINE);
//默认使用12pt的Helvetica字体,并且这个默认设置不能更改
doc.add(new Chunk("Japan"));
doc.add(new Chunk(" ,"));
Font font2 = new Font(Font.FontFamily.HELVETICA,6,Font.BOLD,BaseColor.RED);
Chunk id2 = new Chunk("japanese",font2);
id2.setBackground(BaseColor.GRAY,1f,0.5f,1f,1.5f);
//设置块Chunk的上标/下标SetTextRise是设置上标,其中参数表示,离开基线的距离,如果设置负数就表示设置下标
id2.setTextRise(6);
doc.add(id2);
doc.add(Chunk.NEWLINE);
//Phrase对象是带有行间距的Chunk对象的列表(短语):
//doc.newPage();
doc.add(new Phrase("Phrase page"));
Phrase director = new Phrase();
//设置行间距
director.setLeading(30);
Chunk chunk1 = new Chunk("Hello",bold_underlined);
Chunk chunk2 = new Chunk("How are you",normal);
//插入下划线
chunk1.setUnderline(0.2f, -2f);
//文本块换行
director.add(Chunk.NEWLINE);
director.add(chunk1);
director.add(Chunk.NEWLINE);
director.add(chunk2);
director.add(new Chunk(" chinese",normal));
doc.add(director);
Phrase director2 = new Phrase();
//设置行间距
director2.setLeading(30);
Chunk name1 = new Chunk("Japan",normal);
Chunk name2 = new Chunk("Japan22222",bold_underlined);
//插入下划线
name1.setUnderline(0.5f, 3f);
director2.add(Chunk.NEWLINE);
director2.add(name1);
director2.add(Chunk.NEWLINE);
director2.add(name2);
doc.add(director2);
/**
* Paragraph对象(段落): 可理解为能进行更多属性设置的Phrase对象和一个换行符
* Paragraph类继承自Phrase类
* Paragraph实例可以设置文本对齐方式、缩进以及段前段后空间
*/
doc.newPage();
doc.add(new Chunk("第二章",normal).setLocalDestination("2"));
doc.add(new Paragraph(new Chunk("sub 2.1").setLocalDestination("sub 2.1")));
doc.add(new Paragraph(new Chunk("sub 2.2").setLocalDestination("sub 2.2")));
doc.add(new Paragraph("Paragraph page"));
Paragraph info = new Paragraph();
for (int i = 0; i <10; i++) {
info.add(new Chunk("China"));
//换行
info.add(Chunk.NEWLINE);
info.add(new Chunk("南京",normal));
info.add(new Chunk("上海",normal));
info.add(Chunk.NEWLINE);
}
//每段文字上面和下面的空间
info.setSpacingBefore(50);
info.setSpacingAfter(50);
//设置段落对齐方式 (默认情况下,文本的对齐方式为左对齐)
info.setAlignment(Element.ALIGN_CENTER);
doc.add(info);
/**
* 插入Anchor
*/
Paragraph info2 = new Paragraph("This is a test!center");
Anchor dest = new Anchor("外部链接",bold_underlined);
dest.setName("CN");
dest.setReference("http://www.china.com");//外部链接
info2.add(dest);
info2.add(String.format(":%d sites", 10000));
//设置缩进
info2.setIndentationLeft(10f);
//注意增加段落时会自动换行
doc.add(info2);
/**
* List对象: a sequence of Paragraphs called ListItem
*/
doc.newPage();
List list = new List(List.ORDERED);
for (int i = 0; i < 3; i++) {
ListItem item=new ListItem(String.format("%s: %d movies", "country"+(i+1),(i+1)*100), new Font(
Font.FontFamily.HELVETICA,6,Font.BOLD,BaseColor.RED));
List movieList = new List(List.ORDERED,List.ALPHABETICAL);
movieList.setLowercase(List.LOWERCASE);
for (int j = 0; j < 4 ;j++) {
ListItem movieitem = new ListItem("Title"+(j+1));
List directorList = new List(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);
}
doc.add(list);
Anchor toUs = new Anchor("内部链接",bold_underlined);
toUs.setReference("#CN");//内部链接
doc.add(toUs);
/**
* 插入 Image对象, Chapter, Section
*/
Image img = Image.getInstance("WebContent/image/8.jpg");
img.setAlignment(Image.LEFT|Image.TEXTWRAP);
img.setBorder(Image.BOX);
img.setBorderWidth(10);
img.setBorderColor(BaseColor.WHITE);
img.scaleToFit(1000, 72);//大小
img.setRotationDegrees(-30);//旋转
doc.add(img);
//Chapter, Section对象(章节和(子)区域)Chapter对象和Section对象自动构建一个树
doc.newPage();
Paragraph title = new Paragraph("个人信息",normal);
//Paragraph title2 = new Paragraph("Title2",normal);
Chapter chapter = new Chapter(title,1);
title = new Paragraph("性别",normal);
title.setIndentationLeft(10);//设置左缩进
Section section = chapter.addSection(title);
section.setBookmarkTitle("性别");
section.setIndentation(20);//设置缩进
section.setBookmarkOpen(false);
section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);
Section subSection = section.addSection(new Paragraph(" 男",normal));
subSection.setIndentation(30);//设置缩进
subSection.setNumberDepth(1);
Section subSection2 = section.addSection(new Paragraph(" 女",normal));
subSection2.setNumberDepth(1);
doc.add(chapter);
/**
* 画图
*/
doc.add(new VerticalPositionMark(){
/**
* 左右箭头
*/
public void draw(PdfContentByte canvas,float llx,float lly,float urx,float ury,float y){
canvas.beginText();
BaseFont bf =null;
try {
bf = BaseFont.createFont(BaseFont.ZAPFDINGBATS,"",BaseFont.EMBEDDED);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
canvas.setFontAndSize(bf, 12);
//左边
canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char)220), llx-10, y, 0);
//右边
canvas.showTextAligned(Element.ALIGN_CENTER, String.valueOf((char)220), urx+10, y+8,180);
canvas.endText();
}
});
/**
* 直线
*/
Paragraph p1 = new Paragraph("开始",normal);
p1.add(new Chunk(new LineSeparator()));
p1.add("结束");
doc.add(p1);
/**
* 点线
*/
Paragraph p2 = new Paragraph("开始",normal);
p2.add(new Chunk(new DottedLineSeparator()));
p2.add("结束");
doc.add(p2);
/**
* 下滑线
*/
Paragraph p3 = new Paragraph("你好",normal);
LineSeparator underLine = new LineSeparator(1,100,null,Element.ALIGN_CENTER,-2);
//左右文字(貌似没什么效果待测试)
//ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, p3,100, 572, 0);
p3.add(underLine);
doc.add(p3);
/**
* 删除Page
reader = new PdfReader("d://create4.pdf");
reader.selectPages("1,3");//选中第1个和第3个page
stamp = new PdfStamper(reader, new FileOutputStream("d://2.pdf"));//把选中的page添加到新的pdf中
*/
/**
* 插入Page
stamp.insertPage(2, reader.getPageSize(1));
ColumnText ct = new ColumnText(null);
//这个内容不显示,但是可以添加一张新的page
ct.addElement(new Paragraph(24,new Chunk("新添加的Page",normal)));
ct.setCanvas(stamp.getOverContent(2));
ct.setSimpleColumn(36,36,559,770);
*/
/**
* 排序page(发现总共5页但只能排序4页)
*/
int[] order = {1,2,3,4};
writer.reorderPages(order);
/**
* 目录
* 允许读者从文档的一个地方跳转到另外一个地方的链接,
* 可以通过类Chunk的setLocalGoto 和setLocalDestination两个方法实现
*/
doc.add(new Chunk("第三章",normal).setLocalDestination("3"));
PdfContentByte cb = writer.getDirectContent();
PdfOutline root = cb.getRootOutline();
@SuppressWarnings("unused")
PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "第一章");
PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "第二章");
oline2.setOpen(false);
@SuppressWarnings("unused")
PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "sub 2.1");
@SuppressWarnings("unused")
PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "sub 2.2");
@SuppressWarnings("unused")
PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "第三章");
doc.newPage();
PdfPTable table = new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Phrase("新增的一页",normal)) ;
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
table.addCell(new PdfPCell(new Paragraph("cell 1")));
table.addCell(new PdfPCell(new Paragraph("cell 2")));
table.addCell(new PdfPCell(new Paragraph("cell 3")));
doc.add(table);
doc.newPage();
doc.add(table);
/**
* 幻灯片放映
//全屏
writer.setViewerPreferences(PdfWriter.PageModeFullScreen);
writer.setPageEvent(new PdfPageEventHelper(){
public void onStartPage(PdfWriter writer,Document cocument){
writer.setTransition(new PdfTransition(PdfTransition.DISSOLVE,3));
writer.setDuration(1);//间隔时间
}
});
*/
/**
* 压缩PDF到Zip
zip = new ZipOutputStream(new FileOutputStream("d://zipPDF.zip"));
ZipEntry entry = new ZipEntry("create1.pdf");
zip.putNextEntry(entry);
writer.setCloseStream(false);
zip.closeEntry();
*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
/*try {
stamp.close();
reader.close();
doc.close();
//zip.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
doc.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
PDFBuilder builder = new PDFBuilder();
builder.setPageHeight(400);
builder.setPageWidth(600);
MyFirstPDF2.pdf1();
}
}
最近下载更多
最近浏览更多
1358849392 LV21
2022年11月23日
akbar2020 LV9
2022年9月4日
是你爸爸啊100 LV5
2022年8月29日
crosa_Don LV18
2022年6月7日
heifenglei LV7
2022年4月7日
nickshen111 LV8
2021年12月13日
来恬爸爸晋亚阳 LV3
2021年10月29日
疯狂的巨兔12138 LV4
2021年5月14日
阿玉之父 LV1
2021年4月22日
一字清华 LV8
2021年2月21日

