如何在onEndPage()itext方法中传递自定义对象? [英] How to pass custom object in onEndPage() itext method?

查看:220
本文介绍了如何在onEndPage()itext方法中传递自定义对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onEndPage()方法(itext)遇到了一些问题,基本上我已经在basc方法中用这种方式在页脚上写了一些文本:

i've some problems with the onEndPage() method (itext), basically i'm already writing some text on footer, with the basc method like this way:

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

基本,我想将一个对象以及写和文档传递给onEndPage,但是在创建Pdf,阅读文档时永远不会调用此方法,此类方法被Event调用,所以我什至无法更改我猜方法的签名...有什么建议吗? 谢谢大家的回答.

Basicaly i want to pass one more object plus write and document to the onEndPage,but this method is never called on the creation of the Pdf, reading on documentation, this kind of method is called by an Event, so i cant even change the signature of the method i guess...any suggestion? Thank you all for the answers.

推荐答案

发现自己后,您不能更改方法签名,因为您不是方法的调用者.但是在创建和注册事件对象时,您可以扩展其类,使其具有可用于您的任务的其他属性,例如:

As you have found out yourself, you cannot change the method signature as you aren't the caller of the method. But as you create and register the event object, you can extend its class to have an additional property which you can use for your task, e.g.:

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变量即可.

Thus, as soon as you know the value to use for the next footer, you simply assign it to its extraValue variable.

这篇关于如何在onEndPage()itext方法中传递自定义对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