欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iText生成 PDF / Word

程序员文章站 2024-03-24 12:07:52
...
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter;

public class Test {

	/**
	 * @param args
	 */
	
	static ResourceBundle resourceBundle;
	
	public static void main(String[] args) {

		generatePDF();
		generateWord();

	}
	
	public void test(){
		try {
			FileInputStream fis = new FileInputStream("file path name");
			resourceBundle = new PropertyResourceBundle(fis);
			
			this.getClass().getClassLoader();
			InputStream is = ClassLoader.getSystemResourceAsStream("file");	
			resourceBundle = new PropertyResourceBundle(is);
			Enumeration enumeration = resourceBundle.getKeys();
			while ( enumeration.hasMoreElements() ) {
				String key = (String) enumeration.nextElement();
				String value = resourceBundle.getString(key);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void generatePDF(){
		try {
			Document document = new Document();
			PdfWriter. getInstance(document, new FileOutputStream("D:\\test.pdf"));
			document.open();
			document.add(new Paragraph( "pride in me!" ));
			document.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
		  e.printStackTrace();
		}
	}
	
	public static void generateWord(){
		try {
			Document document2 = new Document(PageSize.A4);
			RtfWriter. getInstance(document2, new FileOutputStream("D:\\test.doc" ));
			document2.open();
			Paragraph title = new Paragraph("你好 地球人..." );
			document2.add(title); 
			document2.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

}