java导出pdf

package com.pdf;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
 
public class createPdf {
	//自己做的一个简单例子,中间有图片之类的
	//先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document 
    Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);
	public  void getPDFdemo() throws DocumentException, IOException{
		//这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见:
		//首先
		//字体的定义:这里用的是自带的jar里面的字体
		BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
		// 当然你也可以用你电脑里面带的字体库
		//BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
	    //定义字体 注意在最新的包里面 颜色是封装的
	    Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));
		//生成pdf的第一个步骤:
	    //保存本地指定路径
	     saveLocal();
		 document.open();
		 ByteArrayOutputStream ba = new ByteArrayOutputStream();       
	//	 PdfWriter writer = PdfWriter.getInstance(document, ba);
		 document.open();
		 //获取此编译的文件路径
		 String path = this.getClass().getClassLoader().getResource("").getPath();
		 //获取根路径
		 String filePath  = path.substring(1, path.length()-15);
		 //获取图片路径 找到你需要往pdf上生成的图片  
		 //这里根据自己的获取的路径写 只要找到图片位置就可以
		 String  picPath = filePath +"\\WebContent" +"\\images\\";
		 //往PDF中添加段落
		 Paragraph pHeader = new Paragraph();
		 pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1)));
		 //pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 );
		 document.add(pHeader);//在文档中加入你写的内容
		//获取图片 
		 Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png");
		//定义图片在文档中显示的绝对位置
		 img2.scaleAbsolute(137.0F, 140.0F);
	     img2.setAbsolutePosition(330.0F, 37.0F);
		 //将图片添加到文档中
         document.add(img2);
		 //关闭文档
		 document.close();
		/*//设置文档保存的文件名
		 response.setHeader("Content-disposition", "attachment;filename=\""+ new String(("CCF会员资格确认函.pdf").getBytes("GBK"),"ISO-8859-1") + "\"");
		//设置类型
	     response.setContentType("application/pdf");
	     response.setContentLength(ba.size());
	     ServletOutputStream out = response.getOutputStream();
	     ba.writeTo(out);
	     out.flush();*/
	}
	public static void main(String[]args) throws DocumentException, IOException{
		createPdf pdf= new createPdf();
		pdf.getPDFdemo();
	}
	
	//指定一个文件进行保存 这里吧文件保存到D盘的text.pdf
	public void saveLocal() throws IOException, DocumentException{
		//直接生成PDF 制定生成到D盘test.pdf
		 File file = new File("D:\\text2.pdf");
		 file.createNewFile();
		 PdfWriter.getInstance(document, new FileOutputStream(file));
		 
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值