六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 75|回复: 0

使用itext生成word文件并包含图片

[复制链接]

升级  0.67%

11

主题

11

主题

11

主题

秀才

Rank: 2

积分
51
 楼主| 发表于 2013-1-27 05:15:43 | 显示全部楼层 |阅读模式
这是在网上找到的别人写的代码 ,利用itext2 的版本只是自己修改了一个地方
RtfWriter2.getInstance(document, new FileOutputStream("C:/a.rtf"));



package test;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.rtf.RtfWriter2;

/*
* Created on 2004-11-10
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class RTFCreate {

public static void main(String[] args) throws MalformedURLException,
IOException {
RTFCreate rtfCreate = new RTFCreate();
try {
rtfCreate.createRTF();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void createRTF() throws DocumentException, BadElementException,
MalformedURLException, IOException {
Document document = new Document(PageSize.A4);
// document.addTitle("Title");
// document.addHeader("header","Header");

RtfWriter2.getInstance(document, new FileOutputStream("C:/a.rtf"));

document.open();
// add a word
document.add(new Paragraph("Hello World!您好!hehe!"));
// add a table
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);

table.addCell("cell test2");


document.add(table);

Image png = Image.getInstance("a.png");     
document.add(png);
document.add(new Paragraph("Hello World!您好!hehe!"));
document.close();
}
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表