java中生成pdf,插入图片,页眉、页脚、表格

全栈工程师开发手册 (作者:栾鹏)

java教程全解

java中生成pdf,插入图片,页眉、页脚、表格

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.*;
import java.awt.Color;

//Description: 本实例通过使用iText包生成一个表格的PDF文件
public class MyPdf{
  public static void main(String[] arg){
	  write_pdf("table.pdf");
  }
	//写PDF文件
  public static void write_pdf(String filepath)
  {
     try{
	     //设置pdf文件输出流
	     Document document=new Document(PageSize.A4);  
	     document.setMargins(50, 50, 100, 50);      //pdf的4个页边距
	     Rectangle pageRect=document.getPageSize();
	     PdfWriter.getInstance(document, new FileOutputStream(filepath));
	     //创建汉字字体
	     BaseFont bfSong = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
	     Font fontSong = new Font(bfSong, 10, Font.NORMAL);
	     
	     //添加一个图片,设置图片的位置,大小
	     try {
	         Watermark watermark = new Watermark(Image.getInstance("test.jpg"), pageRect.left()+50,pageRect.top()-85);
	         watermark.scalePercent(50);
	         document.add(watermark);
	     }catch(Exception e) {
			  System.err.println("找不到图片");
	     }
	     
	      //增加页头信息
	     HeaderFooter header = new HeaderFooter(new Phrase("Java实例一百例",fontSong), false);   //设置页眉文字
	     header.setBorder(2);  		//设置边框
	     header.setAlignment(Element.ALIGN_RIGHT);   //设置对齐方式
	     document.setHeader(header);
	     
		  //增加页脚信息
	     HeaderFooter footer = new HeaderFooter(new Phrase("第 ",fontSong),new Phrase(" 页",fontSong));  //设置页脚文字
	     footer.setAlignment(Element.ALIGN_CENTER);   //设置对齐方式
	     footer.setBorder(1);   //设置边框
	     document.setFooter(footer);
	
	      // 打开文档
	     document.open(); 
	     //添加表格
	     Table table = new Table(4);  //设置Table列数
	     table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);  //表格垂直对齐方式
	     table.setBorder(Rectangle.NO_BORDER);   //设置边框
	     int hws[] = {10, 20, 10, 20};   //按比例分配单元格宽度
	     table.setWidths(hws);
	     table.setWidth(100);    //设置表格占据的宽度比例
	     //表头信息
	     Cell cellmain = new Cell(new Phrase("用户信息",new Font(bfSong, 10, Font.BOLD,new Color(0,0,255)))); //创建单元格
	     cellmain.setHorizontalAlignment(Element.ALIGN_CENTER);  //设置水平对齐方式
	     cellmain.setColspan(4);   //设置占据列数
	     cellmain.setBorder(Rectangle.NO_BORDER); //设置边框
	     cellmain.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));   //设置背景色
	     table.addCell(cellmain);   //添加单元格
	      //分表头信息
	     Cell cellleft= new Cell(new Phrase("收货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));//创建单元格
	     cellleft.setColspan(2); //设置占据列数
	     cellleft.setHorizontalAlignment(Element.ALIGN_CENTER); //设置水平对齐方式
	     table.addCell(cellleft);
	     Cell cellright= new Cell(new Phrase("订货人信息",new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
	     cellright.setColspan(2);//设置占据列数
	     cellright.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平对齐方式
	     table.addCell(cellright);
	     
	     //收货和订货人信息,表体内容
	     table.addCell(new Phrase("姓名",fontSong));  //添加一个单元格
	     table.addCell(new Phrase("张三",fontSong));  //添加一个单元格
	     table.addCell(new Phrase("姓名",fontSong));  //添加一个单元格
	     table.addCell(new Phrase("李四",fontSong));  //添加一个单元格
	
	     table.addCell(new Phrase("电话",fontSong));  //添加一个单元格
	     table.addCell(new Phrase("23456789",fontSong));  //添加一个单元格
	     table.addCell(new Phrase("电话",fontSong));  //添加一个单元格
	     table.addCell(new Phrase("9876543",fontSong));  //添加一个单元格
	
	     //将表格添加到文本中
	     document.add(table);
	     //关闭文本,释放资源
	     document.close(); 
     
     }catch(Exception e){
         System.out.println(e);   
     }
  }


}
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

腾讯AI架构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值