首页 > 编程 > Java > 正文

itext生成PDF设置页眉页脚的实例详解

2019-11-26 11:19:16
字体:
来源:转载
供稿:网友

itext生成PDF设置页眉页脚的实例详解

实例代码:

/**  * ITextTest  * iText生成PDF加入列表,注释等内容,同时设置页眉和页脚及页码等。  */ 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个参数为非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构造方法第一参数值必须为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);              //添加注释,注释有标题和内容,注释可以是文本,内部链接,外部链接,图片等       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();     }   } }  

工程结构图:

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表