生成PDF文件添加總頁數


  在頁眉或者頁腳加上第x頁這樣的信息

  HeaderFooter footer = new HeaderFooter(new Phrase("第:",FontChinese), new Phrase("頁",FontChinese));       footer.setBorder(Rectangle.NO_BORDER);    

  document.setFooter(footer);

      document.open();  

  • 在每頁顯示第x頁/共y頁這樣的功能

  • import java.io.FileOutputStream;

    import com.lowagie.text.Document;
    import com.lowagie.text.Element;
    import com.lowagie.text.ExceptionConverter;
    import com.lowagie.text.Font;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.pdf.PdfContentByte;
    import com.lowagie.text.pdf.PdfPageEventHelper;
    import com.lowagie.text.pdf.PdfTemplate;
    import com.lowagie.text.pdf.PdfWriter;

    public class PdfExport extends PdfPageEventHelper{

    public PdfTemplate tpl;
    public BaseFont bf;
    public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 20, 20, 20, 20);
    try {
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\HelloItext.pdf"));
    writer.setPageEvent(new PdfExport());
    BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    document.open();
    Paragraph title = new Paragraph("測試內容。。。。", new Font(bfChinese,15));
    title.setAlignment(Element.ALIGN_CENTER);
    document.add(title);
    } catch (Exception de) {
    de.printStackTrace();
    }
    document.close();
    }

    public void onOpenDocument(PdfWriter writer, Document document) {
    try {
    tpl = writer.getDirectContent().createTemplate(100, 100);
    bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    }catch(Exception e) {
    throw new ExceptionConverter(e);
    }
    }

    public void onEndPage(PdfWriter writer, Document document) {
    //在每頁結束的時候把“第x頁”信息寫道模版指定位置
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    String text ="第"+ writer.getPageNumber() +"頁,共";
    cb.beginText();
    cb.setFontAndSize(bf, 8);
    cb.setTextMatrix(460, 786);//定位“第x頁,共” 在具體的頁面調試時候需要更改這xy的坐標
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, 492, 786);//定位“y頁” 在具體的頁面調試時候需要更改這xy的坐標
    cb.saveState();
    cb.stroke();
    cb.restoreState();
    cb.closePath();//sanityCheck();
    }

    public void onCloseDocument(PdfWriter writer, Document document) {
    //關閉document的時候獲取總頁數,並把總頁數按模版寫道之前預留的位置
    tpl.beginText();
    tpl.setFontAndSize(bf, 8);
    tpl.showText(Integer.toString(writer.getPageNumber() - 1)+"頁");
    tpl.endText();
    tpl.closePath();//sanityCheck();
    }
    }

官方頁面有具體實現的例子,連接地址: 
http://itextdocs.lowagie.com/tutorial/directcontent/pageevents/index.php 
源碼: 
http://itextdocs.lowagie.com/examples/com/lowagie/examples/directcontent/pageevents/PageNumbersWatermark.java

 

其他相關學習連接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM