Example usage for com.lowagie.text FontFactory getFont

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

Introduction

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

Prototype


public static Font getFont(String fontname, float size) 

Source Link

Document

Constructs a Font-object.

Usage

From source file:ExamplePdfPrinter.java

License:Open Source License

public static void main(String[] args) throws Exception {
    SpooledFile spooledFile = null; // TODO: provide your spool file!
    final IScsDataProvider dp = As400ScsDataProviderFactory.getPrintObjectDataProvider(spooledFile);
    final ReaderConfig rcfg = ReaderConfig.getDefault();
    final SCSStreamReader reader = new SCSStreamReader(dp, rcfg);

    PrinterConfig pcfg = PrinterConfig.getDefault();
    float FONT_SIZE = 9.0F;
    float leading = FONT_SIZE * 1.05F;

    // calculate font, font size, and margins for the PDF
    final Font monoSpacedFont = FontFactory.getFont(BaseFont.COURIER, FONT_SIZE);
    final Font monoSpacedFontBold = FontFactory.getFont(BaseFont.COURIER_BOLD, Font.BOLD);
    Rectangle pageSize = new Rectangle(PageSize.A4);
    final Document pdfdoc = new Document(pageSize);

    final FileOutputStream fos = new FileOutputStream("test.pdf");
    final PdfWriter pdfwriter = PdfWriter.getInstance(pdfdoc, fos);
    pdfdoc.open();//from ww w  . j av a2  s  .  c  o  m

    PdfPrinter pdfprinter = new PdfPrinter(pcfg, pdfdoc, monoSpacedFont, monoSpacedFontBold, leading);

    try {
        while (reader.hasNext()) {
            final IPrinterMicroCommand event = reader.next();
            if (event == null)
                break;
            pdfprinter.runMicroCommand(event);
        }
    } catch (EndOfFileSignal e) {
        System.out.println("eof.");
    }

    pdfprinter.finish();
    pdfdoc.close();
    pdfwriter.close();
}

From source file:bucks.GenerateChecks.java

License:Open Source License

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String message = "";
    if (url.equals("")) {
        url = getServletContext().getInitParameter("url");
        String str = getServletContext().getInitParameter("debug");
        if (str != null && str.equals("true"))
            debug = true;//from www . j a v a2 s.co m
        imageUrl = url + "js/images/city_logo3.jpg";
        try {
            FontFactory.register("c:/windows/fonts/verdana.ttf", "Verdana");
            FontFactory.register("c:/windows/fonts/verdana.ttf", "VerdanaB");
        } catch (Exception e) {
            // linux 
            // need to be installed
            try {
                FontFactory.register("/srv/webapps/marketbucks/fonts/verdana.ttf", "Verdana");
                FontFactory.register("/srv/webapps/marketbucks/fonts/verdanab.ttf", "VerdanaB");
            } catch (Exception ee) {
                FontFactory.register("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", "Verdana");
                FontFactory.register("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf", "VerdanaB");
            }
        }
        fnts = FontFactory.getFont(fontName, 8); // small
        fnt10 = FontFactory.getFont(fontName, 10);
        fnt = FontFactory.getFont(fontName, 12);
        fntb = FontFactory.getFont(fontName, 12, Font.BOLD);
        fnt2 = FontFactory.getFont(fontName, 14);
        fntb2 = FontFactory.getFont(fontName, 14, Font.BOLD);
    }
    HttpSession session = null;
    Enumeration<String> values = req.getParameterNames();
    String name = "", action = "Generate";
    String value = "", id = "";
    while (values.hasMoreElements()) {
        name = values.nextElement().trim();
        value = req.getParameter(name).trim();
        if (name.equals("action")) {
            action = value;
        }
        if (name.equals("id")) {
            id = value;
        }
    }
    if (!id.equals("")) {
        Batch batch = new Batch(debug, id);
        String back = batch.doSelect();
        if (back.equals("")) {
            generate(res, batch);
        } else {
            message += back;
        }
    } else {
        message = "Batch id not set ";
    }
    if (!message.equals("")) {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<head><title></title><body>");
        out.println("<p><font color=red>");
        out.println(message);
        out.println("</p>");
        out.println("</body>");
        out.println("</html>");
        out.flush();
    }

}

