2011-11-04 130 views
0

嗨,大家好,我有一個java類,用於在iText PDF中顯示頁眉和頁腳。Itext pageNumber在PDF中顯示

的頁碼顯示在頁面頂部和頁腳在頁腳部分正確地來.. 現在我想以顯示頁腳處的頁碼...

下使用

代碼IM這樣

         1 (header) 
public HeaderAndFooter() { 

header = new Phrase("**** Header ****"); 

footer = new PdfPTable(1); 

footer.setTotalWidth(130); 

footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); 

footer.addCell(new Phrase(new Chunk(

"**** Footer summary****") 

.setAction(new PdfAction(PdfAction.FIRSTPAGE)))); 

} 

public void onEndPage(PdfWriter writer, Document document) { 

PdfContentByte cb = writer.getDirectContent(); 

ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format("%d", writer.getPageNumber()), 

PDFUtil.getNormalFont()), (document 

.right() - document.left()) 

/2 + document.leftMargin(), document.top() + 10, 0); 

footer.writeSelectedRows(0, -1, 

(document.right() - document.left() - 300)/2 

+ document.leftMargin(), document.bottom() - 10, cb); 

} 


} 

即時得到輸出

26月 - 2011 Rentaldetails月----5500個月

雜---- 500 總--- 5500

                           **** Footer summary (footer) 
          **** 

好心建議我如何顯示頁腳部分編號和副... Thankxxx提前

+0

什麼版本的IText?老版本有HeaderFooter對象 – Sean

+0

@Sean Am使用iText.2.1.0 – Aravinth

+0

用writer.getPageNumber()輸出的短語是什麼?如果它是空白的,請嘗試在其周圍添加一些文字,以查看是否可能存在其他問題。這是獲取頁碼的呼叫。你應該能夠相應地進行格式化 – Sean

回答

1

傢伙,我已經找到了解決方案,並chaged的類太..這裏itzzz ...

public class HeaderAndFooter extends PdfPageEventHelper { 

public void onEndPage (PdfWriter writer, Document document) { 
    Rectangle rect = writer.getBoxSize("art"); 
    switch(writer.getPageNumber() % 2) { 
    case 0: 
     ColumnText.showTextAligned(writer.getDirectContent(),       Element.ALIGN_RIGHT, new Phrase("even header"), 
       rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0); 
     break; 
    case 1: 
     ColumnText.showTextAligned(writer.getDirectContent(), 
       Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())), 
       300f, 62f, 0); 
     break; 
    } 

下面的代碼,你在你的打印類給最終的字節流排出了... 那裏你必須給

baos = new ByteArrayOutputStream(); 
     Document document = new Document(PageSize.A4, 60, 60, 120, 80); 
     PdfWriter writer = PdfWriter.getInstance(document, baos); 
     HeaderAndFooter event = new HeaderAndFooter(); 
     writer.setPageEvent(event); 
     document.open(); 

它的工作原理精細和簡單的PageNumber代.... 注:我已經使用iText的2.1.0.jar

1

全部工人階級從iText的

import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Element; 
import com.lowagie.text.ExceptionConverter; 
import com.lowagie.text.Image; 
import com.lowagie.text.Phrase; 
import com.lowagie.text.Rectangle; 
import com.lowagie.text.pdf.ColumnText; 
import com.lowagie.text.pdf.PdfPCell; 
import com.lowagie.text.pdf.PdfPTable; 
import com.lowagie.text.pdf.PdfPageEventHelper; 
import com.lowagie.text.pdf.PdfTemplate; 
import com.lowagie.text.pdf.PdfWriter; 

class TableHeader extends PdfPageEventHelper { 
    /** The header text. */ 
    String header; 
    /** The template with the total number of pages. */ 
    PdfTemplate total; 

    /** 
    * Allows us to change the content of the header. 
    * 
    * @param header 
    *   The new header String 
    */ 
    public void setHeader(String header) { 
     this.header = header; 
    } 

    /** 
    * Creates the PdfTemplate that will hold the total number of pages. 
    * 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter, 
    *  com.itextpdf.text.Document) 
    */ 
    public void onOpenDocument(PdfWriter writer, Document document) { 
     total = writer.getDirectContent().createTemplate(30, 16); 
    } 

    /** 
    * Adds a header to every page 
    * 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, 
    *  com.itextpdf.text.Document) 
    */ 
    public void onEndPage(PdfWriter writer, Document document) { 
     PdfPTable table = new PdfPTable(3); 
     try { 
      table.setWidths(new int[] { 24, 24, 2 }); 
      table.setTotalWidth(527); 
      table.setLockedWidth(true); 
      table.getDefaultCell().setFixedHeight(20); 
      table.getDefaultCell().setBorder(Rectangle.BOTTOM); 
      table.addCell(header); 
      table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); 
      table.addCell(String.format("Page %d of", writer.getPageNumber())); 
      PdfPCell cell = new PdfPCell(Image.getInstance(total)); 
      cell.setBorder(Rectangle.BOTTOM); 
      table.addCell(cell); 
      table.writeSelectedRows(0, -1, 34, 50, writer.getDirectContent()); 
     } catch (DocumentException de) { 
      throw new ExceptionConverter(de); 
     } 
    } 

    /** 
    * Fills out the total number of pages before the document is closed. 
    * 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, 
    *  com.itextpdf.text.Document) 
    */ 
    public void onCloseDocument(PdfWriter writer, Document document) { 
     ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber() - 1)), 2, 2, 0); 
    } 
}