日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

itextpdf添加表格元素_java使用iText生成pdf表格详解

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 itextpdf添加表格元素_java使用iText生成pdf表格详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先需要你自己下載itext相關的jar包并添加引用,或者在maven中添加如下引用配置:

com.lowagie

iText

2.1.5

com.lowagie

iText-rtf

2.1.4

com.lowagie

iTextAsian

2.1.4

如下代碼:

package com.iText.create;

import java.awt.Color;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import com.lowagie.text.Cell;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;

import com.lowagie.text.Element;

import com.lowagie.text.Font;

import com.lowagie.text.HeaderFooter;

import com.lowagie.text.Image;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Phrase;

import com.lowagie.text.Table;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

/** *//**

* 功能描述:使用Itext組件創建pdf文檔

* 創建時間:2010-07-01

* @author sxyx2008

*

*/

public class CreatePdf {

public CreatePdf() throws Exception{

//創建一個文檔對象紙張大小為A4

Document doc=new Document(PageSize.A4,50,50,50,50);

//設置要輸出到磁盤上的文件名稱

PdfWriter writer=PdfWriter.getInstance(doc,new FileOutputStream(new File("徐熙媛.pdf")));

//設置作者信息

doc.addAuthor("sxyx2008");

//設置文檔創建日期

doc.addCreationDate();

//設置標題

doc.addTitle("iText測試");

//設置值主題

doc.addSubject("iText");

//構建頁腳

HeaderFooter footer=new HeaderFooter(new Phrase(), true);

//設置頁腳是否有邊框

//0表示無

//1上邊框

//2下邊框

//3上下邊框都有 默認都有

//設置頁腳是否有邊框

footer.setBorder(0);

//footer.setBorder(1);

//footer.setBorder(2);

//footer.setBorder(3);

//設置頁腳的對齊方式

footer.setAlignment(Element.ALIGN_CENTER);

//將頁腳添加到文檔中

doc.setFooter(footer);

//打開文檔開始寫內容

doc.open();

//Paragraph par1=new Paragraph("Hello,Welcome You");

//Paragraph par2=new Paragraph("你好,中文測試",ChineseFont());

/**//*par1.setAlignment(Element.ALIGN_CENTER);

doc.add(par1);*/

//par2.setAlignment(Element.ALIGN_CENTER);

//doc.add(par2);

//構建一段落

Paragraph par3=new Paragraph("客戶信息表",ChineseFont());

//設置局中對齊

par3.setAlignment(Element.ALIGN_CENTER);

//添加到文檔

doc.add(par3);

//創建一個四列的表格

Table table=new Table(4);

//設置邊框

table.setBorder(1);

//創建表頭

Cell cell1=new Cell(new Phrase("編號",ChineseFont()));

cell1.setHorizontalAlignment(Element.ALIGN_CENTER);

cell1.setVerticalAlignment(Element.ALIGN_CENTER);

cell1.setHeader(true);

cell1.setBackgroundColor(Color.RED);

Cell cell2=new Cell(new Phrase("姓名",ChineseFont()));

cell2.setHorizontalAlignment(Element.ALIGN_CENTER);

cell2.setVerticalAlignment(Element.ALIGN_CENTER);

cell2.setHeader(true);

cell2.setBackgroundColor(Color.RED);

Cell cell3=new Cell(new Phrase("性別",ChineseFont()));

cell3.setHorizontalAlignment(Element.ALIGN_CENTER);

cell3.setVerticalAlignment(Element.ALIGN_CENTER);

cell3.setHeader(true);

cell3.setBackgroundColor(Color.RED);

Cell cell4=new Cell(new Phrase("備注",ChineseFont()));

cell4.setHorizontalAlignment(Element.ALIGN_CENTER);

cell4.setVerticalAlignment(Element.ALIGN_CENTER);

cell4.setHeader(true);

cell4.setBackgroundColor(Color.RED);

table.addCell(cell1);

table.addCell(cell2);

table.addCell(cell3);

table.addCell(cell4);

//添加此代碼后每頁都會顯示表頭

table.endHeaders();

//循環向表格中添加100條記錄 100行4列的表格

//以下代碼的作用是創建100行數據,其中每行有四列,列依次為 編號 姓名 性別 備注

for (int i = 1; i <=100; i++) {

//設置編號單元格

Cell cell11=new Cell(i+"");

//設置姓名單元格

Cell cell22=new Cell(new Phrase("徐熙媛",ChineseFont()));

//設置性別單元格

Cell cell33=new Cell(new Phrase("女",ChineseFont()));

//設置備注單元格

Cell cell44=new Cell(new Phrase("好姑娘",ChineseFont()));

//單元格水平對齊方式

cell11.setHorizontalAlignment(Element.ALIGN_CENTER);

//單元格垂直對齊方式

cell11.setVerticalAlignment(Element.ALIGN_CENTER);

cell22.setHorizontalAlignment(Element.ALIGN_CENTER);

cell22.setVerticalAlignment(Element.ALIGN_CENTER);

cell33.setHorizontalAlignment(Element.ALIGN_CENTER);

cell33.setVerticalAlignment(Element.ALIGN_CENTER);

cell44.setHorizontalAlignment(Element.ALIGN_CENTER);

cell44.setVerticalAlignment(Element.ALIGN_CENTER);

table.addCell(cell11);

table.addCell(cell22);

table.addCell(cell33);

table.addCell(cell44);

}

//將表格添加到新的文檔

doc.add(table);

//創建新的一頁

doc.newPage();

//添加圖片

Image image=Image.getInstance("D://Program Files//myeclipseworkspace//6.5//iText//src//5.jpg");

//添加到文檔

doc.add(image);

//設置對象方式

image.setAlignment(Element.ALIGN_CENTER);

doc.close();

writer.close();

}

//pdf文檔中文字符處理

public static Font ChineseFont()

{

BaseFont baseFont=null;

try {

baseFont=BaseFont.createFont("STSong-Light","UniGB-UCS2-H", true);

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Font chineseFont=new Font(baseFont,8,Font.NORMAL,Color.BLUE);

return chineseFont;

}

public static void main(String[] args) {

try {

new CreatePdf();

} catch (Exception e) {

e.printStackTrace();

}

}

}

總結

以上是生活随笔為你收集整理的itextpdf添加表格元素_java使用iText生成pdf表格详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。