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

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

Introduction

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

Prototype

public PdfStamper(PdfReader reader, OutputStream os) throws DocumentException, IOException 

Source Link

Document

Starts the process of adding extra content to an existing PDF document.

Usage

From source file:questions.stamppages.PageXofYRightAligned.java

public static void main(String[] args) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {// ww  w.ja va  2 s  .  c o m
        Document document = new Document();
        PdfWriter.getInstance(document, baos);
        document.open();
        BufferedReader reader = new BufferedReader(new FileReader(RESOURCE));
        String line;
        Paragraph p;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph(line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            document.newPage();
        }
        reader.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        PdfReader reader = new PdfReader(baos.toByteArray());
        int n = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte page;
        Rectangle rect;
        BaseFont bf = BaseFont.createFont();
        for (int i = 1; i < n + 1; i++) {
            page = stamper.getOverContent(i);
            rect = reader.getPageSizeWithRotation(i);
            page.beginText();
            page.setFontAndSize(bf, 12);
            page.showTextAligned(Element.ALIGN_RIGHT, "page " + i + " of " + n, rect.getRight(36),
                    rect.getTop(32), 0);
            page.endText();
        }
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}

From source file:questions.stamppages.RemoveAttachmentAnnotations.java

public static void main(String[] args) throws IOException, DocumentException {
    createPdfWithAttachments();//from   www  .  j a  va 2s .  co m
    PdfReader reader = new PdfReader(RESOURCE);
    PdfDictionary page;
    PdfDictionary annotation;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        page = reader.getPageN(i);
        PdfArray annots = page.getAsArray(PdfName.ANNOTS);
        if (annots != null) {
            for (int j = annots.size() - 1; j >= 0; j--) {
                annotation = annots.getAsDict(j);
                if (PdfName.FILEATTACHMENT.equals(annotation.get(PdfName.SUBTYPE))) {
                    annots.remove(j);
                }
            }
        }
    }
    reader.removeUnusedObjects();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    stamper.close();
}

From source file:questions.stamppages.StampAndAddColumns.java

public static void main(String[] args) {
    try {/*  w w  w  .  ja v  a 2  s  .  c om*/
        PdfReader reader = new PdfReader(RESOURCE);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        AcroFields form = stamper.getAcroFields();
        form.setField("Who", "world");
        ColumnText ct = new ColumnText(stamper.getOverContent(1));
        String line;
        Phrase p;
        BufferedReader br = new BufferedReader(new FileReader(TXT));
        while ((line = br.readLine()) != null) {
            p = new Phrase(line + "\n");
            ct.addText(p);
        }
        int status = ColumnText.START_COLUMN;
        ct.setSimpleColumn(100, 700, 495, 100);
        status = ct.go();
        int pageCt = 1;
        while (ColumnText.hasMoreText(status)) {
            stamper.insertPage(++pageCt, reader.getPageSize(1));
            ct.setYLine(700);
            ct.setCanvas(stamper.getOverContent(pageCt));
            status = ct.go();
        }
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:ud.ing.modi.controlador.ptorecarga.GeneracionFactura.java

public void generacionFacturaPDF() {
    try {/*  w  w w.ja va2  s  .co  m*/
        System.out.println("Generaa");
        cargarDatosFactura();

        if (this.factura != null) {

            PdfReader reader = new PdfReader(
                    "C:\\Users\\Administrador\\Dropbox\\Tesis\\Monedero Digital\\Factura_PtoRecarga.pdf");
            String nombreFact = "D:\\REPOSITORIO TESIS2\\REPO\\MONEDERO_DIGITAL_APP\\MONEDERO_DIGITAL_APP_WEB\\src\\main\\webapp\\docs\\FACTURA_"
                    + traerUsu() + ".pdf";
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(nombreFact));
            AcroFields formulario = stamp.getAcroFields();

            formulario.setField("NUM_FACTURA", Integer.toString(this.factura.getCodFactura()));

            int mes = this.factura.getFechaCorte().getMonth() + 1;
            int anio = this.factura.getFechaCorte().getYear() + 1900;
            String fechaC = this.factura.getFechaCorte().getDate() + "-" + mes + "-" + anio;
            formulario.setField("FECHA_CORTE", fechaC);
            mes = this.factura.getFechaVencimiento().getMonth() + 1;
            anio = this.factura.getFechaVencimiento().getYear() + 1900;
            String fechaL = this.factura.getFechaVencimiento().getDate() + "-" + mes + "-" + anio;
            formulario.setField("FECHA_LIMITE", fechaL);
            formulario.setField("VALOR_PAGO", "$ " + Double.toString(this.factura.getValorTotalPago()));
            formulario.setField("COD_CLIENTE", Integer.toString(this.factura.getPuntoRecarga().getIdCliente()));
            formulario.setField("NOMBRE_CLIENTE", this.factura.getPuntoRecarga().getRazonSocial());
            formulario.setField("NUM_RECARGAS", Integer.toString(this.factura.getNumTotalRecargas()));
            formulario.setField("VALOR_RECARGAS", "$ " + Double.toString(this.factura.getValorTotalRecargas()));
            formulario.setField("VALOR_COMISION",
                    "$ " + Double.toString(this.factura.getValorCalculoComision()));

            stamp.setFormFlattening(true);
            stamp.close();

        }

    } catch (IOException ex) {
        Logger.getLogger(GeneracionFactura.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(GeneracionFactura.class.getName()).log(Level.SEVERE, null, ex);
    }
}