java通过itext导出word文档

首先导入itext相关jar包

itext-asian-5.2.0.jar

itext-rtf-2.1.7.jar

itext-2.1.7.jar

创建word文档

// 设置纸张的大小
Document document = new Document(PageSize.A4);
// 创建word文档
RtfWriter2.getInstance(document, new FileOutputStream(filePath));
// 打开文档
document.open();
//设置段落内容
document.add(new Paragraph("这是要导出的数据")); 
// 关流
document.close();

设置文体样式

//微软雅黑
String wryh = equest.getSession().getServletContext().getRealPath("/font_type/MSYHBD.TTF");   
BaseFont bf = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 10f, Font.NORMAL);
Paragraph option = new Paragraph(text, font);
			

设置密码

PdfWriter writer = PdfWriter.getInstance(document, out);  
  
// 设置密码为:"World"  
writer.setEncryption("Hello".getBytes(), "World".getBytes(),  
        PdfWriter.ALLOW_SCREENREADERS,  
        PdfWriter.STANDARD_ENCRYPTION_128);  
  
document.open();
document.add(new Paragraph("Hello World"));  

添加page

document.open();  
document.add(new Paragraph("First page"));  
document.add(new Paragraph(Document.getVersion()));  
  
document.newPage();  
writer.setPageEmpty(false);  
  
document.newPage();  
document.add(new Paragraph("New page"));

添加水印(背景图)

//图片水印  
PdfReader reader = new PdfReader(FILE_DIR + "setWatermark.pdf");  
PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(FILE_DIR  
        + "setWatermark2.pdf"));  
  
Image img = Image.getInstance("resource/watermark.jpg");  
img.setAbsolutePosition(200, 400);  
PdfContentByte under = stamp.getUnderContent(1);  
under.addImage(img);  
  
//文字水印  
PdfContentByte over = stamp.getOverContent(2);  
over.beginText();  
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,  
        BaseFont.EMBEDDED);  
over.setFontAndSize(bf, 18);  
over.setTextMatrix(30, 30);  
over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);  
over.endText();  
  
//背景图  
Image img2 = Image.getInstance("resource/test.jpg");  
img2.setAbsolutePosition(0, 0);  
PdfContentByte under2 = stamp.getUnderContent(3);  
under2.addImage(img2);  
  
stamp.close();  
reader.close(); 

插入Chunk, Phrase, Paragraph, List 

//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 = new Phrase();  
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 = new Phrase();  
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 = new Paragraph();  
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 = new List(List.ORDERED);  
for (int i = 0; i < 10; 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.WHITE));  
    List movielist = new List(List.ORDERED, List.ALPHABETICAL);  
    movielist.setLowercase(List.LOWERCASE);  
    for (int j = 0; j < 5; 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);  
}  
document.add(list);  

插入Anchor, Image, Chapter, Section 

//Anchor对象: internal and external links  
Paragraph country = new Paragraph();  
Anchor dest = new Anchor("china", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));  
dest.setName("CN");  
dest.setReference("http://www.china.com");//external  
country.add(dest);  
country.add(String.format(": %d sites", 10000));  
document.add(country);  
  
document.newPage();  
Anchor toUS = new Anchor("Go to first page.", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));  
toUS.setReference("#CN");//internal  
document.add(toUS);  
  
//Chapter, Section对象(目录)  
document.newPage();  
Paragraph title = new Paragraph("Title");  
Chapter chapter = new Chapter(title, 1);  
  
title = new Paragraph("Section A");  
Section section = chapter.addSection(title);  
section.setBookmarkTitle("bmk");  
section.setIndentation(30);  
section.setBookmarkOpen(false);  
section.setNumberStyle(  
Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT);  
  
Section subsection = section.addSection(new Paragraph("Sub Section A"));  
subsection.setIndentationLeft(20);  
subsection.setNumberDepth(1);  
  
document.add(chapter);  

设置段落 

Paragraph p = new Paragraph("在企业的信息系统中,报表处理一直占比较重要的作用,iText是一种生成PDF报表的Java组件。通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超链接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。");  
  
//默认  
p.setAlignment(Element.ALIGN_JUSTIFIED);  
document.add(p);  
  
document.newPage();  
p.setAlignment(Element.ALIGN_JUSTIFIED);  
p.setIndentationLeft(1 * 15f);  
p.setIndentationRight((5 - 1) * 15f);  
document.add(p);  
  
//居右  
document.newPage();  
p.setAlignment(Element.ALIGN_RIGHT);  
p.setSpacingAfter(15f);  
document.add(p);  
  
//居左  
document.newPage();  
p.setAlignment(Element.ALIGN_LEFT);  
p.setSpacingBefore(15f);  
document.add(p);  
  
//居中  
document.newPage();  
p.setAlignment(Element.ALIGN_CENTER);  
p.setSpacingAfter(15f);  
p.setSpacingBefore(15f);  
document.add(p);
// Code 1  
document.add(new Chunk("Chapter 1").setLocalDestination("1"));  
  
document.newPage();  
document.add(new Chunk("Chapter 2").setLocalDestination("2"));  
document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));  
document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));  
  
document.newPage();  
document.add(new Chunk("Chapter 3").setLocalDestination("3"));  
  
// Code 2  
PdfContentByte cb = writer.getDirectContent();  
PdfOutline root = cb.getRootOutline();  
  
// Code 3  
@SuppressWarnings("unused")  
PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1");  
  
PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");  
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), "Chapter 3"); 

幻灯片放映 

PdfWriter writer = PdfWriter.getInstance(doc, out);  
  
writer.setPdfVersion(PdfWriter.VERSION_1_5);  
  
writer.setViewerPreferences(PdfWriter.PageModeFullScreen);//全屏  
writer.setPageEvent(new PdfPageEventHelper() {  
    public void onStartPage(PdfWriter writer, Document document) {  
        writer.setTransition(new PdfTransition(PdfTransition.DISSOLVE, 3));  
        writer.setDuration(5);//间隔时间  
    }  
});  
  
doc.open();  
doc.add(new Paragraph("1 page"));  
doc.newPage();  
doc.add(new Paragraph("2 page"));  
doc.newPage();  
doc.add(new Paragraph("3 page"));  
doc.newPage();  
doc.add(new Paragraph("4 page"));  
doc.newPage();  
doc.add(new Paragraph("5 page"));  

插入图片

Image img = Image.getInstance("resource/test.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);//旋转  
img.scalePercent(15);  //设置图片大小比例
document.add(img);  

插入表格

//创建一个为三列的表格
Table table = new Table(3);
Cell cell = new Cell("第一个表格");
//设置宽度
cell.setWidth(width);
//设置垂直居中
cell.setVerticalAlignment(1); 
 //设置水平居中
cell.setHorizontalAlignment(1);
//横向合并两格
cell.setColspan(2);
table.addCell(cell);
//也可以直接添加字符串到表格中
table.addCell("第二个表格");

//表格中嵌套表格
Table table2 = new Table(3);
table2.addCell("11111");
table2.addCell("22222");
table2.addCell("33333");
table.insertTable(table2);

document.add(tables); 

HTML to PDF 

Document document = new Document(PageSize.LETTER);  
PdfWriter.getInstance(document, new FileOutputStream("c://testpdf1.pdf"));  
document.open();  
HTMLWorker htmlWorker = new HTMLWorker(document);  
htmlWorker.parse(new StringReader("<h1>This is a test!</h1>"));  
document.close();

 

等等功能具体参考Itext中文文档

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值