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

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

Introduction

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

Prototype


public PdfContentByte getDirectContent() 

Source Link

Document

Use this method to get the direct content for this document.

Usage

From source file:com.vectorprint.report.itext.TocOutputStream.java

License:Open Source License

@Override
public void secondPass(InputStream firstPass, OutputStream orig) throws IOException {
    PdfReader reader = null;/* w w  w.  j a  va2s .c  om*/
    VectorPrintDocument vpd = (VectorPrintDocument) outer.getDocument();
    try {
        reader = new PdfReader(firstPass);
        prepareToc();
        // init fresh components for second pass styling
        StylerFactory _stylerFactory = outer.getStylerFactory().getClass().newInstance();
        StylerFactoryHelper.SETTINGS_ANNOTATION_PROCESSOR.initSettings(_stylerFactory, outer.getSettings());
        _stylerFactory.setLayerManager(outer.getElementProducer());
        _stylerFactory.setImageLoader(outer.getElementProducer());
        outer.getStyleHelper().setStylerFactory(_stylerFactory);
        EventHelper event = outer.getEventHelper().getClass().newInstance();
        event.setItextStylerFactory(_stylerFactory);
        event.setElementProvider(outer.getElementProducer());
        ((DefaultElementProducer) outer.getElementProducer()).setPh(event);
        Document d = new VectorPrintDocument(event, _stylerFactory, outer.getStyleHelper());
        PdfWriter w = PdfWriter.getInstance(d, orig);
        w.setPageEvent(event);
        outer.getStyleHelper().setVpd((VectorPrintDocument) d);
        _stylerFactory.setDocument(d, w);
        DocumentStyler ds = _stylerFactory.getDocumentStyler();
        outer.getStyleHelper().style(d, null, StyleHelper.toCollection(ds));
        d.open();
        ds.styleAfterOpen(d, null);
        List outline = SimpleBookmark.getBookmark(reader);
        if (!ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
            printToc(d, w, vpd);
            if (outline != null) {
                int cur = w.getCurrentPageNumber();
                SimpleBookmark.shiftPageNumbers(outline, cur, null);
            }
            d.newPage();
        }
        outer.getSettings().put(ReportConstants.DEBUG, Boolean.FALSE.toString());
        for (int p = 1; p <= reader.getNumberOfPages(); p++) {
            Image page = Image.getInstance(w.getImportedPage(reader, p));
            page.setAbsolutePosition(0, 0);
            d.setPageSize(page);
            d.newPage();
            Chunk i = new Chunk(" ");
            if (vpd.getToc().containsKey(p)) {
                Section s = null;
                for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) {
                    if (e.getKey() == p) {
                        s = e.getValue().get(0);
                        break;
                    }
                }
                i.setLocalDestination(s.getTitle().getContent());
            }
            d.add(i);
            w.getDirectContent().addImage(page);
            w.freeReader(reader);
        }
        if (_stylerFactory.getDocumentStyler().getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
            printToc(d, w, vpd);
        }
        w.setOutlines(outline);
        if (outer.isWasDebug()) {
            event.setLastPage(outer.getWriter().getCurrentPageNumber());
            d.setPageSize(new Rectangle(ItextHelper.mmToPts(297), ItextHelper.mmToPts(210)));
            d.setMargins(5, 5, 5, 5);
            d.newPage();
            outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString());
            event.setDebugHereAfter(true);
            DebugHelper.appendDebugInfo(w, d, outer.getSettings(), _stylerFactory);
        }
        d.close();
    } catch (VectorPrintException | DocumentException | InstantiationException | IllegalAccessException ex) {
        throw new VectorPrintRuntimeException(ex);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.vectorprint.report.itext.TocOutputStream.java

License:Open Source License

private void printToc(Document d, PdfWriter w, VectorPrintDocument vpd)
        throws VectorPrintException, InstantiationException, IllegalAccessException, DocumentException {
    DocumentStyler ds = outer.getStylerFactory().getDocumentStyler();
    if (ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
        d.add(Chunk.NEXTPAGE);//from   w w w .j  av a2s.  c  o m
    }
    if (outer.isWasDebug()) {
        outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString());
        PdfContentByte canvas = w.getDirectContent();
        outer.startLayerInGroup(ReportConstants.DEBUG, canvas);
        BaseFont bf = DebugHelper.debugFont(canvas, outer.getSettings());
        canvas.showTextAligned(Element.ALIGN_RIGHT,
                "FOR DEBUG INFO IN THE DOCUMENT TURN OFF TOC (-DocumentSettings.toc=false)", d.right(),
                d.getPageSize().getHeight() - ItextHelper.getTextHeight("F", bf, 8), 0);
        canvas.endLayer();
    }
    ElementProducer ep = outer.getElementProducer();
    StylerFactory sf = outer.getStylerFactory();
    PdfPTable tocTable = ep.createElement(null, PdfPTable.class, sf.getStylers(DocumentSettings.TOCTABLEKEY));
    for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) {
        String link = null;
        for (Section s : e.getValue()) {
            if (ds.isParameterSet(DocumentSettings.TOCMAXDEPTH)
                    && ds.getValue(DocumentSettings.TOCMAXDEPTH, Integer.class) < s.getDepth()) {
                continue;
            }
            if (link == null) {
                link = s.getTitle().getContent();
            }
            Chunk c = ep.createElement(s.getTitle().getContent(), Chunk.class,
                    sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY));
            if (ds.getValue(DocumentSettings.TOCDOTS, Boolean.class)) {
                float tw = ItextHelper.getTextWidth(c);
                float cw = tocTable.getAbsoluteWidths()[0];
                float dw = ItextHelper.getTextWidth(
                        ep.createElement(".", Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)))
                        * 1.5f;
                int numDots = (int) ((cw > tw) ? (cw - tw) / dw : 0);
                char[] dots = new char[numDots];
                Arrays.fill(dots, '.');
                c = ep.createElement(s.getTitle().getContent() + "  " + String.valueOf(dots), Chunk.class,
                        sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY));
            }
            c.setLocalGoto(link);
            tocTable.addCell(
                    ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)));
            c = ep.createElement(e.getKey(), Chunk.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY));
            c.setLocalGoto(link);
            tocTable.addCell(
                    ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY)));
        }
    }
    d.add(tocTable);
}

