Java用iText5生成PDF

jar包:itext-asian-5.2.0.jar itextpdf-5.5.5.jar itext-1.4.6.jar

Java用iText5操作PDF itext-asian-5.2.0.jar itextpdf-5.5.5.jar itext-1.4.6.jar

说明

新建document对象

  1. Document document =new Document(); // 默认页面大小是A4
  2. Document document =new Document(PageSize.A4); // 指定页面大小为A4
  3. Document document =new Document(PageSize.A4,50,50,30,20); // 指定页面大小为A4,且自定义页边距
     

PdfWriter 对象进行磁盘写入操作

PdfWriter.getInstance(document, new FileOutputStream(filePath));

打开文档

document.open();

向文档中添加内容

document.add();

关闭文档

document.close();

段落Paragraph

Paragraph pt=new Paragraph(name,headfont);//设置字体样式

pt.setAlignment(1);//设置文字居中 0靠左 1,居中 2,靠右

pt.setIndentationLeft(12);// 左缩进

pt.setIndentationRight(12);// 右缩进

pt.setFirstLineIndent(24);// 首行缩进

paragraph.setLeading(20f); //行间距

paragraph.setSpacingBefore(5f); //设置段落上空白

paragraph.setSpacingAfter(10f); //设置段落下空白

表格table

      Table table =new Table(4);//括号参数表示列
      int width[] = {10,40,40};//设置每列宽度比例

      table.setWidths(width);

      table.setWidth(60);//占页面宽度比例

      table.setAlignment(Element.ALIGN_LEFT);//居左

      table.setAutoFillEmptyCells(true);//自动填满

      table.setBorderWidth((float)0.1);//表格边框线条宽度

      table.setPadding(1);//边距:单元格的边线与单元格内容的边距

      table.setSpacing(0);//间距:单元格与单元格之间的距离

      table.addCell(new Paragraph("name"),"单元格"));//添加单元格内容

      table.endHeaders();//每页都会显示表头

实例

生成一个只有标题的pdf


