Example usage for com.lowagie.text.pdf BaseFont EMBEDDED

List of usage examples for com.lowagie.text.pdf BaseFont EMBEDDED

Introduction

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

Prototype

boolean EMBEDDED

To view the source code for com.lowagie.text.pdf BaseFont EMBEDDED.

Click Source Link

Document

if the font has to be embedded

Usage

From source file:questions.graphics2D.SwingForceArialUni.java

public static void main(String[] args) {
    Document document = new Document(new Rectangle(210, 25));
    try {/*from  w w  w. j  a  v  a  2  s. c o  m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        FontMapper arialuni = new FontMapper() {
            public BaseFont awtToPdf(Font font) {
                try {
                    return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H,
                            BaseFont.EMBEDDED);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            public Font pdfToAwt(BaseFont font, int size) {
                return null;
            }

        };
        Graphics2D g2 = cb.createGraphics(200, 50, arialuni);
        g2.setFont(null);
        g2.drawString("Greek mu: \u03bc - \u039c; degree symbol: \u00b0", 0, 40);
        g2.dispose();
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

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

From source file:test.poi.ConvertDocxResumeToPDF.java

License:LGPL

private static void create() {
    long startTime = System.currentTimeMillis();

    try {/*  w w  w .ja  v a2  s  . c om*/
        // 1) Load docx with POI XWPFDocument
        XWPFDocument document = new XWPFDocument(
                ConvertDocxResumeToPDF.class.getClassLoader().getResourceAsStream("DocxResume.docx"));

        // 2) Convert POI XWPFDocument 2 PDF with iText
        File outFile = new File("d:/DocxResume.pdf");
        outFile.getParentFile().mkdirs();

        OutputStream out = new FileOutputStream(outFile);
        PdfOptions options = PdfOptions.create();
        //
        options.fontProvider(new IFontProvider() {

            @Override
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    BaseFont bfChinese = BaseFont.createFont("c:/Windows/Fonts/arialuni.ttf",
                            BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
                } catch (Throwable e) {
                    e.printStackTrace();
                    return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
                }
            }
        });

        PdfConverter.getInstance().convert(document, out, options);
    } catch (Throwable e) {
        e.printStackTrace();
    }

    System.out.println("Generate DocxResume.pdf with " + (System.currentTimeMillis() - startTime) + " ms.");
}

From source file:textdisplay.TagFilter.java

License:Educational Community License