From source file:com.xumpy.itext.services.HeaderFooter.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfPTable header = new PdfPTable(3);
    try {/*  w w  w  .  ja va 2 s  .com*/
        header.setWidths(new int[] { 24, 24, 24 });
        header.setTotalWidth(527);
        header.setLockedWidth(true);
        header.getDefaultCell().setFixedHeight(20);
        header.getDefaultCell().setBorder(Rectangle.BOTTOM);
        for (PdfPCell cell : tableHeader) {
            cell.setBorder(Rectangle.NO_BORDER);
            header.addCell(cell);
        }
        header.writeSelectedRows(0, -1, 60, 803, writer.getDirectContent());
    } catch (DocumentException ex) {
        Logger.getLogger(HeaderFooter.class.getName()).log(Level.SEVERE, null, ex);
    }

    PdfPTable footer = new PdfPTable(3);
    try {
        footer.setWidths(new int[] { 24, 24, 24 });
        footer.setTotalWidth(527);
        footer.setLockedWidth(true);
        footer.getDefaultCell().setFixedHeight(20);
        footer.getDefaultCell().setBorder(Rectangle.BOTTOM);
        for (PdfPCell cell : tableFooter) {
            cell.setBorder(Rectangle.NO_BORDER);
            footer.addCell(cell);
        }
        footer.writeSelectedRows(0, -1, 60, 200, writer.getDirectContent());
    } catch (DocumentException ex) {
        Logger.getLogger(HeaderFooter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:comisionesafis.informes.CintaComisionesPie.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    //Rectangle rect = writer.getBoxSize("art");
    Font font = new Font(Font.FontFamily.COURIER, 10, Font.NORMAL);
    Phrase texto = new Phrase("Pgina " + Integer.toString(document.getPageNumber()), font);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, texto, 300, 30, 0);
}

From source file:comisionesafis.informes.FacturasComisionesAgentesCabecera.java

@Override
public void onStartPage(PdfWriter writer, Document document) {
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("FFF"), 200, 830, 0);
}

From source file:comisionesafis.informes.FacturasComisionesAgentesPie.java

@Override
public void onEndPage(PdfWriter writer, Document document) {

    Font font = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT,
            new Phrase("FacturasAgentes", font), 100, 80, 0);
}

From source file:comisionesafis.informes.LiquidacionComisionesCabecera.java

