IText导出word

这篇博客介绍了如何在Java中使用IText库来创建Word文档。首先,展示了所需的依赖项,包括iText的不同版本。接着,提供了一个示例代码,详细展示了如何初始化Document对象,设置字体和样式,创建Table、Paragraph、Cell等元素,并将其添加到文档中。最后,还展示了文档的样式布局。
摘要由CSDN通过智能技术生成

1.加入依赖

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext-rtf</artifactId>
    <version>2.1.7</version>
</dependency>

2示例代码

package com.djy.dygl.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class WordUtil {

    public static void main(String[] args) throws IOException {
        Document document=null;
        try {
            document = new Document();
            RtfWriter2.getInstance(document, new FileOutputStream("D:/word.doc"));
            document.open();
            String os = System.getProperty("os.name");
            String path="";
            if(os.contains("Windows")) {
                path = "c:/windows/fonts/STXIHEI.TTF";//适用于windows系统
            }else {
                path = this.getClass().getResource("/").getPath() + "SIMKAI.TTF";//linux下获取文件路径
            }
            BaseFont baseFont = BaseFont.createFont(path,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);  
            Font f = new Font(baseFont, 12, Font.UNDERLINE);
            Font f1 = new Font(baseFont,12);
            Font f2 = new Font(baseFont,13,Font.BOLD);
            Font f3 = new Font(baseFont,11);
            Font f4 = new Font(baseFont,10,Font.BOLD);
            Font f5 = new Font(baseFont,15,Font.BOLD);
            Table table=new Table(18);//18列
            table.setBorderWidth(1);
            Paragraph p=new Paragraph("党员介绍信存根",f2);
            p.setIndentationLeft(10);//左边距10
            p.setIndentationRight(10);//右边距10
            Cell cell = new Cell(p);
            cell.setVerticalAlignment(Element.ALIGN_CENTER);//垂直居中
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
            cell.setColspan(2);//合并2列
            table.addCell(cell);
            //和chunk结合可实现一段文字多种字体
            Paragraph p1=new Paragraph("第20180310号",f1);
            p1.setIndentationRight(20);
            p1.setAlignment(Element.ALIGN_RIGHT);
            p1.setSpacingBefore(10);//段前10
            Paragraph p2=new Paragraph();
            p2.setAlignment(Element.ALIGN_JUSTIFIED);//自适应
            p2.setIndentationLeft(10);
            p2.setIndentationRight(10);
            p2.setSpacingBefore(10);
            p2.setSpacingAfter(10);//断后10
            Chunk c6=new Chunk("    ",f1);
            Chunk c1=new Chunk("XX",f);
            Chunk c2=new Chunk("同志系中共(预备/正式)党员,组织关系由",f1);
            Chunk c3=new Chunk("xxx",f);
            Chunk c4=new Chunk("转到",f1);
            Chunk c5=new Chunk("xxxxxx",f);
            p2.add(c6);
            p2.add(c1);
            p2.add(c2);
            p2.add(c3);
            p2.add(c4);
            p2.add(c5);
            
            Paragraph p3=new Paragraph("2018年10月30日",f1);
            p3.setIndentationRight(20);
            p3.setAlignment(Element.ALIGN_RIGHT);
            p3.setSpacingBefore(10);
            p3.setSpacingAfter(10);
            Cell cell2 = new Cell();
            cell2.add(p1);
            cell2.add(p2);
            cell2.add(p3);
            cell2.setColspan(15);
            table.addCell(cell2);
            
            Cell cell3= new Cell(new Phrase("第一联",f1));
            cell3.setVerticalAlignment(Element.ALIGN_CENTER);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setColspan(1);
            table.addCell(cell3);
            
            Paragraph p5=new Paragraph("(贴回执联处)",f3);
            p5.setAlignment(Element.ALIGN_CENTER);
            p5.setSpacingBefore(5);
            Paragraph p6=new Paragraph(".......................................................................................................................................................",f4);
            p6.setAlignment(Element.ALIGN_CENTER);
            p6.setSpacingAfter(10);
            
            document.add(table);
            document.add(p5);
            document.add(p6);
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }finally{
            if(document!=null){
                document.close();
            }
        }

    }

}

3.样式展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值