Example usage for com.itextpdf.text.pdf BaseFont createFont

List of usage examples for com.itextpdf.text.pdf BaseFont createFont

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf BaseFont createFont.

Prototype

public static BaseFont createFont(String name, String encoding, boolean embedded)
        throws DocumentException, IOException 

Source Link

Document

Creates a new font.

Usage

From source file:org.ganttproject.impex.htmlpdf.fonts.TTFontCache.java

License:Open Source License

private Function<String, BaseFont> createFontSupplier(final File fontFile, final boolean isEmbedded)
        throws DocumentException, IOException {
    try {/*from ww w  .j  av a  2 s. c  o m*/
        BaseFont.createFont(fontFile.getAbsolutePath(), GanttLanguage.getInstance().getCharSet(), isEmbedded);
    } catch (DocumentException e) {
        if (!e.getMessage().contains("is not recognized")
                || !e.getMessage().contains(GanttLanguage.getInstance().getCharSet())) {
            throw e;
        }
    } finally {
        BaseFontPublicMorozov.clearCache();
    }
    return new Function<String, BaseFont>() {
        @Override
        public BaseFont apply(String charset) {
            try {
                if (fontFile.getName().toLowerCase().endsWith(".ttc")) {
                    return BaseFont.createFont(fontFile.getAbsolutePath() + ",0", charset, isEmbedded);
                } else {
                    return BaseFont.createFont(fontFile.getAbsolutePath(), charset, isEmbedded);
                }
            } catch (DocumentException e) {
                GPLogger.log(e);
            } catch (IOException e) {
                GPLogger.log(e);
            }
            return null;
        }
    };
}

From source file:org.ganttproject.impex.htmlpdf.fonts.TTFontCache.java

License:Open Source License

protected BaseFont getFallbackFont(String charset) {
    if (myFallbackFont == null) {
        try {/*from  ww w . j ava 2  s  .  c  o  m*/
            myFallbackFont = BaseFont.createFont(
                    Platform.resolve(getClass().getResource(FALLBACK_FONT_PATH)).getPath(), charset,
                    BaseFont.EMBEDDED);
        } catch (DocumentException e) {
            GPLogger.logToLogger(e);
        } catch (IOException e) {
            GPLogger.logToLogger(e);
        }
    }
    return myFallbackFont;
}

From source file:org.h819.commons.file.MyPDFUtils.java

/**
 * ? iTextAsian.jar/* w  w w .  j  a v  a2s  .com*/
 * itext 5.5.9 ???
 *
 * @return
 */
