Example usage for com.itextpdf.text FontFactory register

List of usage examples for com.itextpdf.text FontFactory register

Introduction

In this page you can find the example usage for com.itextpdf.text FontFactory register.

Prototype


public static void register(final String path) 

Source Link

Document

Register a ttf- or a ttc-file.

Usage

From source file:com.ideationdesignservices.txtbook.pdf.TxtBookPdf.java

public void createPDF(Context context) throws DocumentException, IOException {
    LicenseKey.loadLicenseFile(context.getAssets().open("itextkey.xml"));
    this.mContext = context;
    Map<String, String> pdfParams = new HashMap();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_Hmmss", Locale.US);
    dateFormat.setTimeZone(TimeZone.getDefault());
    this.filename = "txtbook_" + dateFormat.format(new Date()) + ".pdf";
    float leftMargin = Txtbook.RIGHT_MARGIN_EVEN;
    float rightMargin = Txtbook.RIGHT_MARGIN_ODD;
    if (this.settings.addFrontCover.booleanValue()) {
        leftMargin = Txtbook.RIGHT_MARGIN_ODD;
        rightMargin = Txtbook.RIGHT_MARGIN_EVEN;
    }/*from w  ww  . jav  a2 s.  com*/
    Document document = new Document(PageSize.LETTER, leftMargin, rightMargin, Txtbook.TOP_MARGIN,
            Txtbook.TOP_MARGIN);
    File file = new File(Environment.getExternalStorageDirectory(), this.filename);
    if (!(file.exists() && file.canRead())) {
        file.createNewFile();
    }
    this.writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    this.writer.setCompressionLevel(this.settings.compressionLevel);
    this.writer.setStrictImageSequence(true);
    FontFactory.register("assets/fonts/DroidSans.ttf");
    this.sansFont6Gray = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 6.0f, 0,
            new BaseColor(152, 152, 152));
    this.sansFont9 = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 9.0f);
    this.sansFont9Gray = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 9.0f, 0,
            new BaseColor(103, 103, 103));
    this.sansFont11Gray = FontFactory.getFont("DroidSans", BaseFont.WINANSI, true, 11.0f, 0,
            new BaseColor(152, 152, 152));
    FontFactory.register("assets/fonts/CourierNew.ttf");
    this.serifFont8Gray = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 8.0f, 0,
            new BaseColor(103, 103, 103));
    this.serifFont11 = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 11.0f);
    this.serifFont14 = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 14.0f);
    this.serifFont24 = FontFactory.getFont("Courier New", BaseFont.WINANSI, true, 24.0f);
    document.open();
    document.setMarginMirroring(true);
    document.addCreationDate();
    document.addCreator("Created with txt-book for Android www.txt-book.com");
    document.addTitle("txt-book for Android");
    if (this.settings.bookStyle == 1) {
        ConversationHeaderFooter hf = new ConversationHeaderFooter();
        hf.footerFont = this.sansFont9;
        hf.hasFrontCover = this.settings.addFrontCover;
        this.writer.setPageEvent(hf);
    } else {
        StorybookHeaderFooter hf2 = new StorybookHeaderFooter();
        hf2.title = this.settings.bookCoverTitle;
        hf2.headerFont = this.serifFont8Gray;
        hf2.hasFrontCover = this.settings.addFrontCover;
        this.writer.setPageEvent(hf2);
    }
    if (this.settings.addFrontCover.booleanValue()) {
        createFrontCoverPage(document, this.settings.bookCoverTitle,
                this.settings.addFrontCoverImage.booleanValue() ? this.settings.coverPhoto : null);
        pdfParams.put("PDF Front Cover", "YES");
    } else {
        pdfParams.put("PDF Front Cover", "NO");
    }
    if (this.settings.addFrontCoverImage.booleanValue()) {
        pdfParams.put("PDF Front Cover Image", "YES");
    } else {
        pdfParams.put("PDF Front Cover Image", "NO");
    }
    if (this.settings.bookStyle == 1) {
        createContentPagesConversation(document);
    } else {
        createContentPagesStorybook(document, this.settings.bookCoverTitle);
    }
    int pages = this.writer.getPageNumber();
    if (this.settings.bookStyle == 2) {
        ((StorybookHeaderFooter) this.writer.getPageEvent()).reachedEndOfContent = Boolean.valueOf(true);
    } else {
        ((ConversationHeaderFooter) this.writer.getPageEvent()).reachedEndOfContent = Boolean.valueOf(true);
    }
    if (this.settings.addBackCover.booleanValue()) {
        pages++;
        pdfParams.put("PDF Back Cover", "YES");
        if (pages % 2 == 1) {
            createBlankSpacerPage(document);
        }
        createBackCoverPage(document, this.settings.backCoverNote);
    } else {
        pdfParams.put("PDF Back Cover", "NO");
        if (pages % 2 == 1) {
            createBlankSpacerPage(document);
        }
    }
    if (this.settings.bookStyle == 2) {
        pdfParams.put("PDF Style", "Storybook");
    } else {
        pdfParams.put("PDF Style", "Conversation");
    }
    pdfParams.put("PDF Num Pages", Integer.valueOf(this.writer.getPageNumber()).toString());
    document.close();
    FlurryAgent.logEvent("PDF_CREATED", (Map) pdfParams);
}

From source file:com.netsteadfast.greenstep.bsc.command.KpiReportPdfCommand.java

