欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java開(kāi)源工具iText生成PDF簡(jiǎn)單實(shí)例

 更新時(shí)間:2015年07月06日 09:24:19   投稿:junjie  
這篇文章主要介紹了Java開(kāi)源工具iText生成PDF簡(jiǎn)單實(shí)例,本文給出了3段代碼實(shí)例,講解創(chuàng)建一個(gè)簡(jiǎn)單PDF文件,在PDF中添加表格以及在PDF中添加圖片,需要的朋友可以參考下

iText下載頁(yè)面: http://sourceforge.net/projects/itext/files/
1.創(chuàng)建簡(jiǎn)單的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文件中創(chuàng)建表格
 */
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();
    }
  }
}




相關(guān)文章

  • 用Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)板功能

    用Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)板功能

    這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java代理模式的深入了解

    Java代理模式的深入了解

    這篇文章主要為大家介紹了Java代理模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Java Spring @Lazy延遲注入源碼案例詳解

    Java Spring @Lazy延遲注入源碼案例詳解

    這篇文章主要介紹了Java Spring @Lazy延遲注入源碼案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 使用Netty搭建服務(wù)端和客戶端過(guò)程詳解

    使用Netty搭建服務(wù)端和客戶端過(guò)程詳解

    這篇文章主要介紹了使用Netty搭建服務(wù)端和客戶端過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • SpringBoot實(shí)現(xiàn)人臉識(shí)別等多種登錄方式

    SpringBoot實(shí)現(xiàn)人臉識(shí)別等多種登錄方式

    本文主要介紹了SpringBoot實(shí)現(xiàn)人臉識(shí)別等多種登錄方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • SpringBoot MockMvc單元測(cè)試的示例代碼

    SpringBoot MockMvc單元測(cè)試的示例代碼

    這篇文章主要介紹了SpringBoot MockMvc單元測(cè)試的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • Java文件寫(xiě)入器FileWriter使用指南

    Java文件寫(xiě)入器FileWriter使用指南

    在Java中,FileWriter類用于將字符寫(xiě)入文件中,它繼承了Writer類,因此可以使用Writer類中的所有方法,下面我們就來(lái)深入探討一下FileWriter類的使用方法吧
    2023-10-10
  • Kotlin 基礎(chǔ)語(yǔ)法實(shí)例詳解

    Kotlin 基礎(chǔ)語(yǔ)法實(shí)例詳解

    這篇文章主要介紹了Kotlin 基礎(chǔ)語(yǔ)法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • java如何獲取本機(jī)IP地址

    java如何獲取本機(jī)IP地址

    這篇文章主要為大家詳細(xì)介紹了java如何獲取本機(jī)IP地址,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Mybatis初始化知識(shí)小結(jié)

    Mybatis初始化知識(shí)小結(jié)

    Mybatis的初始化過(guò)程就是加載自己運(yùn)行時(shí)所需要的配置信息的過(guò)程,這篇文章主要介紹了Mybatis初始化知識(shí)小結(jié),需要的朋友可以參考下
    2021-10-10

最新評(píng)論