Example usage for com.lowagie.text Phrase getFont

List of usage examples for com.lowagie.text Phrase getFont

Introduction

In this page you can find the example usage for com.lowagie.text Phrase getFont.

Prototype

public Font getFont() 

Source Link

Document

Gets the font of the first Chunk that appears in this Phrase.

Usage

From source file:org.lucee.extension.pdf.tag.PDF.java

License:Open Source License

private void doActionAddHeaderFooter(boolean isHeader) throws PageException, IOException, DocumentException {
    required("pdf", "write", "source", source);
    // required("pdf", "write", "destination", destination);

    /*/*from w  w w. jav  a  2s  .com*/
     * optinal - pages
     */
    /*
     * isBase64 = "yes|no" showonprint = "yes|no"> opacity = "header opacity" image = "image file name to be used as the header"
     * 
     */
    PDFStruct doc = toPDFDocument(source, password, null);
    PdfReader reader = doc.getPdfReader();
    BIF bif = null;
    if (NUMBERFORMAT_NUMERIC != numberformat) {
        ClassUtil classUtil = engine.getClassUtil();
        try {
            bif = classUtil.loadBIF(pageContext, "lucee.runtime.functions.displayFormatting.NumberFormat");
        } catch (Exception e) {
            e.printStackTrace();
            throw engine.getCastUtil().toPageException(e);
        }
    }
    // output stream
    boolean destIsSource = destination != null && doc.getResource() != null
            && destination.equals(doc.getResource());
    OutputStream os = null;
    if (!Util.isEmpty(name) || destIsSource) {
        os = new ByteArrayOutputStream();
    } else if (destination != null) {
        os = destination.getOutputStream();
    }
    PdfStamper stamper = null;
    try {
        if (destination != null && destination.exists() && !overwrite)
            throw engine.getExceptionUtil()
                    .createApplicationException("destination file [" + destination + "] already exists");

        int len = reader.getNumberOfPages();
        Set<Integer> pageSet = PDFUtil.parsePageDefinition(pages, len);
        stamper = new PdfStamper(reader, destination.getOutputStream());
        if (font == null)
            font = getDefaultFont();
        // , new Font(FontFamily.HELVETICA, 14)
        for (int p = 1; p <= len; p++) {
            if (pageSet != null && !pageSet.contains(p))
                continue;

            Phrase header = text(text, p, len, numberformat, bif, font);
            // vertical orientation
            float y;

            if (isHeader) {
                y = reader.getPageSize(p).getTop(header.getFont().getCalculatedSize() + (topmargin - 3));
            } else {
                y = reader.getPageSize(p).getBottom((bottommargin + 2));
                /*
                 * System.out.println("y:"+y); System.out.println("bottom:"+reader.getPageSize(p).getBottom()); System.out.println("margin:"+bottommargin);
                 * System.out.println("font:"+header.getFont().getSize()); System.out.println("CalculatedStyle:"+header.getFont().getCalculatedStyle());
                 * System.out.println("CalculatedSize:"+header.getFont().getCalculatedSize());
                 */
            }
            // float yh = reader.getPageSize(p).getTop(topmargin);
            // float yf = reader.getPageSize(p).getBottom(bottommargin);
            System.out.println("++++++++++++");
            System.out.println(y);
            System.out.println(reader.getPageSize(p).getTop());
            // horizontal orientation

            float x = reader.getPageSize(p).getWidth() / 2;
            if (Element.ALIGN_LEFT == align) {
                x = leftmargin;
            } else if (Element.ALIGN_RIGHT == align) {
                x = reader.getPageSize(p).getWidth() - rightmargin;
            } else {
                x = reader.getPageSize(p).getWidth() / 2;
            }
            ColumnText.showTextAligned(stamper.getOverContent(p), align, header, x, y, 0);

        }
    } finally {
        try {
            if (stamper != null)
                stamper.close();
        } catch (IOException ioe) {
        }
        ;
        Util.closeEL(os);
        if (os instanceof ByteArrayOutputStream) {
            if (destination != null)
                engine.getIOUtil().copy(new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray()),
                        destination, true);// MUST overwrite
            if (!Util.isEmpty(name)) {
                pageContext.setVariable(name,
                        new PDFStruct(((ByteArrayOutputStream) os).toByteArray(), password));
            }
        }
    }

    // PdfReader pr = doc.getPdfReader();
    // output
    /*
     * boolean destIsSource = doc.getResource()!=null && destination.equals(doc.getResource());
     * 
     * OutputStream os=null; if(destIsSource){ os=new ByteArrayOutputStream(); } else if(destination!=null) { os=destination.getOutputStream(); }
     * 
     * try { PDFUtil.concat(new PDFStruct[]{doc}, os, true, true, true,version); } finally { Util.closeEL(os); if(os instanceof ByteArrayOutputStream) {
     * if(destination!=null)engine.getIOUtil().copy(new ByteArrayInputStream(((ByteArrayOutputStream)os).toByteArray()), destination,true);// MUST overwrite
     * } }
     */
}

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createSpeakerNotes4PerPage(File file) {
    //page size notes:
    //martin-top,left,right,bottom = 36
    //widht :612//from w ww. jav  a2 s  . c om
    //height : 792
    //usable space 540 x 720
    // step 1: creation of a document-object

    Document document = new Document(PageSize.LETTER);

    try {
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        //  writer.setDefaultColorspace(PdfName.DEFAULTRGB, null);
        // writer.setStrictImageSequence(true);

        // step 3: we open the document
        document.open();

        PdfPTable table;
        PdfPCell cell;
        int entryCount = 0;
        int entryOnPage = 0;
        int currentIndex = VUE.getActivePathway().getIndex();

        VUE.getActivePathway().setIndex(-1);

        for (LWPathway.Entry entry : VUE.getActivePathway().getEntries()) {

            final LWSlide slide = entry.produceSlide();
            final LWComponent toDraw = (slide == null ? entry.node : slide);
            final String notes = entry.getNotes();
            entryCount++;

            table = new PdfPTable(new float[] { 1, 1 });
            table.getDefaultCell().setBorder(0);
            //table.getDefaultCell().setPaddingBottom(50.0f);
            table.setSpacingAfter(20.0f);
            Paragraph p = new Paragraph();

            p.setAlignment(Element.ALIGN_CENTER);

            Phrase phrase = new Phrase(notes);
            Font f = phrase.getFont();
            f.setSize(8.0f);
            p.setFont(f);
            cell = new PdfPCell(phrase);
            cell.setBorder(0);

            PdfPCell i2 = new PdfPCell();
            i2.setFixedHeight(172);
            i2.setBorder(0);

            //Render the table then throw the images on
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(SlideSizeX, SlideSizeY);

            Point2D.Float offset = new Point2D.Float();
            // center vertically only if landscape mode
            //if (format.getOrientation() == PageFormat.LANDSCAPE)
            //TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit)

            Rectangle2D bounds = null;
            //if (!entry.isMapView())
            bounds = slide.getBounds();
            //else 
            //bounds = entry.getFocal().getBounds();

            Dimension page = null;

            page = new Dimension(SlideSizeX, 172);

            //PdfTemplate tp = cb.createTemplate(document.getPageSize().width()-80, document.getPageSize().height()-80);
            double scale = ZoomTool.computeZoomFit(page, 5, bounds, offset, true);
            PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(SlideSizeX, SlideSizeY, getFontMapper(),
                    false, 60.0f);
            DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle
                    entry.isMapView() ? entry.getFocal() : slide, false); // todo: absolute links shouldn't be spec'd here

            dc.setClipOptimized(false);
            dc.setPrintQuality();
            /*if (!entry.isMapView())                   
               slide.drawZero(dc);
            else
            {                
               entry.getFocal().draw(dc);
            }*/
            toDraw.drawFit(dc, 0);

            g2d.dispose();
            //document.add(Image.getInstance(tp));

            //  int position = cell.
            //     drawSequenceNumber(writer,36,position+203,entryCount);

            //     cb.addTemplate(tp,56, position);                                                           

            Image img = Image.getInstance(tp);
            table.addCell(img);
            table.addCell(cell);
            p.add(table);
            document.add(p);
        }
        VUE.getActivePathway().setIndex(currentIndex);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();

}

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createNodeNotes4PerPage(File file) {
    //page size notes:
    //martin-top,left,right,bottom = 36
    //widht :612//from w w  w.  j av a  2 s. c  o m
    //height : 792
    //usable space 540 x 720
    // step 1: creation of a document-object

    Document document = new Document(PageSize.LETTER);

    try {
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        //  writer.setDefaultColorspace(PdfName.DEFAULTRGB, null);
        // writer.setStrictImageSequence(true);

        // step 3: we open the document
        document.open();

        PdfPTable table;
        PdfPCell cell;
        int entryCount = 0;
        int entryOnPage = 0;

        Iterator i = VUE.getActiveMap().getAllDescendents(LWComponent.ChildKind.PROPER).iterator();
        while (i.hasNext()) {
            LWComponent component = (LWComponent) i.next();
            if (component instanceof LWNode) {
                final LWNode node = (LWNode) component;

                final String notes = node.getNotes();

                entryCount++;

                table = new PdfPTable(new float[] { 1, 1 });
                table.getDefaultCell().setBorder(0);

                table.setSpacingAfter(20.0f);
                Paragraph p = new Paragraph();

                p.setAlignment(Element.ALIGN_CENTER);

                Phrase phrase = new Phrase(notes);

                Font f = phrase.getFont();
                f.setSize(8.0f);
                p.setFont(f);
                cell = new PdfPCell(phrase);
                cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
                cell.setBorder(0);

                PdfPCell i2 = new PdfPCell();
                i2.setFixedHeight(172);
                i2.setBorder(0);

                //Render the table then throw the images on
                PdfContentByte cb = writer.getDirectContent();
                PdfTemplate tp = cb.createTemplate(SlideSizeX, SlideSizeY);

                Point2D.Float offset = new Point2D.Float();
                //center vertically only if landscape mode
                //if (format.getOrientation() == PageFormat.LANDSCAPE)
                //TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit)

                Rectangle2D bounds = null;

                bounds = node.getBounds();

                Dimension page = null;

                page = new Dimension(SlideSizeX, 172);

                //   PdfTemplate tp = cb.createTemplate(document.getPageSize().width()-80, document.getPageSize().height()-80);
                double scale = ZoomTool.computeZoomFit(page, 15, bounds, offset, true);
                PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(SlideSizeX, SlideSizeY, getFontMapper(),
                        false, 60.0f);
                DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle
                        node, false); // todo: absolute links shouldn't be spec'd here

                dc.setClipOptimized(false);
                dc.setPrintQuality();
                node.drawFit(dc, 15);

                g2d.dispose();

                Image img = Image.getInstance(tp);
                table.addCell(img);
                table.addCell(cell);
                p.add(table);
                document.add(p);
            }
        }

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();

}