欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java开源工具iText生成PDF简单实例

程序员文章站 2024-03-05 14:52:56
itext下载页面: 1.创建简单的pdf文件 package console.pdf; import java.io.filenotfoundexce...

itext下载页面:
1.创建简单的pdf文件

package console.pdf;

import java.io.filenotfoundexception;
import java.io.fileoutputstream;

import com.itextpdf.text.document;
import com.itextpdf.text.documentexception;
import com.itextpdf.text.pagesize;
import com.itextpdf.text.paragraph;
import com.itextpdf.text.pdf.pdfwriter;

/**
 * 使用itext生成pdf文件
 */
public class createpdf {

  public static void main(string[] args) {
    createpdf p001 = new createpdf();

    string filename = "p001.pdf";
    p001.createpdf(filename);
  }

  public void createpdf(string filename) {
    // step 1
    document document = new document(pagesize.a4);
    // step 2
    try {
      pdfwriter.getinstance(document, new fileoutputstream(filename));
      
      document.addtitle("id.net");
      document.addauthor("dotuian"); 
      document.addsubject("this is the subject of the pdf file."); 
      document.addkeywords("this is the keyword of the pdf file.");
      
      // step 3
      document.open();
      // step 4
      document.add(new paragraph("hello world!"));

      
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (documentexception e) {
      e.printstacktrace();
    } finally {
      // step 5
      document.close();
    }
  }

  
  
  
}


2.在pdf文件中添加table

package console.pdf;

import java.io.filenotfoundexception;
import java.io.fileoutputstream;

import com.itextpdf.text.basecolor;
import com.itextpdf.text.document;
import com.itextpdf.text.documentexception;
import com.itextpdf.text.pagesize;
import com.itextpdf.text.phrase;
import com.itextpdf.text.rectangle;
import com.itextpdf.text.pdf.pdfpcell;
import com.itextpdf.text.pdf.pdfptable;
import com.itextpdf.text.pdf.pdfwriter;

/**
 * 使用itext生成pdf文件
 * 在pdf文件中创建表格
 */
public class tableofpdf {

  public static void main(string[] args) {
    tableofpdf p001 = new tableofpdf();

    string filename = "p002.pdf";
    p001.createpdf(filename);
  }

