iText处理Word文档

用到的jar

iText-2.1.7.jar

iText-rtf-2.1.7.jar

iTextAsian.jar

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

import java.awt.Color;

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

public class RTFDemo2 {
    public RTFDemo2() {
        super();
    }

    public static void main(String[] args) throws Exception {
        test("E:\\demo6.doc");
    }

    public static void test(String path) throws FileNotFoundException, DocumentException, IOException {
        //创建word文档,并设置纸张大小
        Document doc = new Document(PageSize.A4);
        //创建RtfWriter2对象,与Document对象关联
        RtfWriter2.getInstance(doc, new FileOutputStream(path));
        doc.open();
        //设置中文字体
        BaseFont cnFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font titleFont = new Font(cnFont, 12, Font.BOLD, new Color(255, 0, 0)); //标题字体
        Font contentFont = new Font(cnFont, 10, Font.NORMAL); //正文字体
        //创建一个段落
        Paragraph titlePara = new Paragraph("这是标题", titleFont);
        titlePara.setAlignment(Element.ALIGN_CENTER); //p.setAlignment(1);
        doc.add(titlePara);
        Paragraph contentPara =
            new Paragraph("语言的学习与处理,知识表现,智能搜索,推理,规划,机器学习,知识获取,组合调度问题,感知问题,模式识别,逻辑程序设计,软计算,不精确和不确定的管理,人工生命,神经网络,复杂系统,遗传算法人类思维方式,最关键的难题还是机器的自主创造性思维能力的塑造与提升。");
        contentPara.setFont(contentFont);
        contentPara.setAlignment(Element.ALIGN_LEFT);
        contentPara.setSpacingBefore(3); //段间距
        contentPara.setFirstLineIndent(20);
        doc.add(contentPara);
        //创建表格
        Table table = new Table(3); //参数个数可为0,1,2
        table.setWidth(90); //占页面宽度比例
        table.setWidths(new int[] { 25, 25, 50 }); //每列宽度比例
        table.setAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.setAutoFillEmptyCells(true);
        table.setBorderWidth(1);
        table.setBorderColor(Color.blue);
        //设置表头
        Cell headerCell = new Cell("表格表头");
        headerCell.setHeader(true);
        headerCell.setColspan(3);
        table.addCell(headerCell);
        table.endHeaders();
        Cell cell = new Cell(new Paragraph("#1", contentFont));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
        table.addCell(new Cell("#2"));
        table.addCell(new Cell("#3"));
        table.addCell(new Cell("#4"));
        Cell dblCell = new Cell("当前单元格占两行");
        dblCell.setColspan(2);
        table.addCell(dblCell);
        //插入图片
        Image img=Image.getInstance("E:\\luffy.jpg");
        Cell imgCell = new Cell(img);
        table.addCell(imgCell);
        doc.add(table);
        doc.close();
    }
}

转载于:https://my.oschina.net/u/3646781/blog/1529371

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值