From source file:com.aurel.track.util.PdfUtils.java

License:Open Source License

public static void createPdfFromText(StringBuilder text, File pdfFile) {
    Document output = null;/*from www.ja v  a2s. c  o m*/
    try {
        BufferedReader input = new BufferedReader(new StringReader(text.toString()));
        // Size DIN A4
        //    see com.lowagie.text.PageSize for a complete list of page-size constants.
        output = new Document(PageSize.A4, 40, 40, 40, 40);
        float fntSize, lineSpacing;
        fntSize = 9f;
        lineSpacing = 11f;
        Font font1 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        Font font2 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        font2.setColor(Color.BLUE);
        Font font3 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        font3.setColor(Color.RED);

        PdfWriter.getInstance(output, new FileOutputStream(pdfFile));

        output.open();
        output.addAuthor("Steinbeis");
        output.addSubject("Debug Info");
        output.addTitle(pdfFile.getName());

        String line = "";
        while (null != (line = input.readLine())) {
            Font ft = font1;
            if (line.startsWith("%")) {
                ft = font2;
            }
            if (line.startsWith("% ^^^") || line.startsWith("% vvv")) {
                ft = font3;
            }
            Paragraph p = new Paragraph(lineSpacing, line, ft);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            output.add(p);
        }
        output.close();
        input.close();
    } catch (Exception e) {
        LOGGER.debug("Problem creating debug info pdf file", e);
    }
}

From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java

License:Open Source License

private static boolean parseContent(WikiPDFContext context, Wiki wiki, String content, Document document,
        PdfPCell cell, Connection db, ArrayList<Integer> wikiListTodo, ArrayList<Integer> wikiListDone,
        float cellWidth) throws Exception {

    LOG.debug("PARSING CONTENT: " + content);

    // Parse the wiki page
    int lastIndent = 0;
    boolean preTag = false;
    boolean pre = false;
    boolean code = false;
    boolean header = true;

    try {//from  w w w . j a v a 2  s  .  c  om

        BufferedReader in = new BufferedReader(new StringReader(content));
        String line = null;

        ArrayList unorderedParents = null;
        List thisList = null;
        Paragraph codeParagraph = null;

        while ((line = in.readLine()) != null) {
            // Tables
            if (line.startsWith("|")) {
                // @todo Close all ordered lists, unordered lists, and paragraphs

                // Parse the table
                line = parseTable(context, wiki, line, document, db, wikiListTodo, wikiListDone, in);

                if (line == null) {
                    continue;
                }
            }

            // Forms
            if (line.startsWith("[{form")) {
                // @todo close any lists or paragraphs

                // parseForm operates over all the lines that make up the form,
                // it will have to look forward so it returns an unparsed line
                parseForm(context, db, in, line, document, cell);
                continue;
            }

            // Handle code blocks
            // @todo chunk the content similar to WikiToHTMLUtils otherwise inaccurate
            if (line.startsWith("<pre>") || line.startsWith("<code>")) {
                if (!code && !pre) {
                    if (line.startsWith("<pre>")) {
                        preTag = true;
                        pre = true;
                    } else if (line.startsWith("<code>")) {
                        code = true;
                    }
                    codeParagraph = new Paragraph("", codeFont);
                    codeParagraph.setSpacingBefore(10);

                    if (pre && line.length() > ("<pre>").length()) {
                        int endOfLine = line.length();
                        if (line.endsWith("</pre>")) {
                            endOfLine = line.indexOf("</pre>");
                        }
                        // This line has some extra content that needs to be added
                        codeParagraph.add(new Chunk(
                                line.substring(line.indexOf("<pre>") + 5, endOfLine) + Chunk.NEWLINE));
                    }
                    if (code && line.length() > ("<code>").length()) {
                        int endOfLine = line.length();
                        if (line.endsWith("</code>")) {
                            endOfLine = line.indexOf("</code>");
                        }
                        // This line has some extra content that needs to be added
                        codeParagraph.add(new Chunk(
                                line.substring(line.indexOf("<code>") + 6, endOfLine) + Chunk.NEWLINE));
                    }
                    // See if this is a single line block
                    if (preTag && line.endsWith("</pre>")) {
                        // This is a single line block, so finish processing it
                    } else if (code && line.endsWith("</code>")) {
                        // This is a single line block, so finish processing it
                    } else {
                        // There are more lines to process, so do that
                        continue;
                    }
                }
            }
            if (line.startsWith("</code>") || line.endsWith("</code>")) {
                if (code) {
                    code = false;
                    if (line.indexOf("</code>") > 0 && !line.startsWith("<code>")) {
                        // This line has some extra content that needs to be added
                        codeParagraph
                                .add(new Chunk(line.substring(0, line.indexOf("</code>")) + Chunk.NEWLINE));
                    }
                    // Draw the final content
                    PdfPTable codeTable = new PdfPTable(1);
                    codeTable.setWidthPercentage(100);
                    codeTable.setSpacingBefore(10);
                    PdfPCell codeCell = new PdfPCell(codeParagraph);
                    codeCell.setPadding(20);
                    codeCell.setBorderColor(new Color(100, 100, 100));
                    codeCell.setBackgroundColor(new Color(200, 200, 200));
                    //            codeCell.setNoWrap(true);
                    codeTable.addCell(codeCell);
                    LOG.debug("document.add(codeTable)");
                    document.add(codeTable);
                    continue;
                }
            }
            if (line.startsWith("</pre>") || line.endsWith("</pre>")) {
                if (pre) {
                    preTag = false;
                    pre = false;
                    if (line.indexOf("</pre>") > 0 && !line.startsWith("<pre>")) {
                        // This line has some extra content that needs to be added
                        codeParagraph.add(new Chunk(line.substring(0, line.indexOf("</pre>")) + Chunk.NEWLINE));
                    }
                    // Draw the final content
                    PdfPTable codeTable = new PdfPTable(1);
                    codeTable.setWidthPercentage(100);
                    codeTable.setSpacingBefore(10);
                    PdfPCell codeCell = new PdfPCell(codeParagraph);
                    codeCell.setPadding(20);
                    codeCell.setBorderColor(new Color(100, 100, 100));
                    codeCell.setBackgroundColor(new Color(200, 200, 200));
                    //            codeCell.setNoWrap(true);
                    codeTable.addCell(codeCell);
                    LOG.debug("document.add(codeTable)");
                    document.add(codeTable);
                    continue;
                }
            }
            if (code || preTag) {
                // Append the chunk
                codeParagraph.add(new Chunk(line + Chunk.NEWLINE));
                continue;
            }

            // Section
            if (line.startsWith("=") && line.endsWith("=")) {
                // @todo close any open lists or paragraphs

                int hCount = parseHCount(line, "=");
                if (hCount > 6) {
                    hCount = 6;
                }
                String section = line.substring(line.indexOf("=") + hCount, line.lastIndexOf("=") - hCount + 1);
                header = true;
                context.foundHeader(hCount);
                String headerAnchor = null;

                // Store the h2's with anchors for table of contents or index
                if (hCount == 2) {
                    headerAnchor = StringUtils.toHtmlValue(section).replace(" ", "_");
                    context.getHeaderAnchors().put(headerAnchor, section);
                }
                if (hCount == 3) {
                    Paragraph title = new Paragraph(section.trim(), section2Font);
                    title.setSpacingBefore(10);
                    if (cell != null) {
                        LOG.debug("phrase.add(title)");
                        cell.addElement(title);
                    } else {
                        LOG.debug("document.add(title)");
                        document.add(title);
                    }
                } else if (hCount > 3) {
                    Paragraph title = new Paragraph(section.trim(), section3Font);
                    title.setSpacingBefore(10);
                    if (cell != null) {
                        LOG.debug("phrase.add(title)");
                        cell.addElement(title);
                    } else {
                        LOG.debug("document.add(title)");
                        document.add(title);
                    }
                } else {
                    Paragraph title = new Paragraph(section.trim(), sectionFont);
                    title.setSpacingBefore(10);
                    if (cell != null) {
                        LOG.debug("phrase.add(title)");
                        cell.addElement(title);
                    } else {
                        LOG.debug("document.add(title)");
                        document.add(title);
                    }
                }
                continue;
            }
            if (header) {
                header = false;
                if (line.trim().equals("")) {
                    // remove the extra space a user may leave after a header
                    continue;
                }
            }

            // Determine if this is a bulleted list
            if (line.startsWith("*") || line.startsWith("#")) {
                // Initialize the list array
                if (unorderedParents == null) {
                    unorderedParents = new ArrayList();
                    //            if (phrase != null) {
                    //              LOG.debug("phrase.add(new Paragraph(Chunk.NEWLINE))");
                    //              phrase.add(new Paragraph(Chunk.NEWLINE));
                    //            } else {
                    //              LOG.debug("document.add(new Paragraph(Chunk.NEWLINE))");
                    //              document.add(new Paragraph(Chunk.NEWLINE));
                    //            }
                }
                // Get the indent level
                boolean ol = line.startsWith("#");
                int hCount = WikiPDFUtils.parseHCount(line, ol ? "#" : "*");
                // Determine a shift in the tree
                if (lastIndent == 0) {
                    if (ol) {
                        thisList = new List(ol, 20);
                    } else {
                        thisList = new List(ol, 10);
                        thisList.setListSymbol(
                                new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12)));
                    }
                    thisList.setIndentationLeft(36);
                    thisList.setIndentationRight(36);
                    unorderedParents.add(thisList);
                } else {
                    if (hCount > lastIndent) {
                        if (ol) {
                            thisList = new List(ol, 20);
                        } else {
                            thisList = new List(ol, 10);
                            thisList.setListSymbol(
                                    new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12)));
                        }
                        thisList.setIndentationLeft(36);
                        thisList.setIndentationRight(36);
                        ((List) unorderedParents.get(unorderedParents.size() - 1)).add(thisList);
                        unorderedParents.add(thisList);
                    } else if (hCount < lastIndent) {
                        unorderedParents.remove(unorderedParents.size() - 1);
                        thisList = (List) unorderedParents.get(unorderedParents.size() - 1);
                    }
                }
                lastIndent = hCount;
                // Append the item...
                Paragraph thisItem = new Paragraph();
                parseLine(context, line.substring(hCount).trim(), thisItem, db, wikiListTodo, cellWidth, cell);
                thisList.add(new ListItem(thisItem));
                continue;
            }
            // List is finished, so append it to the document before working on
            // other paragraphs
            if (unorderedParents != null) {
                if (cell != null) {
                    LOG.debug("phrase.add((List) unorderedParents.get(0))");
                    cell.addElement((List) unorderedParents.get(0));
                } else {
                    LOG.debug("document.add((List) unorderedParents.get(0))");
                    document.add((List) unorderedParents.get(0));
                }
                unorderedParents = null;
                thisList = null;
                lastIndent = 0;
            }

            // Otherwise a simple paragraph
            Paragraph paragraph = new Paragraph();
            parseLine(context, line, paragraph, db, wikiListTodo, cellWidth, cell);
            if (cell != null) {
                LOG.debug("phrase.add(paragraph)");
                if (cell.getHorizontalAlignment() == Element.ALIGN_CENTER) {
                    paragraph.setAlignment(Element.ALIGN_CENTER);
                }
                paragraph.setSpacingBefore(5);
                cell.addElement(paragraph);
            } else {
                LOG.debug("document.add(paragraph)");
                paragraph.setSpacingBefore(5);
                document.add(paragraph);
            }
        }

        // Cleanup now that the lines are finished
        if (pre || code) {
            PdfPTable codeTable = new PdfPTable(1);
            codeTable.setWidthPercentage(100);
            codeTable.setSpacingBefore(10);
            PdfPCell codeCell = new PdfPCell(codeParagraph);
            codeCell.setPadding(20);
            codeCell.setBorderColor(new Color(100, 100, 100));
            codeCell.setBackgroundColor(new Color(200, 200, 200));
            //        codeCell.setNoWrap(true);
            codeTable.addCell(codeCell);
            LOG.debug("document.add(codeTable)");
            document.add(codeTable);
        }
        if (unorderedParents != null) {
            if (cell != null) {
                LOG.debug("phrase.add((List) unorderedParents.get(0))");
                cell.addElement((List) unorderedParents.get(0));
            } else {
                LOG.debug("document.add((List) unorderedParents.get(0))");
                document.add((List) unorderedParents.get(0));
            }
        }
        in.close();
    } catch (Exception e) {
        LOG.error("parseContent", e);
    }
    return true;
}

