2017-07-06 50 views
0

我正在使用itext在我的應用程序中生成PDF。在頁腳部分,包含Y的頁面X的短語在那裏。 PDF創建後不顯示總頁數。請參考下面的代碼:itext頁腳中的頁面總數未顯示

在MainActivity

 PdfPTable table = new PdfPTable(2); 
     table.setSpacingAfter(40.0f); 
     table.setTotalWidth(document.right()-document.left()-100); 

     try { 
      table.setWidths(new int[]{1, 2}); 
     } catch (DocumentException e) { 
      e.printStackTrace(); 
     } 
     PdfPCell cellOne = new PdfPCell(img); 



     Font f=new Font(Font.FontFamily.TIMES_ROMAN,20.0f,Font.BOLD, BaseColor.BLACK); 
     Font Para1_font=new Font(Font.FontFamily.TIMES_ROMAN,15.0f,Font.BOLD, BaseColor.BLACK); 
     Paragraph paragraph1_header = new Paragraph("QUOTE TO: 294087",f); 
     Paragraph paragraph = new Paragraph(); 
     paragraph.add("AAAAAAAAAALLC * hhjkhhhhuhjbbnb" + 
         "jgjkll;, Sjklkjjjhh * AAHHGBJJ"); 

     // Paragraph paragraph3 = new Paragraph(); 
     // paragraph3.add("Page 1"); 
     paragraph.setAlignment(Paragraph.ALIGN_LEFT); 
     PdfPCell cellTwo = new PdfPCell(paragraph); 
     // PdfPCell cellThree = new PdfPCell(paragraph3); 

     cellTwo.setPaddingLeft(10.0f); 
     cellOne.setPaddingBottom(20.0f); 
     cellTwo.setPaddingBottom(20.0f); 
     cellThree.setPaddingBottom(20.0f); 
     cellOne.setBorder(Rectangle.NO_BORDER); 
     cellTwo.setBorder(Rectangle.NO_BORDER); 
     cellThree.setBorder(Rectangle.NO_BORDER); 
     cellOne.setHorizontalAlignment(Element.ALIGN_LEFT); 
     cellTwo.setHorizontalAlignment(Element.ALIGN_LEFT); 
     cellThree.setHorizontalAlignment(Element.ALIGN_RIGHT); 
     table.addCell(cellOne); 
     table.addCell(cellTwo); 
     //table.addCell(cellThree); 



     try { 
      HeaderFooterPageEvent event = new HeaderFooterPageEvent(this, table); 
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); 
      //writer.setBoxSize("art", new Rectangle(36, 54, 559, 788)); 
      writer.setPageEvent(event); 
      document.open(); 

HeaderFooterPageEvent類中:

public void onOpenDocument(PdfWriter writer, Document document) { 
    total = writer.getDirectContent().createTemplate(30, 16); 
    try { 
     totalPages = Image.getInstance(total); 
    } catch (BadElementException e) { 
     e.printStackTrace(); 
    } 
    totalPages.setRole(PdfName.ARTIFACT); 
} 
public void onEndPage(PdfWriter writer, Document document) { 

    footer.writeSelectedRows(0, -100, 36, 65, writer.getDirectContent()); 
    try { 
     PdfPCell cell = new PdfPCell(Image.getInstance(total)); 
    } catch (BadElementException e) { 
     e.printStackTrace(); 
    } 

     Phrase footerPhrase = new Phrase("Page "+writer.getPageNumber()+ 
      " of"); 
    footerPhrase.add(new Chunk(totalPages,0,0,true)); 

     ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footerPhrase, 500, 65, 0); 


    } 

它只是展示,而不是 「Y的頁面X」 「的第x頁」。有什麼我失蹤?請幫忙。

回答

1

您已經實現了onOpenDocument方法創建一個大小30,16的PdfTemplate(是不是比較小?),你是在onEndPage方法每一頁上添加此佔位符。

但是,我沒有看到您在任何地方向PdfTemplate添加內容。如果您沒有添加頁面總數,那麼您將看不到文檔中任何位置的頁面總數。

既然你只知道你關閉該文檔的那一刻的總頁數,你需要實現onCloseDocument()

public void onCloseDocument(PdfWriter writer, Document document) { 
    ColumnText.showTextAligned(total, Element.ALIGN_LEFT, 
      new Phrase(String.valueOf(writer.getPageNumber() - 1)), 
      2, 2, 0); 
} 

MovieCountries1一個完整的例子。這個例子是在「iText in Action」第二版的背景下編寫的。