itext生成PDF設置頁眉頁腳的實例詳解
更新時間:2017年09月23日 11:41:25 作者:tujiyue
這篇文章主要介紹了itext生成PDF設置頁眉頁腳的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
itext生成PDF設置頁眉頁腳的實例詳解
實例代碼:
/** * ITextTest * iText生成PDF加入列表,注釋等內(nèi)容,同時設置頁眉和頁腳及頁碼等。 */ 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);//設置中文字體 } catch (IOException e) { e.printStackTrace(); } Font chinese = new Font(fontChinese, 10, Font.NORMAL); /** * HeaderFooter的第2個參數(shù)為非false時代表打印頁碼 * 頁眉頁腳中也可以加入圖片,并非只能是文字 */ HeaderFooter header=new HeaderFooter(new Phrase("這僅僅是個頁眉,頁碼在頁腳處",chinese),false); //設置是否有邊框等 // 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); /** * 頁眉頁腳的設置一定要在open前設置好 */ doc.open(); /** * true:代表要排序,10代表序號與文字之間的間距 * false:代表不排序,則文字前的符號為"-" */ List itextList=new List(true,10); /** * 也可以改變列表的符號[可選] * $$$ * 要改變列表符號時,上面的List構(gòu)造方法第一參數(shù)值必須為false * $$$ * 可以使用字符串,Chunk,Image等作列表符號,如下 */ //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); //添加注釋,注釋有標題和內(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)圖:
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Java微信公眾平臺開發(fā)(2) 微信服務器post消息體的接收
這篇文章主要為大家詳細介紹了Java微信公眾平臺開發(fā)第二步,微信服務器post消息體的接收,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04基于springboot實現(xiàn)redis分布式鎖的方法
這篇文章主要介紹了基于springboot實現(xiàn)redis分布式鎖的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11使用Gson將字符串轉(zhuǎn)換成JsonObject和JsonArray
這篇文章主要介紹了使用Gson將字符串轉(zhuǎn)換成JsonObject和JsonArray,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解
這篇文章主要介紹了Spring中的底層架構(gòu)核心概念類型轉(zhuǎn)換器詳解,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12關(guān)于Object中equals方法和hashCode方法判斷的分析
今天小編就為大家分享一篇關(guān)于關(guān)于Object中equals方法和hashCode方法判斷的分析,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01