Java導出PDF文件的實現

Java利用itext實現導出PDF文件

所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar

jar包下載地址:http://cn.jarfire.org/com.lowagie.html

一、主要步驟

1.新建document對象

第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。

Document document =new Document(PageSize.A4,50,50,30,20);

2.建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以

  將文檔寫入到磁盤中。

  創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。

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

3.打開文檔

  寫入數據之前要打開文檔

  document.open();

4.向文檔中添加內容

  document.add();

5.關閉文檔

document.close();

二、字體

新建一個字體,iText的方法

BaseFont bfChinese;

bfChinese=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//jar  bfChinese=BaseFont.createFont("C:/Windows/Fonts/msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //系統字體

STSongStd-Light 是字體,在jar 中以property爲後綴

UniGB-UCS2-H   是編碼,在jar 中以cmap爲後綴

H 代表文字版式是橫版,相應的 V 代表豎版

字體設置

參數一:新建好的字體;參數二:字體大小,參數三:字體樣式,多個樣式用“|”分隔

Font topfont =new Font(bfChinese,14Font.BOLD);

Font textfont =new Font(bfChinese,10,Font.BOLD|Font.UNDERLINE);

 

三、添加文本的對象:塊、短句和段落

Chunk(Chunk)是能被添加到文檔的文本的最小單位

Phrase短句(Phrase)是一系列以特定間距(兩行之間的距離)作爲參數的塊

Paragraph 段落是一系列塊和(或)短句。同短句一樣,段落有確定的間距。用戶還可以指定縮排;在邊和(或)右邊保留一定空白,段落可以左對齊、右對齊和居中對齊。添加到文檔中的每一個段落將自動另起一行。

四、步驟2書寫器創建之後,步驟3文檔打開之前

以下項只可在文檔關閉狀態執行 ,包括水印、頁眉、頁腳

水印

Watermark內部類,需要繼承 PdfPageEventHelper

 writer.setPageEvent(new PdfWidget().new Watermark());

 

 頁眉

 Phrase phrase=new Phrase(new Chunk("pdf頁眉   ",topfont));

 phrase.add(new Chunk("頁眉",textfont));

 HeaderFooter header =new HeaderFooter(phrase,false);//false不顯示頁碼。

 header.setAlignment(0);//設置居左0,居中1,居右2

 header.setBorder(Rectangle.BOTTOM);//設置是否有邊框,邊框在下

 document.setHeader(header);//寫進文檔

 頁腳

 HeaderFooter footer =new HeaderFooter(new Phrase("--"),new Phrase("--"));

 footer.setAlignment(1);

 footer.setBorder(Rectangle.NO_BORDER);//Rectangle.NO_BORDER沒有邊框

 document.setFooter(footer);

紅色下劃線部分,自動默認頁碼寫在兩個短句中間

五、設置文檔屬性  (與文檔是否打開沒有關聯)

 document.addTitle("Title@sample");// 標題

 document.addAuthor("Author@nicaisheng");// 作者

 document.addSubject("Subject@iText sample");// 主題

 document.addKeywords("Keywords@iText");// 關鍵字

 document.addCreator("Creator@iText");// 創建者

六、文檔內容

段落Paragraph 

Paragraph pt=new Paragraph(name,headfont);//設置字體樣式pt.setAlignment(1);//設置文字居中 0靠左   1,居中     2,靠右

pt.setIndentationLeft(12);// 左縮進  

pt.setIndentationRight(12);// 右縮進  

pt.setFirstLineIndent(24);// 首行縮進  

  

pt.setLeading(20f);// 行間距  

pt.setSpacingBefore(5f);// 設置上空白  

pt.setSpacingAfter(10f);// 設置段落下空白  

document.add(pt);

表格table

  Table table =new Table(3);//括號參數表示列

  int width[] = {10,45,45};//設置每列寬度比例   

  table.setWidths(width);   

  table.setWidth(95);//佔頁面寬度比例

  table.setAlignment(Element.ALIGN_CENTER);//居中    

  table.setAutoFillEmptyCells(true);//自動填滿       

  table.setBorderWidth((float)0.1);//表格邊框線條寬度    

  table.setPadding(1);//邊距:單元格的邊線與單元格內容的邊距  

  table.setSpacing(0);//間距:單元格與單元格之間的距離

  table.addCell(new Paragraph("name"),textfont));//添加單元格內容

  table.endHeaders();//每頁都會顯示錶頭

 

單元格內容樣式cell

  Cell cell=new Cell(new Paragraph("序號",keyfont));

  cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中

  cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中   

  table.addCell(cell);

 

表格嵌套 

  最外層表格

  PdfPTable table =new PdfPTable(3);

  table.setTotalWidth(300);

  table.setLockedWidth(true);

        

  PdfPCell cell

  cell =new PdfPCell(new Phrase("Table 5"));

  cell.setColspan(3);

  cell.setBorderWidth(0);//設置表格的邊框寬度爲0

  table.addCell(cell);

        

  嵌套表格

  PdfPTable celltable =new PdfPTable(2);

        

  cell =new PdfPCell(celltable);

  cell.setRowspan(2);

  cell.setBorderWidth(1);//設置表格的邊框寬度爲1

  cell.setPadding(10);//設置表格與上一個表格的填充爲10

  table.addCell(cell);

 

  直線  

  Paragraph p1 =new Paragraph();  

  p1.add(new Chunk(new LineSeparator()));   

  doc.add(p1);   

  點線 

  Paragraph p2 =new Paragraph();  

  p2.add(new Chunk(new DottedLineSeparator()));

超鏈接

 Anchor anchor =new Anchor("this is anchor");

定位

點擊後,跳到topline的位置

 Anchor gotop =new Anchor("go top");

 gotop.setReference("#us");

 

添加圖片

Image image =Image.getInstance(imgPath);

 image.setAlignment(Image.ALIGN_CENTER);

 image.scalePercent(40);//依照比例縮放


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章