public void replaceTagsWithPDFEncoding(String[] tags, styles[] tagStyles, OutputStream os)
        throws DocumentException {
    //   FileWriter w = null;

    try {// w w  w.j  ava  2s  .c  om
        BaseFont bf = BaseFont.createFont("/usr/Junicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        Document doc = new Document();
        PdfWriter p = PdfWriter.getInstance(doc, os);
        doc.open();
        Paragraph para = new Paragraph();
        para.setFont(new Font(bf, 12, Font.NORMAL));
        //doc.add(para);
        Font italic = new Font(bf, 12, Font.ITALIC);
        Font bold = new Font(bf, 12, Font.BOLD);
        Font underlined = new Font(bf, 12, Font.UNDERLINE);

        StringBuilder chunkBuffer = new StringBuilder(""); //holds the next bit of content that will be added to the pdf as a chunk
        styles chunkStyle = null; //the style to be applied to chunkBuffer when it gets added to the document
        String chunkTag = "";
        Stack<String> wrappingTags = new Stack();
        Stack<styles> wrappingStyles = new Stack();
        String content = text;
        Boolean inTag = false; //is this inside a tag, meaning between the < and >
        String tagTextBuffer = ""; //the text of the tag, including name and any parameters
        Boolean beingTagged = false; //Is the parser currently reading character data that is surrounded by a tag that demands styling
        for (int charCounter = 0; charCounter < this.text.length(); charCounter++) {

            if (text.charAt(charCounter) == '>') {
                inTag = false;
                //if this was a self closing tag, dont do anything
                if (tagTextBuffer.contains("/>")) {
                    tagTextBuffer = "";
                } else {
                    //this is a closing tag, save the chunk and pop the tag and style off of the stack
                    if (tagTextBuffer.startsWith("/")) {
                        if (chunkStyle != null)
                            System.out.print(" closing tag " + tagTextBuffer + " with style "
                                    + chunkStyle.name() + "\n");
                        else
                            System.out.print(" closing tag " + tagTextBuffer + " with style null" + "\n");
                        if (chunkStyle == styles.paragraph)
                            chunkBuffer = new StringBuilder("\n" + chunkBuffer);
                        Chunk c = new Chunk(chunkBuffer.toString());
                        styleChunk(c, chunkStyle);

                        if (chunkStyle != styles.remove)
                            para.add(c);
                        chunkBuffer = new StringBuilder("");
                        chunkStyle = null;
                        chunkTag = "";
                        if (!wrappingStyles.empty()) {
                            chunkStyle = wrappingStyles.pop();
                            chunkTag = wrappingTags.pop();
                        }
                        tagTextBuffer = "";

                    } else {
                        //this is the closing bracket of an opening tag
                        String tagName = tagTextBuffer.split(" ")[0];
                        System.out.print("tag is " + tagName + "\n");
                        for (int i = 0; i < tags.length; i++) {

                            if (tags[i].compareTo(tagName) == 0) {
                                // this is a tag that is suposed to be styled in the pdf
                                if (chunkStyle != null) {
                                    //this tag is nested in a tag that was already applying styling. Add this chunk to the pdf and put the tag/style
                                    //for the previous tag on the stack, so when this new tag ends, the previous styling will resume.
                                    if (chunkStyle == styles.paragraph)
                                        chunkBuffer = new StringBuilder("\n" + chunkBuffer);
                                    Chunk c = new Chunk(chunkBuffer.toString());
                                    styleChunk(c, chunkStyle);
                                    if (chunkStyle != styles.remove)
                                        para.add(c);
                                    wrappingStyles.add(chunkStyle);
                                    wrappingTags.add(chunkTag);
                                    chunkTag = tagName;
                                    chunkStyle = tagStyles[i];
                                    chunkBuffer = new StringBuilder("");
                                } else {
                                    Chunk c = new Chunk(chunkBuffer.toString());
                                    para.add(c);
                                    chunkTag = tagName;
                                    chunkStyle = tagStyles[i];
                                    chunkBuffer = new StringBuilder("");
                                }
                            }
                        }
                        tagTextBuffer = "";
                    }
                }
            }
            if (inTag) {
                tagTextBuffer += text.charAt(charCounter);
            }
            if (text.charAt(charCounter) == '<') {
                if (inTag) {
                    //if we hit another < before hitting a > this was not a tag, so add the tagTextBuffer to the chunk. It was simply conent.
                    chunkBuffer.append(tagTextBuffer);
                    tagTextBuffer = "";
                }
                inTag = true;
            }
            if (!inTag && text.charAt(charCounter) != '>') {
                chunkBuffer.append(text.charAt(charCounter));
            }
        }
        Chunk c = new Chunk(chunkBuffer.toString());
        para.add(c);
        doc.newPage();
        doc.add(para);
        doc.newPage();
        doc.close();
    } catch (IOException ex) {
        Logger.getLogger(TagFilter.class.getName()).log(Level.SEVERE, null, ex);
    } finally {

    }

}

From source file:textdisplay.TagFilter.java

License:Educational Community License

/**Apply a style (font) to a chunk of pdf text*/
private void styleChunk(Chunk c, styles s) {
    try {/*from w  w  w.jav  a  2  s  .com*/

        BaseFont bf = BaseFont.createFont("/usr/Junicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont ita = BaseFont.createFont("/usr/Junicode-Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont bol = BaseFont.createFont("/usr/Junicode-Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        if (bf.charExists(540)) {
            System.out.print("font cant do 540\n");
        }
        Font italic = new Font(ita, 12, Font.ITALIC);
        Font bold = new Font(bol, 12, Font.BOLD);
        Font underlined = new Font(bf, 12, Font.UNDERLINE);
        Font superscript = new Font(bf, 9, Font.NORMAL);

        if (s == styles.bold) {
            c.setFont(bold);
        }
        if (s == styles.italic) {
            c.setFont(italic);
        }
        if (s == styles.underlined) {
            c.setFont(underlined);
        }
        if (s == styles.superscript) {
            c.setTextRise(7.0f);
            c.setFont(superscript);

        }

        //wipe out that content

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

From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java

License:Open Source License

/**
 * Adds a Barcode39 of the id number of a Volunteer to the Badge.
 *
 * @param content to be added//  w w  w . jav  a 2 s.  c o  m
 * @param id volunteer id
 * @throws DocumentException
 * @throws IOException
 */
private static void addBarcode(PdfContentByte contentByte, Integer id) throws IOException, DocumentException {
    Barcode39 barcode = new Barcode39();
    BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
    barcode.setFont(baseFont);
    barcode.setSize(6);
    barcode.setCode(id.toString());
    contentByte.setColorFill(Color.BLACK);
    Image baecodeImg = barcode.createImageWithBarcode(contentByte, null, null);
    contentByte.addImage(baecodeImg, 75, 0, 0, 35, 338, 344);
}