Example usage for com.lowagie.text.pdf PdfPageEvent PdfPageEvent

List of usage examples for com.lowagie.text.pdf PdfPageEvent PdfPageEvent

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPageEvent PdfPageEvent.

Prototype

PdfPageEvent

Source Link

Usage

From source file:nl.dykema.jxmlnote.report.pdf.PdfReport.java

License:Open Source License

public void beginReport(File output) throws ReportException {
    try {//  w  ww.ja  va  2  s .  co m
        _canceled = false;
        _doc = new Document();
        _writer = PdfWriter.getInstance(_doc, new FileOutputStream(output));
        setPageSize(PageSize.A4);
        setOrientation(Orientation.PORTRAIT);
        setMargins(new Rectangle(72.0f, 72.0f, 72.0f, 72.0f));

        _writer.setPageEvent(new PdfPageEvent() {

            public void onChapter(PdfWriter arg0, Document arg1, float arg2, com.lowagie.text.Paragraph arg3) {
            }

            public void onChapterEnd(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onCloseDocument(PdfWriter arg0, Document arg1) {
                PdfReport.super.getReportListeners().informEndReport(PdfReport.this);
            }

            public void onGenericTag(PdfWriter arg0, Document arg1, com.lowagie.text.Rectangle arg2,
                    String arg3) {
            }

            public void onOpenDocument(PdfWriter arg0, Document arg1) {
            }

            public void onParagraph(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onParagraphEnd(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onSection(PdfWriter arg0, Document arg1, float arg2, int arg3,
                    com.lowagie.text.Paragraph arg4) {
            }

            public void onSectionEnd(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onStartPage(PdfWriter wrt, Document doc) {
            }

            public void onEndPage(PdfWriter wrt, Document doc) {
                try {
                    ReportListeners _listeners = PdfReport.super.getReportListeners();
                    _listeners.informNextPage(PdfReport.this);

                    Vector<ReportElement> vhdr = _listeners.getHeader(PdfReport.this);
                    Vector<ReportElement> vftr = _listeners.getFooter(PdfReport.this);

                    PdfContentByte cb = wrt.getDirectContent();

                    // write headers on top of each other
                    if (vhdr != null) {
                        Iterator<ReportElement> it = vhdr.iterator();

                        while (it.hasNext()) {
                            ReportElement hdr = it.next();

                            Rectangle pageSize = PdfReport.this.getPageRect();
                            Rectangle margins = PdfReport.this.getMargins();
                            float ytop = pageSize.top() - margins.top();
                            float hdrHeight = PdfReport.this.getHeight(hdr, null);
                            float rpos = (margins.top() - hdrHeight) / 2;
                            float hytop = ytop + rpos + hdrHeight;

                            if (hdr instanceof PdfTable) {
                                int firstRow = 0, lastRow = -1;
                                PdfTable phdr = (PdfTable) hdr;
                                phdr.writeSelectedRows(firstRow, lastRow, margins.left(), hytop, cb);
                            } else if (hdr instanceof PdfParagraph) {
                                PdfParagraph ppar = (PdfParagraph) hdr;
                                ColumnText ct = new ColumnText(cb);
                                float textWidth = PdfReport.this.getTextWidth();
                                ct.addElement(ppar);
                                ct.setSimpleColumn(margins.left(), ytop + rpos, margins.left() + textWidth,
                                        hytop);
                                ct.go();
                            }
                        }
                    }

                    // write footers on top of each other

                    if (vftr != null) {
                        Iterator<ReportElement> it = vftr.iterator();

                        while (it.hasNext()) {
                            ReportElement ftr = it.next();

                            Rectangle margins = PdfReport.this.getMargins();
                            float ytop = margins.bottom();
                            float hdrHeight = PdfReport.this.getHeight(ftr, null);
                            float rpos = (margins.bottom() - hdrHeight) / 2;
                            float hytop = ytop - rpos;

                            if (ftr instanceof PdfTable) {
                                int firstRow = 0, lastRow = -1;
                                PdfTable pftr = (PdfTable) ftr;
                                pftr.writeSelectedRows(firstRow, lastRow, margins.left(), hytop, cb);
                            } else if (ftr instanceof PdfParagraph) {
                                PdfParagraph ppar = (PdfParagraph) ftr;
                                ColumnText ct = new ColumnText(cb);
                                float textWidth = PdfReport.this.getTextWidth();
                                ct.addElement(ppar);
                                ct.setSimpleColumn(margins.left(), rpos, margins.left() + textWidth, hytop);
                                ct.go();
                            }
                        }
                    }

                } catch (ReportException e) {
                    DefaultXMLNoteErrorHandler.exception(e);
                } catch (DocumentException e) {
                    DefaultXMLNoteErrorHandler.exception(e);
                }

            }

        });

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

From source file:org.jdesktop.swingx.jxmlnote.report.pdf.PdfReport.java

License:Open Source License

public void beginReport(File output) throws ReportException {
    try {/*  w  ww  .  j a v a 2 s  . c  om*/
        _canceled = false;
        _doc = new Document();
        _writer = PdfWriter.getInstance(_doc, new FileOutputStream(output));
        setPageSize(PageSize.A4);
        setMargins(new Rectangle(72.0f, 72.0f, 72.0f, 72.0f));

        _writer.setPageEvent(new PdfPageEvent() {

            public void onChapter(PdfWriter arg0, Document arg1, float arg2, com.lowagie.text.Paragraph arg3) {
            }

            public void onChapterEnd(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onCloseDocument(PdfWriter arg0, Document arg1) {
                PdfReport.super.getReportListeners().informEndReport(PdfReport.this);
            }

            public void onGenericTag(PdfWriter arg0, Document arg1, com.lowagie.text.Rectangle arg2,
                    String arg3) {
            }

            public void onOpenDocument(PdfWriter arg0, Document arg1) {
            }

            public void onParagraph(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onParagraphEnd(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onSection(PdfWriter arg0, Document arg1, float arg2, int arg3,
                    com.lowagie.text.Paragraph arg4) {
            }

            public void onSectionEnd(PdfWriter arg0, Document arg1, float arg2) {
            }

            public void onStartPage(PdfWriter wrt, Document doc) {
            }

            public void onEndPage(PdfWriter wrt, Document doc) {
                try {
                    ReportListeners _listeners = PdfReport.super.getReportListeners();
                    _listeners.informNextPage(PdfReport.this);

                    Vector<ReportElement> vhdr = _listeners.getHeader(PdfReport.this);
                    Vector<ReportElement> vftr = _listeners.getFooter(PdfReport.this);

                    PdfContentByte cb = wrt.getDirectContent();

                    // write headers on top of each other
                    if (vhdr != null) {
                        Iterator<ReportElement> it = vhdr.iterator();

                        while (it.hasNext()) {
                            ReportElement hdr = it.next();

                            Rectangle pageSize = PdfReport.this.getPageRect();
                            Rectangle margins = PdfReport.this.getMargins();
                            float ytop = pageSize.top() - margins.top();
                            float hdrHeight = PdfReport.this.getHeight(hdr, null);
                            float rpos = (margins.top() - hdrHeight) / 2;
                            float hytop = ytop + rpos + hdrHeight;

                            if (hdr instanceof PdfTable) {
                                int firstRow = 0, lastRow = -1;
                                PdfTable phdr = (PdfTable) hdr;
                                phdr.writeSelectedRows(firstRow, lastRow, margins.left(), hytop, cb);
                            } else if (hdr instanceof PdfParagraph) {
                                PdfParagraph ppar = (PdfParagraph) hdr;
                                ColumnText ct = new ColumnText(cb);
                                float textWidth = PdfReport.this.getTextWidth();
                                ct.addElement(ppar);
                                ct.setSimpleColumn(margins.left(), ytop + rpos, margins.left() + textWidth,
                                        hytop);
                                ct.go();
                            }
                        }
                    }

                    // write footers on top of each other

                    if (vftr != null) {
                        Iterator<ReportElement> it = vftr.iterator();

                        while (it.hasNext()) {
                            ReportElement ftr = it.next();

                            Rectangle margins = PdfReport.this.getMargins();
                            float ytop = margins.bottom();
                            float hdrHeight = PdfReport.this.getHeight(ftr, null);
                            float rpos = (margins.bottom() - hdrHeight) / 2;
                            float hytop = ytop - rpos;

                            if (ftr instanceof PdfTable) {
                                int firstRow = 0, lastRow = -1;
                                PdfTable pftr = (PdfTable) ftr;
                                pftr.writeSelectedRows(firstRow, lastRow, margins.left(), hytop, cb);
                            } else if (ftr instanceof PdfParagraph) {
                                PdfParagraph ppar = (PdfParagraph) ftr;
                                ColumnText ct = new ColumnText(cb);
                                float textWidth = PdfReport.this.getTextWidth();
                                ct.addElement(ppar);
                                ct.setSimpleColumn(margins.left(), rpos, margins.left() + textWidth, hytop);
                                ct.go();
                            }
                        }
                    }

                } catch (ReportException e) {
                    DefaultXMLNoteErrorHandler.exception(e);
                } catch (DocumentException e) {
                    DefaultXMLNoteErrorHandler.exception(e);
                }

            }

        });

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