最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java開源工具iText生成PDF簡單實例

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

iText下載頁面: http://sourceforge.net/projects/itext/files/
1.創(chuàng)建簡單的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)文章

  • maven引入本地jar包運(yùn)行報錯java.lang.NoClassDefFoundError解決

    maven引入本地jar包運(yùn)行報錯java.lang.NoClassDefFoundError解決

    這篇文章主要為大家介紹了maven引入本地jar包運(yùn)行報錯java.lang.NoClassDefFoundError解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Spring Boot 集成 MongoDB Template 的步驟詳解

    Spring Boot 集成 MongoDB Template 的步驟

    MongoDB 是一個流行的 NoSQL 數(shù)據(jù)庫,適合處理大量非結(jié)構(gòu)化數(shù)據(jù),本篇文章將詳細(xì)介紹如何在 Spring Boot 3.4.0 中集成 MongoDB Template,從零開始構(gòu)建一個簡單的應(yīng)用程序,感興趣的朋友一起看看吧
    2024-12-12
  • java實現(xiàn)KFC點餐小程序

    java實現(xiàn)KFC點餐小程序

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)KFC點餐系統(tǒng)小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 淺談Spring refresh的工作流程

    淺談Spring refresh的工作流程

    這篇文章主要介紹了淺談Spring refresh的工作流程,refresh 是 AbstractApplicationContext 中的一個方法,負(fù)責(zé)初始化 ApplicationContext容器,讓我們一起來學(xué)習(xí)一下吧
    2023-04-04
  • Springboot中整合knife4j接口文檔的過程詳解

    Springboot中整合knife4j接口文檔的過程詳解

    knife4j就swagger的升級版API文檔的一個框架,但是用起來比swagger方便多了,UI更加豐富,這篇文章主要介紹了Springboot中整合knife4j接口文檔,需要的朋友可以參考下
    2022-04-04
  • SpringMVC上傳文件FileUpload使用方法詳解

    SpringMVC上傳文件FileUpload使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了SpringMVC上傳文件FileUpload的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • cookie、session和java過濾器結(jié)合實現(xiàn)登陸程序

    cookie、session和java過濾器結(jié)合實現(xiàn)登陸程序

    這篇文章主要為大家詳細(xì)介紹了cookie、session和java過濾器結(jié)合實現(xiàn)登陸程序的具體代碼,感興趣的朋友可以參考一下
    2016-05-05
  • Spring?Boot指定外部配置文件簡單示例

    Spring?Boot指定外部配置文件簡單示例

    Spring Boot可以讓你將配置外部化,這樣你就可以在不同的環(huán)境中使用相同的應(yīng)用程序代碼,這篇文章主要給大家介紹了關(guān)于Spring?Boot指定外部配置文件的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • 枚舉java語言中的修飾符組合的實例代碼

    枚舉java語言中的修飾符組合的實例代碼

    這篇文章主要介紹了枚舉java語言中的修飾符組合,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • Mybatis 緩存原理及失效情況解析

    Mybatis 緩存原理及失效情況解析

    這篇文章主要介紹了Mybatis 緩存原理及失效情況解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11

最新評論

凤冈县| 神木县| 潮州市| 乐清市| 股票| 成安县| 漯河市| 淳化县| 陈巴尔虎旗| 东乡县| 巴青县| 甘肃省| 迁安市| 宁南县| 沈丘县| 乾安县| 琼中| 开远市| 阳东县| 霞浦县| 祁连县| 改则县| 湛江市| 全椒县| 寿阳县| 徐闻县| 石景山区| 金阳县| 绥中县| 定日县| 宣恩县| 安图县| 荆门市| 岫岩| 偃师市| 新龙县| 寿阳县| 溧水县| 当阳市| 砚山县| 鹤庆县|