Example usage for com.itextpdf.text.pdf PdfWriter setPageEvent

List of usage examples for com.itextpdf.text.pdf PdfWriter setPageEvent

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter setPageEvent.

Prototype


public void setPageEvent(final PdfPageEvent event) 

Source Link

Document

Sets the PdfPageEvent for this document.

Usage

From source file:dbedit.actions.ExportPdfAction.java

License:Open Source License

@Override
protected void performThreaded(ActionEvent e) throws Exception {
    boolean selection = false;
    JTable table = ResultSetTable.getInstance();
    if (table.getSelectedRowCount() > 0 && table.getSelectedRowCount() != table.getRowCount()) {
        Object option = Dialog.show("PDF", "Export", Dialog.QUESTION_MESSAGE,
                new Object[] { "Everything", "Selection" }, "Everything");
        if (option == null || "-1".equals(option.toString())) {
            return;
        }// ww  w.  j  a va2s  . c  o  m
        selection = "Selection".equals(option);
    }
    List list = ((DefaultTableModel) table.getModel()).getDataVector();
    int columnCount = table.getColumnCount();
    PdfPTable pdfPTable = new PdfPTable(columnCount);
    pdfPTable.setWidthPercentage(100);
    pdfPTable.getDefaultCell().setPaddingBottom(4);
    int[] widths = new int[columnCount];

    // Row Header
    pdfPTable.getDefaultCell().setBorderWidth(2);
    for (int i = 0; i < columnCount; i++) {
        String columnName = table.getColumnName(i);
        pdfPTable.addCell(new Phrase(columnName, ROW_HEADER_FONT));
        widths[i] = Math.min(50000, Math.max(widths[i], ROW_HEADER_BASE_FONT.getWidth(columnName + " ")));
    }
    pdfPTable.getDefaultCell().setBorderWidth(1);
    if (!list.isEmpty()) {
        pdfPTable.setHeaderRows(1);
    }

    // Body
    for (int i = 0; i < list.size(); i++) {
        if (!selection || table.isRowSelected(i)) {
            List record = (List) list.get(i);
            for (int j = 0; j < record.size(); j++) {
                Object o = record.get(j);
                if (o != null) {
                    if (ResultSetTable.isLob(j)) {
                        o = Context.getInstance().getColumnTypeNames()[j];
                    }
                } else {
                    o = "";
                }
                PdfPCell cell = new PdfPCell(new Phrase(o.toString()));
                cell.setPaddingBottom(4);
                if (o instanceof Number) {
                    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                }
                pdfPTable.addCell(cell);
                widths[j] = Math.min(50000, Math.max(widths[j], BASE_FONT.getWidth(o.toString())));
            }
        }
    }

    // Size
    pdfPTable.setWidths(widths);
    int totalWidth = 0;
    for (int width : widths) {
        totalWidth += width;
    }
    Rectangle pageSize = PageSize.A4.rotate();
    pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f));
    pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f));

    // Document
    Document document = new Document(pageSize);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream);
    document.open();
    pdfTemplate = writer.getDirectContent().createTemplate(100, 100);
    pdfTemplate.setBoundingBox(new Rectangle(-20, -20, 100, 100));
    writer.setPageEvent(this);
    document.add(pdfPTable);
    document.close();
    FileIO.saveAndOpenFile("export.pdf", byteArrayOutputStream.toByteArray());
}

From source file:de.aw.awlib.pdf.PDFDocument.java

License:Open Source License

/**
 * @param filename//from  w  ww .j a  v a2 s .  c  o  m
 *         Names des Files. .pdf wird angehaengt
 * @throws FileNotFoundException
 *         Wenn das File nicht gefunden wird
 * @throws DocumentException
 *         Wenn das pdf nicht erstellt werden kann
 */
public PDFDocument(@NonNull String filename) throws FileNotFoundException, DocumentException {
    mDocument = new Document(PageSize.A4);
    mFile = new File(filename + ".pdf");
    PdfWriter writer = PdfWriter.getInstance(mDocument, new FileOutputStream(mFile));
    writer.setPageEvent(new PDFEventHelper());
    mDocument.open();
}

From source file:de.domjos.schooltools.core.utils.fileUtils.PDFBuilder.java

License:Open Source License

public PDFBuilder(String path, Context context) throws Exception {
    this.document = new Document();
    PdfWriter writer = PdfWriter.getInstance(this.document, new FileOutputStream(path));
    writer.setBoxSize("art", new Rectangle(55, 25, 550, 788));
    writer.setPageEvent(new Footer(context));
    this.document.open();
    this.fonts = new LinkedHashMap<>();
}

From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java

License:Apache License

private Document initPdfWriterAndDocument(String filePath, boolean addHandlerHeaderFooter)
        throws FileNotFoundException, DocumentException {
    chapterCounter = 0;// w  ww .  j a  v  a2 s . c o m
    currPageNumber = 1;
    Document document = new Document();
    document.setMargins(40, 40, 55, 55);
    PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(filePath));
    if (addHandlerHeaderFooter) {
        // Eventhandler fr die Fuzeile
        HeaderFooter hf = new HeaderFooter();
        pdfWriter.setPageEvent(hf);
    }
    document.open();
    return document;
}

From source file:de.jost_net.JVerein.io.Reporter.java

License:Open Source License

