栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java中输出pdf文件代码分享

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java中输出pdf文件代码分享

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"));
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151738.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号