From source file:com.crm.webapp.util.PDFCustomExporter.java

License:Apache License

protected void createCustomFonts(String encoding) {
    this.cellFont = FontFactory.getFont(FontFactory.TIMES, encoding);
    this.facetFont = FontFactory.getFont(FontFactory.TIMES, encoding, Font.DEFAULTSIZE, Font.BOLD);
    if (facetFontColor != null) {
        this.facetFont.setColor(facetFontColor);
    }// w w  w  .j ava 2 s  . c o m

    if (facetFontSize != null) {
        this.facetFont.setSize(facetFontSize);
    }

    if (facetFontStyle != null) {
        this.facetFont.setStyle(facetFontStyle);
    }

    if (cellFontColor != null) {
        this.cellFont.setColor(cellFontColor);
    }

    if (cellFontSize != null) {
        this.cellFont.setSize(cellFontSize);
    }

    if (cellFontStyle != null) {
        this.cellFont.setStyle(cellFontStyle);
    }

    if (fontName != null) {
        cellFont.setFamily(fontName);
        facetFont.setFamily(fontName);
    }
}

From source file:CPS.Core.TODOLists.PDFExporter.java

License:Open Source License

public PDFExporter() {
    fontHeadFootItal = FontFactory.getFont(FontFactory.HELVETICA_OBLIQUE, 8);
    fontHeadFootReg = FontFactory.getFont(FontFactory.HELVETICA, 8);

    fontTableReg = FontFactory.getFont(FontFactory.HELVETICA, 10);
    fontTableHead = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 10);
    //      fontTableItal = FontFactory.getFont( FontFactory.HELVETICA_OBLIQUE, 10 );

    fontPageHeader = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 14);

    dateValidator = new CPSDateValidator();

}

