天天看點

java中利用itext.jar生成word文檔

import java.io.fileoutputstream;

import java.io.ioexception;

import com.lowagie.text.document;

import com.lowagie.text.documentexception;

import com.lowagie.text.font;

import com.lowagie.text.headerfooter;

import com.lowagie.text.pagesize;

import com.lowagie.text.paragraph;

import com.lowagie.text.phrase;

import com.lowagie.text.rectangle;

import com.lowagie.text.pdf.basefont;

import com.lowagie.text.rtf.rtfwriter2;

public class rules {

 public static void main(string[] args) throws documentexception, ioexception {

  document document = new document(pagesize.a4);

  rtfwriter2.getinstance(document, new fileoutputstream("e:/1.doc"));

  document.open();

  // 添加頁眉

  headerfooter header = new headerfooter(new phrase("header"), false);

  header.setalignment(rectangle.align_center);

  document.setheader(header);

  // 添加頁腳

  headerfooter footer = new headerfooter(new phrase("footer"), false);

  footer.setalignment(rectangle.align_center);

  document.setfooter(footer);

  // 設定中文字型

  basefont bfchinese = basefont.createfont("stsongstd-light", "unigb-ucs2-h", basefont.not_embedded);

  font contextfont = new font(bfchinese, 10, font.normal);

  paragraph context = new paragraph("");

  context.setfont(contextfont);

  //image png = image.getinstance("d:/busy.gif");

  //png.setabsoluteposition(0, 0);  

  //png.setalignment(image.textwrap);

  context.add("内容1");

  //context.add(new phrase(new chunk(png, 0, 0, true)));

  context.add("内容2");

  context.add("内容3");

  document.add(context);

  document.close();

 }

}

原帖位址:http://housen1987.iteye.com/blog/1335036