import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfJava
{

   public void pdfTest(String filePath) throws Exception
   {
      Document document = new Document(PageSize.A4, 36, 36, 36, 36);
      PdfWriter.getInstance(document, new FileOutputStream(filePath));

      document.open();

      
      //中文字体,解决中文不能显示问题
      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
      Font cusTitleFont = new Font(bfChinese);
      cusTitleFont.setSize(16);

      Font cusFont = new Font(bfChinese);
      cusFont.setSize(9);

      //添加标题开始
      Font redFont = new Font(bfChinese);
      redFont.setColor(BaseColor.RED);//设置颜色
      redFont.setSize(20);//设置字体大小
      Paragraph chapterTitle = new Paragraph("java生成pdf文件", redFont);//设置内容
      chapterTitle.setAlignment(1);

      Chapter chapter1 = new Chapter(chapterTitle, 1);
      chapter1.setNumberDepth(0);

      document.add(chapter1);
      //添加标题结束
      
      //关闭文档
      document.close();
      
   }

   /**
    * @param args
    */
   public static void main(String[] args)
   {
      try
      {
         String filePath="d:\\test1.pdf";
         new PdfJava().pdfTest(filePath);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

   }

}

文件如下:

生成table内容


import java.io.FileOutputStream;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPRow;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfJava
{

   public void pdfTest(String filePath) throws Exception
   {
      Document document = new Document(PageSize.A4, 36, 36, 36, 36);
      PdfWriter.getInstance(document, new FileOutputStream(filePath));

      document.open();

      
      //中文字体,解决中文不能显示问题
      BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
      Font cusTitleFont = new Font(bfChinese);
      cusTitleFont.setSize(16);

      Font cusFont = new Font(bfChinese);
      cusFont.setSize(9);

      //添加标题开始
      Font redFont = new Font(bfChinese);
      redFont.setColor(BaseColor.RED);//设置颜色
      redFont.setSize(20);//设置字体大小
      Paragraph chapterTitle = new Paragraph("java生成pdf文件", redFont);//设置内容
      chapterTitle.setAlignment(1);

      Chapter chapter1 = new Chapter(chapterTitle, 1);
      chapter1.setNumberDepth(0);

      document.add(chapter1);
      //添加标题结束
      
      
    //添加一个内容段落
      document.add(new Paragraph("表格信息", cusTitleFont));
      // 表格.
      PdfPTable table = new PdfPTable(6);
      table.setWidthPercentage(100); // 宽度100%填充
      table.setSpacingBefore(10f); // 前间距
      table.setSpacingAfter(10f); // 后间距

      List<PdfPRow> listRow = table.getRows();
      //设置列宽
      float[] columnWidths = { 2f, 2f, 2f ,2f, 2f, 2f};
      table.setWidths(columnWidths);

      //行
      PdfPCell cells1[] = new PdfPCell[6];
      PdfPRow row1 = new PdfPRow(cells1);

      //单元格
      cells1[0] = new PdfPCell(new Paragraph("第一行第1列:", cusFont));//单元格内容
      cells1[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells1[1] = new PdfPCell(new Paragraph("第一行第1列值", cusFont));
      cells1[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells1[2] = new PdfPCell(new Paragraph("第一行第2列:", cusFont));//单元格内容
      cells1[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells1[3] = new PdfPCell(new Paragraph("第一行第2列值", cusFont));
      cells1[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells1[4] = new PdfPCell(new Paragraph("第一行第3列:", cusFont));//单元格内容
      cells1[4].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells1[5] = new PdfPCell(new Paragraph("第一行第3列值", cusFont));
      cells1[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      //把第一行添加到集合
      listRow.add(row1);
      
    //行
      PdfPCell cells2[] = new PdfPCell[6];
      PdfPRow row2 = new PdfPRow(cells2);

      //单元格
      cells2[0] = new PdfPCell(new Paragraph("第二行第1列:", cusFont));//单元格内容
      cells2[0].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells2[1] = new PdfPCell(new Paragraph("第二行第1列值", cusFont));
      cells2[1].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells2[2] = new PdfPCell(new Paragraph("第二行第2列:", cusFont));//单元格内容
      cells2[2].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells2[3] = new PdfPCell(new Paragraph("第二行第2列值", cusFont));
      cells2[3].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      
      cells2[4] = new PdfPCell(new Paragraph("第二行第3列:", cusFont));//单元格内容
      cells2[4].setHorizontalAlignment(Element.ALIGN_RIGHT);//水平居右

      cells2[5] = new PdfPCell(new Paragraph("第二行第3列值", cusFont));
      cells2[5].setHorizontalAlignment(Element.ALIGN_LEFT);//水平居左
      //把第二行添加到集合
      listRow.add(row2);
      
      //把表格添加到文件中
      document.add(table);
      
      //关闭文档
      document.close();
      
   }

   /**
    * @param args
    */
   public static void main(String[] args)
   {
      try
      {
         String filePath="d:\\test1.pdf";
         new PdfJava().pdfTest(filePath);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }

   }

}

文件如下:

加入公章(图片)

   
      Image image1 = Image.getInstance("d:\\gongzhang.jpg");
      //设置图片位置的x轴和y轴
      image1.setAbsolutePosition(100, 500);//注意是从文档的坐下角往右、往上计算的
      //设置图片的宽度和高度
      image1.scaleAbsolute(100, 100);
      //将图片1添加到pdf文件中
      document.add(image1);
      

加入作者信息

 //Anchor对象: internal and external links  
      Paragraph country = new Paragraph();  
      Anchor dest = new Anchor("china", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));  
      dest.setName("CN");  
      dest.setReference("http://www.china.com");//external  
      country.add(dest);  
      country.add(String.format(": %d sites", 10000));  
      document.add(country); 

加入跳转到指定位置(点击跳转)

  document.newPage();  
      Anchor toUS = new Anchor("Go to first page.", new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.BLUE));  
      toUS.setReference("#CN");//internal  
      document.add(toUS);  

HTML to PDF 

Java用iText5操作PDF代码及相关jar下载

猜你喜欢

转载自blog.csdn.net/dkm123456/article/details/114701470