From source file:datasoul.servicelist.ServiceListExporterDocument.java

License:Open Source License

public void addServicePlan() throws DocumentException {

    ServiceListTable slt = ServiceListTable.getActiveInstance();

    Paragraph p = new Paragraph(
            java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("SERVICE PLAN"),
            FontFactory.getFont(FontFactory.HELVETICA, 12));
    p.setAlignment(Element.ALIGN_CENTER);
    document.add(p);//  w  ww. j  a v a2s. c om

    p = new Paragraph(slt.getTitle(), FontFactory.getFont(FontFactory.HELVETICA_BOLD, 16));
    p.setAlignment(Element.ALIGN_CENTER);
    document.add(p);

    Table t = new Table(4);
    t.setWidths(new int[] { 10, 5, 50, 35 });
    t.setPadding(2.0f);
    t.setWidth(100.0f);

    t.addCell(createHeaderCell(
            java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("TIME")));
    t.addCell(
            createHeaderCell(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("MIN")));
    t.addCell(createHeaderCell(
            java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("TITLE")));
    t.addCell(createHeaderCell(
            java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("NOTES")));

    for (int i = 0; i < slt.getRowCount(); i++) {

        ServiceItem si = (ServiceItem) slt.getServiceItem(i);
        Cell c;

        // Start time
        c = new Cell(si.getStartTime());
        c.setMaxLines(1);
        c.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        c.setHorizontalAlignment(Cell.ALIGN_CENTER);
        t.addCell(c);

        // Duration
        c = new Cell(Integer.toString(si.getDuration()));
        c.setMaxLines(1);
        c.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        c.setHorizontalAlignment(Cell.ALIGN_RIGHT);
        t.addCell(c);

        // Title
        c = new Cell(si.getTitle());
        c.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        t.addCell(c);

        // Notes
        c = new Cell(si.getNotes());
        c.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        t.addCell(c);

    }

    document.add(t);

    p = new Paragraph(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("NOTES"),
            FontFactory.getFont(FontFactory.HELVETICA_BOLD));
    document.add(p);

    p = new Paragraph(slt.getNotes(), FontFactory.getFont(FontFactory.HELVETICA));
    p.setIndentationLeft(10.0f);
    document.add(p);

    document.newPage();

}

