java 經過使用itext 生成2003 word 文檔(java生成word文檔2)

public static RtfWriter2 getDoc() throws Exception {
		// 設置紙張大小
		Document document = new Document(PageSize.A4);
		// 創建一個書寫器(Writer)與document對象關聯,經過書寫器(Writer)能夠將文檔寫入到磁盤中
		RtfWriter2 writer = RtfWriter2.getInstance(document,
				new FileOutputStream(new File("d:\\hello.doc")));
		document.open();
		// 設置中文字體
		BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 標題字體風格
		Font titleFont = new Font(bfChinese, 12, Font.BOLD);
		// 正文字體風格
		Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
		Font parTitle = new Font(bfChinese, 10, Font.BOLD);
		Paragraph title = new Paragraph("標題");
		// 設置標題格式對齊方式
		title.setAlignment(Element.ALIGN_CENTER);
		title.setFont(titleFont);
		document.add(title);

		Paragraph paragraph = new Paragraph();
		paragraph.setAlignment(Element.ALIGN_LEFT);
		paragraph.setFirstLineIndent(20);

		Chunk chunk1 = new Chunk("這是一句話");
		chunk1.setFont(parTitle);
		paragraph.add(chunk1);

		Chunk chunk = new Chunk();
		chunk.append("這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話這是另外一句話");
		chunk.setFont(contextFont);
		paragraph.add(chunk);

		document.add(paragraph);

		Paragraph paragraph2 = new Paragraph("很正常很正常很正常很正常很正常很正常很正常很正常很正常");
		paragraph2.setAlignment(Element.ALIGN_LEFT);
		paragraph2.setFirstLineIndent(20);
		document.add(paragraph2);

		document.close();
		return writer;
	}
 所需jar包在下面
相關文章
相關標籤/搜索