@Override
public void onStartPage(PdfWriter writer, Document document) {
    Font fontFecha = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT,
            new Phrase("11/04/2014", fontFecha), 500, 830, 0);
    Font fontPaginaNum = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT,
            new Phrase("Pgina " + document.getPageNumber(), fontPaginaNum), 500, 823, 0);
    Font fontLIQ03 = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT,
            new Phrase("LIQ03_0202", fontLIQ03), 500, 816, 0);
    Font fontTitulo = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER,
            new Phrase("LIQUIDACIN DE COMISIONES: Marzo 2014", fontTitulo), 300, 800, 0);
    Font fontDireccion = new Font(Font.FontFamily.COURIER, 10, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT,
            new Phrase("PELAYO MUTUA DE SEGUROS", fontDireccion), 400, 750, 0);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT,
            new Phrase("CL SANTA ENGRACIA, 67", fontDireccion), 400, 730, 0);
}

From source file:comisionesafis.informes.LiquidacionComisionesPie.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    Font font = new Font(Font.FontFamily.COURIER, 8, Font.NORMAL);
    ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT,
            new Phrase("Ejemplar para el mediador", font), 60, 20, 0);
}

From source file:Controlador.ControladorCrearPase.java

private static void absText(PdfWriter writer, String text, int x, int y) throws DocumentException, IOException {

    PdfContentByte cb = writer.getDirectContent();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.saveState();/*from www .  j  a  va2s  .c  o m*/
    cb.beginText();
    cb.moveText(x, y);
    cb.setFontAndSize(bf, 12);
    cb.showText(text);
    cb.endText();
    cb.restoreState();

}

From source file:Controlador.EmailWithPdf.java

/**
 * Writes the content of a PDF file (using iText API)
 * to the {@link OutputStream}./*w  w w . j a  va 2s . co  m*/
 * @param outputStream {@link OutputStream}.
 * @throws Exception
 */
public void writePdf(OutputStream outputStream) throws Exception {
    Document document = new Document(PageSize.LETTER, 50, 50, 50, 30);
    Font boldFontTitulo = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);
    Font boldFontTexto = new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD);
    Font FontTexto = new Font(Font.FontFamily.HELVETICA, 12);

    // document.setPageSize(null);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    Image imagen = Image.getInstance("D:\\ALEX\\Bseguros\\web\\img\\Aseguradoras\\BSeguroLogo.png");
    Image imagen2 = Image.getInstance("D:\\ALEX\\Bseguros\\web\\img\\Aseguradoras\\mapfre.png");

    DateFormat df = new SimpleDateFormat("dd/MM/YYYY");
    Calendar cdos = Calendar.getInstance();
    Date datediamas = new Date();
    Date dateaniomas = new Date();
    cdos.add(Calendar.DATE, 1);
    datediamas = cdos.getTime();
    String fechadiamas = df.format(datediamas);
    cdos.add(Calendar.YEAR, 1);
    dateaniomas = cdos.getTime();
    String fechavencimiento = df.format(dateaniomas);
    document.open();

    PdfContentByte canvas = writer.getDirectContent();
    Rectangle rect = new Rectangle(36, 36, 579, 756);
    rect.setBorder(Rectangle.BOX);
    rect.setBorderWidth(2);
    canvas.rectangle(rect);

    //         Rectangle rect= new Rectangle(36,108);
    //         rect.setBorder(Rectangle.BOX);
    //         
    //rect.setBorderColor(BaseColor.BLACK);
    //rect.setBorderWidth(2);
    //document.add(rect);
    document.addTitle("Cotizacion");
    document.addSubject("Cotizacion");
    document.addKeywords("Cotizacion, seguros");
    document.addAuthor("BSeguro");
    document.addCreator("Bseguro");

    imagen.scaleAbsoluteHeight(30f);
    imagen.setAbsolutePosition(45f, 720f);
    imagen2.scaleAbsoluteHeight(30f);
    imagen2.setAbsolutePosition(450f, 720f);
    document.add(imagen);
    document.add(imagen2);

    Paragraph paragraph2 = new Paragraph("DATOS DE TU POLIZA" + fechadiamas + " hasta: " + fechavencimiento,
            boldFontTitulo);
    Paragraph paragraph3 = new Paragraph(
            "DhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhATOS DE TU POLIZA",
            boldFontTitulo);

    paragraph2.setAlignment(Element.ALIGN_CENTER);

    document.add(paragraph2);
    document.add(paragraph3);

    document.close();

}