From source file:datasoul.servicelist.ServiceListExporterDocument.java

License:Open Source License

private void addSongHeader(Song s) throws DocumentException {
    Paragraph p;//from w w w  .  j  a  v a2s.c om

    p = new Paragraph(s.getTitle(), FontFactory.getFont(FontFactory.HELVETICA_BOLD, 16));
    document.add(p);

    if (!s.getSongAuthor().trim().equals("")) {
        p = new Paragraph(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("AUTHOR")
                + ": " + s.getSongAuthor(), FontFactory.getFont(FontFactory.HELVETICA_BOLD));
        document.add(p);
    }

    if (!s.getCopyright().trim().equals("")) {
        p = new Paragraph(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("COPYRIGHT")
                + s.getCopyright(), FontFactory.getFont(FontFactory.HELVETICA_BOLD));
        document.add(p);
    }

    if (!s.getSongSource().trim().equals("")) {
        p = new Paragraph(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("SOURCE")
                + s.getSongSource(), FontFactory.getFont(FontFactory.HELVETICA_BOLD));
        document.add(p);
    }

}

From source file:datasoul.servicelist.ServiceListExporterDocument.java

License:Open Source License

public void addSongLyrics(Song s) throws DocumentException {

    addSongHeader(s);/*  ww w.  j  av  a 2 s  .c o  m*/

    Paragraph p;

    p = new Paragraph(
            "(" + java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("LYRICS") + ")",
            FontFactory.getFont(FontFactory.HELVETICA, 8));
    document.add(p);

    String text = s.getText().replace(Song.CHORUS_MARK, "").replace(Song.SLIDE_BREAK, "");

    p = new Paragraph(" ", FontFactory.getFont(FontFactory.HELVETICA));
    document.add(p);

    p = new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA));
    document.add(p);

    document.newPage();

}

From source file:datasoul.servicelist.ServiceListExporterDocument.java

License:Open Source License

public void addTextItem(TextServiceItem t) throws DocumentException {

    String text = t.getText().replace(Song.CHORUS_MARK, "").replace(Song.SLIDE_BREAK, "");

    Paragraph p;//w w w.j a v  a2  s. c om

    p = new Paragraph(t.getTitle(), FontFactory.getFont(FontFactory.HELVETICA_BOLD, 16));
    document.add(p);

    p = new Paragraph(" ", FontFactory.getFont(FontFactory.HELVETICA));
    document.add(p);

    p = new Paragraph(text, FontFactory.getFont(FontFactory.HELVETICA));
    document.add(p);

    document.newPage();
}