2017-03-17 41 views
0

我一些問題的OnEndPage()方法(iText的),基本上我已經寫在頁腳一些文字,像這樣的方式BASC方法:如何在onEndPage()itext方法中傳遞自定義對象?

public class EventDichiarazionePdf extends PdfPageEventHelper{ 
    private int numPage=0; 
    private static Font h6NormFont = new Font(Font.FontFamily.HELVETICA, 6,Font.NORMAL);  

    public void onStartPage(PdfWriter writer, Document document) { 
     numPage++; 
    } 
    public void onEndPage(PdfWriter writer, Document document) { 

     try {    
       Rectangle page = document.getPageSize(); 

       PdfPTable footer = new PdfPTable(2); 

       PdfPCell cellFooter = new PdfPCell(new Phrase("Something – 03/12/2016 Customer n." + "here i need my variable",h6NormFont)); 
       cellFooter.setHorizontalAlignment(Element.ALIGN_LEFT); 
       cellFooter.setBorder(0);  
       footer.addCell(cellFooter); 

       cellFooter = new PdfPCell(new Phrase(String.format("pag. %d",numPage),h6NormFont)); 
       cellFooter.setHorizontalAlignment(Element.ALIGN_RIGHT); 
       cellFooter.setBorder(0); 

       footer.addCell(cellFooter); 

       footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); 
       footer.writeSelectedRows(0,-1,document.leftMargin(),document.bottomMargin(),writer.getDirectContent()); 

       } catch (Exception e) { 
        throw new ExceptionConverter(e); 
       }   
      } 
    } 
} 

Basicaly我想通過一個更對象加上寫入和文檔到onEndPage,但這種方法永遠不會被創建的Pdf,在文檔上閱讀,這種方法是由一個事件調用,所以我甚至不能改變我猜的方法的簽名..任何建議? 謝謝大家的答案。

回答

0

正如你已經發現你自己,你不能改變方法簽名,因爲你不是方法的調用者。一旦

public class EventDichiarazionePdf extends PdfPageEventHelper{ 
    // vvv--- Change 
    public String extraValue = ""; 
    // ^^^--- Change 
    private int numPage=0; 
    private static Font h6NormFont = new Font(Font.FontFamily.HELVETICA, 6,Font.NORMAL);  

    public void onStartPage(PdfWriter writer, Document document) { 
     numPage++; 
    } 
    public void onEndPage(PdfWriter writer, Document document) { 

     try {    
       Rectangle page = document.getPageSize(); 

       PdfPTable footer = new PdfPTable(2); 

       // vvv--- Change 
       PdfPCell cellFooter = new PdfPCell(new Phrase("Something – 03/12/2016 Customer n." + extraValue ,h6NormFont)); 
       // ^^^--- Change 
       cellFooter.setHorizontalAlignment(Element.ALIGN_LEFT); 
       cellFooter.setBorder(0);  
       footer.addCell(cellFooter); 
       [...] 
      } catch (Exception e) { 
       throw new ExceptionConverter(e); 
      }   
     } 
    } 
} 

因此,當你知道使用的值:但在創建並註冊事件對象,你可以擴展它的類有一個附加屬性,你可以使用你的任務,如下一頁腳,你只需將它分配給它的extraValue變量。

相關問題