栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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; 
 
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 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)); 
       
      footer.setAlignment(1); 
      footer.setBorderColor(Color.red); 
      footer.setBorder(Rectangle.BOX); 
      doc.setFooter(footer); 
     
       
      doc.open(); 
       
      List itextList=new List(true,10); 

       
      //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(); 
    } 
  } 
} 
 

工程结构图:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144080.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号