  public void createpdf(string filename) {
    // step 1
    document document = new document(pagesize.a4);
    // step 2
    try {
      pdfwriter.getinstance(document, new fileoutputstream(filename));

      document.addtitle("id.net");
      document.addauthor("dotuian");
      document.addsubject("this is the subject of the pdf file.");
      document.addkeywords("this is the keyword of the pdf file.");

      // step 3
      document.open();
      // step 4
      pdfptable table = createtable1();
      document.add(table);
      
      table = createtable2();
      table.setspacingbefore(5);
      table.setspacingafter(5);
      document.add(table);
      
      table = createtable3();
      document.add(table);
      
      table = createtable4();
      table.setspacingbefore(5);
      table.setspacingafter(5);
      document.add(table);
      
      table = createtable5();
      document.add(table);
      
      table = createtable6();
      document.add(table);

    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (documentexception e) {
      e.printstacktrace();
    } finally {
      // step 5
      document.close();
    }
  }

  /**
   * creates a table; widths are set with setwidths().
   * 
   * @return a pdfptable
   * @throws documentexception
   */
  public static pdfptable createtable1() throws documentexception {
    pdfptable table = new pdfptable(3);
    table.setwidthpercentage(288 / 5.23f);
    table.setwidths(new int[] { 2, 1, 1 });
    
    pdfpcell cell;
    cell = new pdfpcell(new phrase("table 1"));
    cell.setcolspan(3);
    table.addcell(cell);
    
    cell = new pdfpcell(new phrase("cell with rowspan 2"));
    cell.setrowspan(2);
    table.addcell(cell);
    table.addcell("row 1; cell 1");
    table.addcell("row 1; cell 2");
    table.addcell("row 2; cell 1");
    table.addcell("row 2; cell 2");
    return table;
  }

  /**
   * creates a table; widths are set with setwidths().
   * 
   * @return a pdfptable
   * @throws documentexception
   */
  public static pdfptable createtable2() throws documentexception {
    pdfptable table = new pdfptable(3);
    table.settotalwidth(288);
    table.setlockedwidth(true);
    table.setwidths(new float[] { 2, 1, 1 });
    pdfpcell cell;
    cell = new pdfpcell(new phrase("table 2"));
    cell.setcolspan(3);
    table.addcell(cell);
    cell = new pdfpcell(new phrase("cell with rowspan 2"));
    cell.setrowspan(2);
    table.addcell(cell);
    table.addcell("row 1; cell 1");
    table.addcell("row 1; cell 2");
    table.addcell("row 2; cell 1");
    table.addcell("row 2; cell 2");
    return table;
  }

  /**
   * creates a table; widths are set in the constructor.
   * 
   * @return a pdfptable
   * @throws documentexception
   */
  public static pdfptable createtable3() throws documentexception {
    pdfptable table = new pdfptable(new float[] { 2, 1, 1 });
    table.setwidthpercentage(55.067f);
    pdfpcell cell;
    cell = new pdfpcell(new phrase("table 3"));
    cell.setcolspan(3);
    table.addcell(cell);
    cell = new pdfpcell(new phrase("cell with rowspan 2"));
    cell.setrowspan(2);
    table.addcell(cell);
    table.addcell("row 1; cell 1");
    table.addcell("row 1; cell 2");
    table.addcell("row 2; cell 1");
    table.addcell("row 2; cell 2");
    return table;
  }

  /**
   * creates a table; widths are set with special setwidthpercentage() method.
   * 
   * @return a pdfptable
   * @throws documentexception
   */
  public static pdfptable createtable4() throws documentexception {
    pdfptable table = new pdfptable(3);
    rectangle rect = new rectangle(523, 770);
    table.setwidthpercentage(new float[] { 144, 72, 72 }, rect);
    pdfpcell cell;
    cell = new pdfpcell(new phrase("table 4"));
    cell.setcolspan(3);
    table.addcell(cell);
    cell = new pdfpcell(new phrase("cell with rowspan 2"));
    cell.setrowspan(2);
    table.addcell(cell);
    table.addcell("row 1; cell 1");
    table.addcell("row 1; cell 2");
    table.addcell("row 2; cell 1");
    table.addcell("row 2; cell 2");
    return table;
  }

  /**
   * creates a table; widths are set with settotalwidth().
   * 
   * @return a pdfptable
   * @throws documentexception
   */
  public static pdfptable createtable5() throws documentexception {
    pdfptable table = new pdfptable(3);
    table.settotalwidth(new float[] { 144, 72, 72 });
    table.setlockedwidth(true);
    pdfpcell cell;
    cell = new pdfpcell(new phrase("table 5"));
    cell.setcolspan(3);
    table.addcell(cell);
    cell = new pdfpcell(new phrase("cell with rowspan 2"));
    cell.setrowspan(2);
    table.addcell(cell);
    table.addcell("row 1; cell 1");
    table.addcell("row 1; cell 2");
    table.addcell("row 2; cell 1");
    table.addcell("row 2; cell 2");
    return table;
  }
  
  public static pdfptable createtable6() throws documentexception{
    pdfptable table = new pdfptable(10);
    table.settotalwidth(595);
    //table.setlockedwidth(true);
    
    
    pdfpcell cell;
    cell = new pdfpcell(new phrase("table 6"));
    cell.setcolspan(10);
    table.addcell(cell);
    
    for (int i = 1; i < 100; i++) {
      cell = new pdfpcell(new phrase(string.valueof(i)));
      cell.setbackgroundcolor(basecolor.light_gray);
      table.addcell(cell);
    }
    
    
//    cell = new pdfpcell(new phrase("cell with rowspan 2"));
//    cell.setrowspan(2);
//    table.addcell(cell);
//    table.addcell("row 1; cell 1");
//    table.addcell("row 1; cell 2");
//    table.addcell("row 2; cell 1");
//    table.addcell("row 2; cell 2");
    return table;
  }
  
  

}



3.在pdf文件中添加图片,并且指定文本位置

package console.pdf;

import java.io.filenotfoundexception;
import java.io.fileoutputstream;

import com.itextpdf.text.document;
import com.itextpdf.text.documentexception;
import com.itextpdf.text.image;
import com.itextpdf.text.pagesize;
import com.itextpdf.text.pdf.basefont;
import com.itextpdf.text.pdf.pdfcontentbyte;
import com.itextpdf.text.pdf.pdfwriter;

/**
 * 使用itext生成pdf文件
 * 在pdf文件中添加背景图片,并指定文本在pdf文件中的位置
 */
public class backgroundimageofpdf {

  public static void main(string[] args) {
    backgroundimageofpdf p001 = new backgroundimageofpdf();

    string filename = "p003.pdf";
    p001.createpdf(filename);
  }

  public void createpdf(string filename) {
    // step 1
    document document = new document(pagesize.a4.rotate(),0,0,0,0);
    // step 2
    try {
      pdfwriter pdfwriter = pdfwriter.getinstance(document, new fileoutputstream(filename));
      
      document.addtitle("id.net");
      document.addauthor("dotuian"); 
      document.addsubject("this is the subject of the pdf file."); 
      document.addkeywords("this is the keyword of the pdf file.");
      
      // step 3
      document.open();
      // step 4
      image image = image.getinstance("bg.jpg");
      document.add(image);

      pdfcontentbyte pdfcontentbyte = pdfwriter.getdirectcontent();
      pdfcontentbyte.begintext();
      basefont bf = basefont.createfont(basefont.times_roman,basefont.winansi,basefont.embedded);
      pdfcontentbyte.setfontandsize(bf, 12);

      for (int i = 0; i <= 842; i = i + 50) {
        for (int j = 0; j <= 595; j = j + 20) {
          pdfcontentbyte.settextmatrix(i, j);
          pdfcontentbyte.showtext("(" + i + ":" + j + ")");
        }
      }
      
      pdfcontentbyte.endtext();
      
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (documentexception e) {
      e.printstacktrace();
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      // step 5
      document.close();
    }
  }
}