当前位置: 移动技术网 > IT编程>开发语言>Java > java中输出pdf文件代码分享

java中输出pdf文件代码分享

2019年07月22日  | 移动技术网IT编程  | 我要评论
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"));
	}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网