License:Apache License

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();/*from w w w. j a  v  a  2 s.  c  o m*/
    BscReportSupportUtils.loadExpression(); // 2015-04-18 add
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    //Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    Document document = new Document(PageSize.A4, 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();

    int dateRangeRows = 4 + vision.getPerspectives().get(0).getObjectives().get(0).getKpis().get(0)
            .getDateRangeScores().size();
    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    PdfPTable dateRangeTable = new PdfPTable(dateRangeRows);
    PdfPTable chartsTable = new PdfPTable(2);
    PdfPTable signTable = new PdfPTable(1);
    table.setWidthPercentage(100f);
    dateRangeTable.setWidthPercentage(100f);
    chartsTable.setWidthPercentage(100f);
    signTable.setWidthPercentage(100f);

    this.createHead(table, vision);
    this.createBody(table, vision);
    this.createDateRange(dateRangeTable, vision, context, dateRangeRows);
    this.putCharts(chartsTable, context);
    this.putSignature(signTable, context);

    document.add(chartsTable);
    document.add(table);
    document.add(dateRangeTable);
    document.add(signTable);
    document.close();
    writer.close();

    os.flush();
    os.close();
    os = null;

    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file,
            "kpi-report.pdf");
    file = null;
    return oid;
}

From source file:com.netsteadfast.greenstep.bsc.command.OrganizationReportPdfCommand.java

License:Apache License

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();//from w  ww  .ja  v a  2  s  .co  m
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();

    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    table.setWidthPercentage(100f);
    PdfPTable signTable = new PdfPTable(1);
    signTable.setWidthPercentage(100f);

    this.createHead(table, vision, context);
    this.createBody(table, vision);

    this.putSignature(signTable, context);

    document.add(table);
    document.add(signTable);
    document.close();
    writer.close();

    os.flush();
    os.close();
    os = null;

    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file,
            "department-report.pdf");
    file = null;
    return oid;
}

From source file:com.netsteadfast.greenstep.bsc.command.PersonalReportPdfCommand.java

License:Apache License

private String createPdf(Context context) throws Exception {
    BscReportPropertyUtils.loadData();//  w w  w .  j  av  a2  s.  c  o  m
    String visionOid = (String) context.get("visionOid");
    VisionVO vision = null;
    BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context);
    for (VisionVO visionObj : treeObj.getVisions()) {
        if (visionObj.getOid().equals(visionOid)) {
            vision = visionObj;
        }
    }
    FontFactory.register(BscConstants.PDF_ITEXT_FONT);
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
    OutputStream os = new FileOutputStream(fileFullPath);
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    document.left(100f);
    document.top(150f);
    PdfWriter writer = PdfWriter.getInstance(document, os);
    document.open();

    PdfPTable table = new PdfPTable(MAX_COLSPAN);
    table.setWidthPercentage(100f);
    PdfPTable signTable = new PdfPTable(1);
    signTable.setWidthPercentage(100f);

    this.createHead(table, vision, context);
    this.createBody(table, vision);
    this.createFoot(table, context);
    this.putSignature(signTable, context);

    document.add(table);
    document.add(signTable);
    document.close();
    writer.close();

    os.flush();
    os.close();
    os = null;

    File file = new File(fileFullPath);
    String oid = UploadSupportUtils.create(Constants.getSystem(), UploadTypes.IS_TEMP, false, file,
            "personal-report.pdf");
    file = null;
    return oid;
}

From source file:com.vectorprint.report.itext.style.FontLoader.java

License:Open Source License

/**
 * Bottleneck method for loading fonts, calls {@link FontFactory#register(java.lang.String) } for iText, {@link GraphicsEnvironment#registerFont(java.awt.Font) }
 * for awt.//from ww w.ja v a2  s  .  c  o  m
 * @param path the path to the font file 
ed
 * @return
 * @throws VectorPrintException 
 */
public LOADSTATUS loadFont(String path) throws VectorPrintException {
    try {
        File f = new File(path);
        LOADSTATUS stat = LOADSTATUS.NOT_LOADED;

        if (loadAWTFonts) {
            Font fo = Font.createFont(Font.TRUETYPE_FONT, f);
            GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fo);
            stat = LOADSTATUS.LOADED_ONLY_AWT;
        }

        if (loadiText) {
            FontFactory.register(path);
            stat = (stat.equals(LOADSTATUS.LOADED_ONLY_AWT)) ? LOADSTATUS.LOADED_ITEXT_AND_AWT
                    : LOADSTATUS.LOADED_ONLY_ITEXT;
            log.info(String.format("font loaded from %s", f.getAbsolutePath()));
        }

        return stat;
    } catch (FontFormatException | IOException ex) {
        log.log(Level.SEVERE, null, ex);

        throw new VectorPrintException("failed to load " + path, ex);
    }
}

From source file:org.alfresco.repo.content.transform.ITextPDFWorker.java

License:Apache License

/**
 * Obtains the base arial unicode font, contained in the war.
 *
 * @return the created and registered base font
 */// w ww .  j a v  a  2 s. c o m
private static BaseFont getBaseFont() {
    try {
        InputStream resourceAsStream = ITextPDFWorker.class.getResourceAsStream(ARIAL_TTF);
        File tempDir = TempFileProvider.getSystemTempDir();
        File tempFontFile = new File(tempDir, ARIAL_TTF);
        if (!tempFontFile.exists() || !tempFontFile.canRead()) {
            FileOutputStream fileOutputStream = new FileOutputStream(tempFontFile.getAbsoluteFile());
            IOUtils.copy(resourceAsStream, fileOutputStream);
            IOUtils.closeQuietly(fileOutputStream);
        }
        FontFactory.register(tempFontFile.getAbsolutePath());
        return BaseFont.createFont(tempFontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            return BaseFont.createFont();
        } catch (Exception e1) {
            // shouldn't occur ever
            e1.printStackTrace();
        }
    }
    return null;
}