public Reporter(OutputStream out, String title, String subtitle, int maxRecords, float linkerRand,
        float rechterRand, float obererRand, float untererRand) throws DocumentException {
    this.out = out;
    rpt = new Document();
    hyph = new HyphenationAuto("de", "DE", 2, 2);
    PdfWriter writer = PdfWriter.getInstance(rpt, out);
    rpt.setMargins(linkerRand, rechterRand, obererRand, untererRand);
    AbstractPlugin plugin = Application.getPluginLoader().getPlugin(JVereinPlugin.class);
    rpt.addAuthor(plugin.getManifest().getName() + " - Version " + plugin.getManifest().getVersion());
    rpt.addTitle(subtitle);//w  w w  . jav  a2 s  .c  o  m

    String fuss = title + " | " + subtitle + " | " + "erstellt am "
            + new JVDateFormatTTMMJJJJ().format(new Date()) + "     " + "Seite: ";
    HeaderFooter hf = new HeaderFooter();
    hf.setFooter(fuss);
    writer.setPageEvent(hf);

    rpt.open();

    if (title.length() > 0) {
        Paragraph pTitle = new Paragraph(title, getFreeSansBold(13));
        pTitle.setAlignment(Element.ALIGN_CENTER);
        rpt.add(pTitle);

        Paragraph psubTitle = new Paragraph(subtitle, getFreeSansBold(10));
        psubTitle.setAlignment(Element.ALIGN_CENTER);
        rpt.add(psubTitle);
    }
    headers = new ArrayList<PdfPCell>();
    widths = new ArrayList<Integer>();
}

From source file:direccion.GeneradorFormato.java

public static void main(String[] args) {

    try {//from   w w w . j ava  2 s  .c  o  m
        Document document = new Document(PageSize.LETTER, 50, 50, 85, 50);
        document.addAuthor("Direccin");
        document.addTitle("Reporte de algo");

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Reporte prueba.pdf"));
        writer.setInitialLeading(16);
        Rectangle rct = new Rectangle(80, 104, 500, 688);
        writer.setBoxSize("art", rct);
        GeneradorFormato event = new GeneradorFormato();
        writer.setPageEvent(event);

        document.open();
        Paragraph parrafo2 = new Paragraph(
                "De aqui en adelante el contenido, ya se pone en automatico el encabezado en cada pgina y el nmero",
                FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED));
        parrafo2.setAlignment(0);
        document.add(parrafo2);

        document.add(Chunk.NEWLINE);

        document.close();
    } catch (FileNotFoundException | DocumentException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:edu.uc.modulocontable.general.GenerarFacturaComprasPDF.java

public void generarFactura(CabeceraFacturac cabezera, String ruta) {
    documento = new Documento(ruta);
    PdfWriter writer = documento.getWriter();
    writer.setPageEvent(encabezado);
    documento.setMargins(40, 30, 30, 40);
    informacionpiePaguina();/*  w w w  .ja v  a 2  s .c om*/
    documento.open();
    try {
        cabezera(cabezera);
    } catch (DocumentException ex) {
        Logger.getLogger(GenerarFacturaVentasPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        informaicionCliente(cabezera);
    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE,
                e.toString() + " informacion Cliente mal");
    }

    try {
        detalleDocumento(cabezera);
    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString() + " detalle documento");
    }

    try {
        totalDetalleDocumento(cabezera);

    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE,
                e.toString() + " total detalle documento");
    }

    documento.close();
}

From source file:edu.uc.modulocontable.general.GenerarFacturaVentasPDF.java

public void generarFactura(CabeceraFacturav cabezera, String ruta) {
    documento = new Documento(ruta);
    PdfWriter writer = documento.getWriter();
    writer.setPageEvent(encabezado);
    documento.setMargins(40, 30, 30, 40);
    informacionpiePaguina();//  w  ww. ja  v a2s  . c  o m
    documento.open();
    try {
        cabezera(cabezera);
    } catch (DocumentException ex) {
        Logger.getLogger(GenerarFacturaVentasPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        informaicionCliente(cabezera);
    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE,
                e.toString() + " informacion Cliente mal");
    }

    try {
        detalleDocumento(cabezera);
    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString() + " detalle documento");
    }

    try {
        totalDetalleDocumento(cabezera);

    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE,
                e.toString() + " total detalle documento");
    }

    try {
        informacionAdicional();
    } catch (Exception e) {
        Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, e.toString() + " informacion adicional");
    }

    documento.close();

}

From source file:edu.uc.modulocontable.general.GenerarKardexPDF.java

public void generarFactura(List<KardexLIFO> inventario, Producto producto, String ruta) {
    documento = new Documento(ruta);
    documento.setPageSize(PageSize.A4.rotate());
    PdfWriter writer = documento.getWriter();
    writer.setPageEvent(encabezado);
    documento.setMargins(40, 30, 30, 40);
    informacionpiePaguina();/*from   w  ww.  j  a va  2s. c om*/
    documento.open();
    try {
        cabecera(producto);
        cabeceraTabla();
        detalleDocumento(inventario);
        totalDetalleDocumento();
        comprobarLIFO();
    } catch (DocumentException ex) {
        Logger.getLogger(GenerarFacturaVentasPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    documento.close();

}

From source file:edu.uc.modulocontable.general.GenerarLibroDiarioPDF.java

public void generarFactura(List<Asiento> asientos, String ruta) {
    documento = new Documento(ruta);
    documento.setPageSize(PageSize.A4);/*from   ww w . j ava 2s .  c  om*/
    PdfWriter writer = documento.getWriter();
    writer.setPageEvent(encabezado);
    documento.setMargins(40, 30, 30, 40);
    informacionpiePaguina();
    documento.open();
    try {
        cabecera();
        cabeceraTabla();
        //detalleDocumento(inventario);
        totalDetalleDocumento();
        comprobarLIFO();
    } catch (DocumentException ex) {
        Logger.getLogger(GenerarFacturaVentasPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    documento.close();

}