@Deprecated
private static Font getChineseFont() {
    Font fontChinese = null;
    try {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        fontChinese = new Font(bfChinese, 12, Font.NORMAL);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    return fontChinese;
}

From source file:org.oscarehr.fax.util.PdfCoverPageCreator.java

License:Open Source License

public byte[] createCoverPage() {

    Document document = new Document();
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {/*from  ww w  .  ja  va 2  s  . c  om*/

        PdfWriter pdfWriter = PdfWriter.getInstance(document, os);
        document.open();

        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(95);

        PdfPCell cell = new PdfPCell(table);
        cell.setBorder(0);
        cell.setPadding(3);
        cell.setColspan(1);
        table.addCell(cell);

        ClinicDAO clinicDao = SpringUtils.getBean(ClinicDAO.class);
        Clinic clinic = clinicDao.getClinic();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        Font headerFont = new Font(bf, 28, Font.BOLD);
        Font infoFont = new Font(bf, 12, Font.NORMAL);

        if (clinic != null) {

            cell = new PdfPCell(new Phrase(
                    String.format("%s\n %s, %s, %s %s", clinic.getClinicName(), clinic.getClinicAddress(),
                            clinic.getClinicCity(), clinic.getClinicProvince(), clinic.getClinicPostal()),
                    headerFont));
        } else {

            cell = new PdfPCell(new Phrase("OSCAR", headerFont));

        }

        cell.setPaddingTop(100);
        cell.setPaddingLeft(25);
        cell.setPaddingBottom(25);
        cell.setBorderWidthBottom(1);
        table.addCell(cell);

        PdfPTable infoTable = new PdfPTable(1);
        cell = new PdfPCell(new Phrase(note, infoFont));
        cell.setPaddingTop(25);
        cell.setPaddingLeft(25);
        infoTable.addCell(cell);
        table.addCell(infoTable);

        document.add(table);

    } catch (DocumentException e) {

        MiscUtils.getLogger().error("PDF COVER PAGE ERROR", e);
        return new byte[] {};

    } catch (IOException e) {

        MiscUtils.getLogger().error("PDF COVER PAGE ERROR", e);
        return new byte[] {};

    } finally {
        document.close();
    }

    return os.toByteArray();
}

From source file:org.primaresearch.pdf.PageToPdfConverter.java

License:Apache License

/**
 * Creates the font that is to be used for the hidden text layer in the PDF.
 *///  w  w w.j a v a  2 s . co  m
private void createFont() throws DocumentException, IOException {

    //TODO Even with the 'NOT_EMBEDDED' settings it seems to embed the font!

    if (ttfFontFilePath == null)
        font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    else {
        font = FontFactory.getFont(ttfFontFilePath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED).getBaseFont();
        //PDTrueTypeFont.loadTTF(document, ttfFontFilePath);
        //Encoding enc = font.getFontEncoding();
        //Map<Integer, String> map = enc.getCodeToNameMap();
        //System.out.println("Font encoding map size: " + map.size());
    }
}

From source file:org.qnot.passtab.PDFOutput.java

License:Open Source License

public PDFOutput(boolean withColor) {
    this.withColor = withColor;

    try {/*from  w  ww . j  av  a  2 s  .  c om*/
        font = new Font(BaseFont.createFont(PDFOutput.FONT_MONOSPACE, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
        fontBold = new Font(
                BaseFont.createFont(PDFOutput.FONT_MONOSPACE_BOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
    } catch (Exception e) {
        try {
            font = new Font(BaseFont.createFont());
            fontBold = new Font(BaseFont.createFont());
        } catch (Exception ignored) {
        }
    }

    font.setSize(DEFAULT_FONT_SIZE);
    fontBold.setSize(DEFAULT_FONT_SIZE);
}

From source file:org.tvd.thptty.management.report.Report.java

private BaseFont getTahomaBaseFont() {
    BaseFont tahoma = null;// ww  w.  ja v  a2 s.  com
    try {
        tahoma = BaseFont.createFont(FontPath.TAHOMA_FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tahoma;
}

From source file:pdf.alterLetter.java

public static void main(String args[]) {
    try {// w  w w  .  j  a v a 2  s .  c  o  m
        PdfReader pdfReader;
        pdfReader = new PdfReader("C:\\Users\\asus\\Desktop\\web\\Appointment letter.pdf");
        //pdfReader = new PdfReader("C:\\Users\\asus\\Desktop\\TFMsystem\\Appointment letter.pdf");   

        //Create PdfStamper instance.
        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(
                "C:\\Users\\asus\\Desktop\\TFMsystem\\web\\Modified appointment letter.pdf"));

        //new FileOutputStream("C:\\Users\\asus\\Desktop\\TFMsystem\\Modified appointment letter.pdf"));

        //Create BaseFont instance.
        BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED);

        //Get the number of pages in pdf.
        int pages = pdfReader.getNumberOfPages();

        //Iterate the pdf through pages.
        for (int i = 1; i <= pages; i++) {
            //Contain the pdf data.
            PdfContentByte pageContentByte = pdfStamper.getOverContent(i);

            pageContentByte.beginText();
            //Set text font and size.
            pageContentByte.setFontAndSize(baseFont, 12);

            //Write text
            pageContentByte.setTextMatrix(120, 706);
            pageContentByte.showText("[no rujukan(enter by admin/opai)]");

            pageContentByte.setTextMatrix(500, 706);
            pageContentByte.showText("[current date]");
            //address
            pageContentByte.setTextMatrix(46, 641);
            pageContentByte.showText("[name]");
            pageContentByte.setTextMatrix(46, 629);
            pageContentByte.showText("[position]");
            pageContentByte.setTextMatrix(46, 617);
            pageContentByte.showText("[department]");

            pageContentByte.setTextMatrix(155, 493);
            pageContentByte.showText("[status(penyelaras/ahli),taskforce name]");

            pageContentByte.setTextMatrix(178, 433);
            pageContentByte.showText("[start date]");

            pageContentByte.setTextMatrix(290, 433);
            pageContentByte.showText("[end date] .");

            pageContentByte.setTextMatrix(46, 248);
            pageContentByte.showText("[name]");
            pageContentByte.setTextMatrix(46, 236);
            pageContentByte.showText("[post]");
            pageContentByte.setTextMatrix(46, 224);
            pageContentByte.showText("[faculty]");
            pageContentByte.setTextMatrix(46, 212);
            pageContentByte.showText("[email]");

            pageContentByte.endText();
        }

        //Close the pdfStamper.
        pdfStamper.close();

        System.out.println("PDF modified successfully.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:pdf.letter.java

public boolean AlterLetter(String rujukan, String name, String position, String department, String gStatus,
        String sDate, String eDate, String taskName, String postHolderName, String postHolderEmail,
        String postName) {/* ww  w .  j a  v a 2s  . c  o  m*/
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    Date today = Calendar.getInstance().getTime();
    String currDate = df.format(today);
    try {
        PdfReader pdfReader;
        pdfReader = new PdfReader("C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf");
        //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf

        //Create PdfStamper instance.
        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(
                "C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified appointment letter.pdf"));
        //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified Appointment letter.pdf

        //Create BaseFont instance.
        BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED);

        //Get the number of pages in pdf.
        int pages = pdfReader.getNumberOfPages();

        //Iterate the pdf through pages.
        for (int i = 1; i <= pages; i++) {
            //Contain the pdf data.
            PdfContentByte pageContentByte = pdfStamper.getOverContent(i);

            pageContentByte.beginText();
            //Set text font and size.
            pageContentByte.setFontAndSize(baseFont, 11);

            //Write text
            pageContentByte.setTextMatrix(120, 706);
            pageContentByte.showText(rujukan);

            pageContentByte.setTextMatrix(500, 706);
            pageContentByte.showText(currDate);
            //address
            pageContentByte.setTextMatrix(46, 641);
            pageContentByte.showText(name);
            pageContentByte.setTextMatrix(46, 629);
            pageContentByte.showText(position);
            pageContentByte.setTextMatrix(46, 617);
            pageContentByte.showText(department);

            String gstatus;

            pageContentByte.setTextMatrix(157, 493);
            String changeCase = gStatus + ", " + taskName;
            pageContentByte.showText(changeCase.toUpperCase());

            pageContentByte.setTextMatrix(250, 444);
            pageContentByte.showText(gStatus + "  " + taskName + " .");

            pageContentByte.setTextMatrix(180, 432);
            pageContentByte.showText(sDate);

            pageContentByte.setTextMatrix(290, 432);
            pageContentByte.showText(eDate + " .");

            pageContentByte.setTextMatrix(46, 248);
            pageContentByte.showText(postHolderName);
            pageContentByte.setTextMatrix(46, 236);
            pageContentByte.showText(postName);
            pageContentByte.setTextMatrix(46, 224);
            pageContentByte.showText("Fakulti Komputeran");
            pageContentByte.setTextMatrix(46, 212);
            pageContentByte.showText(postHolderEmail);

            pageContentByte.endText();
        }
        //Close the pdfStamper.
        pdfStamper.close();
        System.out.println("PDF modified successfully.");

        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

From source file:pdf.PDFDesign.java

public PDFDesign(String s, String c, String sn, Map<String, String> fontMap) {
    size = s;/*from   www  .ja  v  a2s  .  co  m*/
    titleLineFixedHeight = 15.0f;
    priceFontInc = 0;
    textFontInc = 0;
    styleName = sn;
    fontPaths = new FontPaths(fontMap);
    nColumns = 6;
    switch (c) {
    case "black":
        color = BaseColor.BLACK;
        break;
    case "gray":
        color = BaseColor.DARK_GRAY;
        break;
    case "red":
        color = BaseColor.RED.darker();
        break;
    case "green":
        color = BaseColor.GREEN.darker().darker().darker();
        break;
    case "blue":
        color = BaseColor.BLUE.darker().darker().darker();
        break;
    default:
        this.color = BaseColor.BLACK;
    }
    switch (size) {
    case "3x5":
        cellHeight = 83.8f;
        break;
    case "3.5x5":
        cellHeight = 97.8f;
        break;
    case "3.5x6":
        priceFontInc = 8;
        cellHeight = 97.8f;
        nColumns = 5;
        break;
    case "3.5x7":
        cellHeight = 97.8f;
        nColumns = 4;
        break;
    case "4x6":
        titleLineFixedHeight = 26.0f;
        priceFontInc = 8;
        cellHeight = 117.0f;
        nColumns = 5;
        break;
    case "4x7":
        titleLineFixedHeight = 26.0f;
        priceFontInc = 8;
        cellHeight = 117.0f;
        nColumns = 4;
        break;
    case "5x6":
        titleLineFixedHeight = 30.0f;
        priceFontInc = 13;
        textFontInc = 2;
        cellHeight = 145.0f;
        nColumns = 5;
        break;
    case "5x7":
        titleLineFixedHeight = 30.0f;
        priceFontInc = 18;
        textFontInc = 3;
        cellHeight = 145.0f;
        nColumns = 4;
        break;
    case "6x7":
        titleLineFixedHeight = 34.0f;
        priceFontInc = 26;
        textFontInc = 5;
        cellHeight = 175.0f;
        nColumns = 4;
        break;
    }
    try {
        int normal = Font.NORMAL;
        int bold = Font.BOLD;

        droidsans = BaseFont.createFont(fontPaths.getPath("droidsans"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont impact = BaseFont.createFont(fontPaths.getPath("impact"), BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);

        switch (styleName) {
        case "style1":
            BaseFont courier = BaseFont.createFont(fontPaths.getPath("courier"), BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            initValues(42f, new Font(courier, 12 + textFontInc, normal, color),
                    new Font(courier, 9 + textFontInc, normal, color),
                    new Font(impact, 43 + priceFontInc + textFontInc, normal, color),
                    new Font(courier, 20 + textFontInc, normal, color),
                    new Font(courier, 8 + textFontInc, normal, color));
            break;
        case "style2":
            BaseFont digital7 = BaseFont.createFont(fontPaths.getPath("digital7"), BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            initValues(42f, new Font(droidsans, 10 + textFontInc, normal, color),
                    new Font(droidsans, 7 + textFontInc, normal, color),
                    new Font(digital7, 55 + priceFontInc + textFontInc, normal, color),
                    new Font(digital7, 30, normal, color), new Font(droidsans, 7 + textFontInc, normal, color));
            break;
        case "style3":
            BaseFont modern = BaseFont.createFont(fontPaths.getPath("modern"), BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            initValues(40f, new Font(droidsans, 10 + textFontInc, normal, color),
                    new Font(droidsans, 7 + textFontInc, normal, color),
                    new Font(modern, 55 + priceFontInc + textFontInc, normal, color),
                    new Font(modern, 30, normal, color), new Font(droidsans, 7 + textFontInc, normal, color));
            break;
        case "style4":
            BaseFont gothic = BaseFont.createFont(fontPaths.getPath("gothic"), BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            initValues(40f, new Font(droidsans, 10 + textFontInc, normal, color),
                    new Font(droidsans, 7 + textFontInc, normal, color),
                    new Font(gothic, 48 + priceFontInc + textFontInc, bold, color),
                    new Font(gothic, 30, bold, color), new Font(droidsans, 7 + textFontInc, normal, color));
            break;
        case "style5":
            BaseFont bookman = BaseFont.createFont(fontPaths.getPath("bookman"), BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
            initValues(35f, new Font(droidsans, 10 + textFontInc, normal, color),
                    new Font(droidsans, 7 + textFontInc, normal, color),
                    new Font(bookman, 45 + priceFontInc + textFontInc, bold, color),
                    new Font(bookman, 25, normal, color), new Font(droidsans, 7 + textFontInc, normal, color));
            break;

        case "style6":
            initValues(45f, new Font(droidsans, 10 + textFontInc, normal, color),
                    new Font(droidsans, 7 + textFontInc, normal, color),
                    new Font(impact, 45 + priceFontInc + textFontInc, normal, color),
                    new Font(droidsans, 20 + textFontInc, normal, color),
                    new Font(droidsans, 7 + textFontInc, normal, color));
            break;

        default:
            System.out.println("[*] Unknown Style");
            break;
        }

    } catch (DocumentException | IOException ex) {
        System.out.println("[E] " + ex.getMessage());
    }
}