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

java中输出pdf文件代码分享

程序员文章站 2024-03-03 21:09:40
package snake; import java.io.file; import java.io.fileinputstream; import ja...
package snake;

import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;

import com.lowagie.text.document;
import com.lowagie.text.documentexception;
import com.lowagie.text.font;
import com.lowagie.text.paragraph;
import com.lowagie.text.pdf.basefont;
import com.lowagie.text.pdf.pdfwriter;

public class pdfutil {

	public static void printpdf(string content) throws documentexception,
			ioexception {
		document document = new document();

		pdfwriter.getinstance(document, new fileoutputstream(
				"d:/demo.pdf"));
		// basefont chinese = basefont.createfont("stsong-light",
		// "unigb-ucs2-h", basefont.not_embedded);
		// 使用windows自带的字体
		basefont chinese = basefont.createfont("c:/windows/fonts/simsun.ttc,1",
				basefont.identity_h, basefont.embedded);
		font font = new font(chinese, 14, font.normal);

		document.addauthor("westdream");
		document.addtitle("test itext");
		document.addsubject("this is an itext demo");
		document.addkeywords("itext keywords");
		document.addcreator("using itext");
		document.open();
		document.add(new paragraph(content, font));
		document.close();
	}

	public static void main(string[] args) throws exception {
		file file = new file("c:\\users\\zan\\desktop\\demo.txt");
		byte[] bytes = new byte[(int) file.length()];
		inputstream input = new fileinputstream(file);
		input.read(bytes, 0, (int)file.length());
		printpdf(new string(bytes,"gbk"));
	}
}