溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

itext生成PDF設(shè)置頁(yè)眉頁(yè)腳的實(shí)例詳解

發(fā)布時(shí)間:2020-08-27 16:23:24 來(lái)源:腳本之家 閱讀:271 作者:tujiyue 欄目:編程語(yǔ)言

itext生成PDF設(shè)置頁(yè)眉頁(yè)腳的實(shí)例詳解

實(shí)例代碼:

/** 
 * ITextTest 
 * iText生成PDF加入列表,注釋等內(nèi)容,同時(shí)設(shè)置頁(yè)眉和頁(yè)腳及頁(yè)碼等。 
 */ 
package com.labci.itext.test; 
import java.awt.Color; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import com.lowagie.text.Annotation; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Font; 
import com.lowagie.text.HeaderFooter; 
import com.lowagie.text.List; 
import com.lowagie.text.ListItem; 
import com.lowagie.text.Phrase; 
import com.lowagie.text.Rectangle; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.pdf.PdfWriter; 
/** 
 * @author Bill Tu(tujiyue/iwtxokhtd) 
 * Jun 6, 2011[4:10:35 PM] 
 * 
 */ 
public class ITextList { 
  private final static String RESULT_FILE="itext_list.pdf"; 
   
  public static void main(String []args){ 
    Document doc=new Document(); 
     
    try { 
       
       
       
      PdfWriter.getInstance(doc, new FileOutputStream(RESULT_FILE)); 
      BaseFont fontChinese=null; 
      try { 
        fontChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//設(shè)置中文字體 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
     
      Font chinese = new Font(fontChinese, 10, Font.NORMAL);  
       
      /** 
       * HeaderFooter的第2個(gè)參數(shù)為非false時(shí)代表打印頁(yè)碼 
       * 頁(yè)眉頁(yè)腳中也可以加入圖片,并非只能是文字 
       */ 
      HeaderFooter header=new HeaderFooter(new Phrase("這僅僅是個(gè)頁(yè)眉,頁(yè)碼在頁(yè)腳處",chinese),false); 
       
      //設(shè)置是否有邊框等 
//     header.setBorder(Rectangle.NO_BORDER); 
      header.setBorder(Rectangle.BOTTOM); 
      header.setAlignment(1); 
      header.setBorderColor(Color.red); 
      doc.setHeader(header); 
       
      HeaderFooter footer=new HeaderFooter(new Phrase("-",chinese),new Phrase("-",chinese)); 
      /** 
       * 0是靠左 
       * 1是居中 
       * 2是居右 
       */ 
      footer.setAlignment(1); 
      footer.setBorderColor(Color.red); 
      footer.setBorder(Rectangle.BOX); 
      doc.setFooter(footer); 
     
      /** 
       * 頁(yè)眉頁(yè)腳的設(shè)置一定要在open前設(shè)置好 
       */ 
      doc.open(); 
      /** 
       * true:代表要排序,10代表序號(hào)與文字之間的間距 
       * false:代表不排序,則文字前的符號(hào)為"-" 
       */ 
      List itextList=new List(true,10); 
       
      /** 
       * 也可以改變列表的符號(hào)[可選] 
       * 
$$$ 
       * 要改變列表符號(hào)時(shí),上面的List構(gòu)造方法第一參數(shù)值必須為false 
       * 
$$$ 
       * 可以使用字符串,Chunk,Image等作列表符號(hào),如下 
       */ 
      //itextList.setListSymbol("*"); 
       
      ListItem firstItem=new ListItem("first paragraph"); 
      ListItem secondItem=new ListItem("second paragraph"); 
      ListItem thirdItem=new ListItem("third paragraph"); 
      itextList.add(firstItem); 
      itextList.add(secondItem); 
      itextList.add(thirdItem); 
       
      doc.add(itextList); 
       
      //添加注釋,注釋有標(biāo)題和內(nèi)容,注釋可以是文本,內(nèi)部鏈接,外部鏈接,圖片等 
      Annotation annotation=new Annotation("what's this?","it's a tree and it is not a big"); 
       
      doc.add(annotation); 
       
      doc.close(); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (DocumentException e) { 
      e.printStackTrace(); 
    } 
  } 
} 
 

工程結(jié)構(gòu)圖:

itext生成PDF設(shè)置頁(yè)眉頁(yè)腳的實(shí)例詳解

itext生成PDF設(shè)置頁(yè)眉頁(yè)腳的實(shí)例詳解

itext生成PDF設(shè)置頁(yè)眉頁(yè)腳的實(shí)例詳解

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI