Example usage for com.itextpdf.text Font getCalculatedBaseFont

List of usage examples for com.itextpdf.text Font getCalculatedBaseFont

Introduction

In this page you can find the example usage for com.itextpdf.text Font getCalculatedBaseFont.

Prototype

public BaseFont getCalculatedBaseFont(final boolean specialEncoding) 

Source Link

Document

Gets the BaseFont this class represents.

Usage

From source file:com.dandymadeproductions.ajqvue.io.PDFDataTableDumpThread.java

License:Open Source License

public void run() {
    // Class Method Instances
    String title;/*from www .  j  av a  2s  . c  o m*/

    Font titleFont;
    Font rowHeaderFont;
    Font tableDataFont;
    BaseFont rowHeaderBaseFont;

    PdfPTable pdfTable;
    PdfPCell titleCell;
    PdfPCell rowHeaderCell;
    PdfPCell bodyCell;

    Document pdfDocument;
    PdfWriter pdfWriter;
    ByteArrayOutputStream byteArrayOutputStream;

    int columnCount, rowNumber;
    int[] columnWidths;
    int totalWidth;
    Rectangle pageSize;

    ProgressBar dumpProgressBar;
    HashMap<String, String> summaryListTableNameTypes;
    DataExportProperties pdfDataExportOptions;

    String currentTableFieldName;
    String currentType, currentString;

    // Setup
    columnCount = summaryListTable.getColumnCount();
    rowNumber = summaryListTable.getRowCount();
    columnWidths = new int[columnCount];

    pdfTable = new PdfPTable(columnCount);
    pdfTable.setWidthPercentage(100);
    pdfTable.getDefaultCell().setPaddingBottom(4);
    pdfTable.getDefaultCell().setBorderWidth(1);

    summaryListTableNameTypes = new HashMap<String, String>();
    pdfDataExportOptions = DBTablesPanel.getDataExportProperties();

    titleFont = new Font(pdfDataExportOptions.getFont());
    titleFont.setStyle(Font.BOLD);
    titleFont.setSize((float) pdfDataExportOptions.getTitleFontSize());
    titleFont.setColor(new BaseColor(pdfDataExportOptions.getTitleColor().getRGB()));

    rowHeaderFont = new Font(pdfDataExportOptions.getFont());
    rowHeaderFont.setStyle(Font.BOLD);
    rowHeaderFont.setSize((float) pdfDataExportOptions.getHeaderFontSize());
    rowHeaderFont.setColor(new BaseColor(pdfDataExportOptions.getHeaderColor().getRGB()));
    rowHeaderBaseFont = rowHeaderFont.getCalculatedBaseFont(false);

    tableDataFont = pdfDataExportOptions.getFont();

    // Constructing progress bar.
    dumpProgressBar = new ProgressBar(exportedTable + " Dump");
    dumpProgressBar.setTaskLength(rowNumber);
    dumpProgressBar.pack();
    dumpProgressBar.center();
    dumpProgressBar.setVisible(true);

    // Create a Title if Optioned.
    title = pdfDataExportOptions.getTitle();

    if (!title.equals("")) {
        if (title.equals("EXPORTED TABLE"))
            title = exportedTable;

        titleCell = new PdfPCell(new Phrase(title, titleFont));
        titleCell.setBorder(0);
        titleCell.setPadding(10);
        titleCell.setColspan(summaryListTable.getColumnCount());
        titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

        pdfTable.addCell(titleCell);
        pdfTable.setHeaderRows(2);
    } else
        pdfTable.setHeaderRows(1);

    // Create Row Header.
    for (int i = 0; i < columnCount; i++) {
        currentTableFieldName = summaryListTable.getColumnName(i);
        rowHeaderCell = new PdfPCell(new Phrase(currentTableFieldName, rowHeaderFont));
        rowHeaderCell.setBorderWidth(pdfDataExportOptions.getHeaderBorderSize());
        rowHeaderCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        rowHeaderCell.setBorderColor(new BaseColor(pdfDataExportOptions.getHeaderBorderColor().getRGB()));
        pdfTable.addCell(rowHeaderCell);
        columnWidths[i] = Math.min(50000,
                Math.max(columnWidths[i], rowHeaderBaseFont.getWidth(currentTableFieldName + " ")));
        if (tableColumnTypeNameHashMap != null)
            summaryListTableNameTypes.put(Integer.toString(i),
                    tableColumnTypeNameHashMap.get(currentTableFieldName));
        else
            summaryListTableNameTypes.put(Integer.toString(i), "String");
    }

    // Create the Body of Data.
    int i = 0;
    while ((i < rowNumber) && !dumpProgressBar.isCanceled()) {
        dumpProgressBar.setCurrentValue(i);

        // Collecting rows of data & formatting date & timestamps
        // as needed according to the Export Properties.

        if (summaryListTable.getValueAt(i, 0) != null) {
            for (int j = 0; j < summaryListTable.getColumnCount(); j++) {
                currentString = summaryListTable.getValueAt(i, j) + "";
                currentString = currentString.replaceAll("\n", "");
                currentString = currentString.replaceAll("\r", "");
                currentType = summaryListTableNameTypes.get(Integer.toString(j));

                // Format Date & Timestamp Fields as Needed.

                if ((currentType != null) && (currentType.equals("DATE") || currentType.equals("DATETIME")
                        || currentType.indexOf("TIMESTAMP") != -1)) {
                    if (!currentString.toLowerCase(Locale.ENGLISH).equals("null")) {
                        int firstSpace;
                        String time;

                        // Dates fall through DateTime and Timestamps try
                        // to get the time separated before formatting
                        // the date.

                        if (currentString.indexOf(" ") != -1) {
                            firstSpace = currentString.indexOf(" ");
                            time = currentString.substring(firstSpace);
                            currentString = currentString.substring(0, firstSpace);
                        } else
                            time = "";

                        currentString = Utils.convertViewDateString_To_DBDateString(currentString,
                                DBTablesPanel.getGeneralDBProperties().getViewDateFormat());
                        currentString = Utils.convertDBDateString_To_ViewDateString(currentString,
                                pdfDataExportOptions.getPDFDateFormat()) + time;
                    }
                }
                bodyCell = new PdfPCell(new Phrase(currentString, tableDataFont));
                bodyCell.setPaddingBottom(4);

                if (currentType != null) {
                    // Set Numeric Fields Alignment.
                    if (currentType.indexOf("BIT") != -1 || currentType.indexOf("BOOL") != -1
                            || currentType.indexOf("NUM") != -1 || currentType.indexOf("INT") != -1
                            || currentType.indexOf("FLOAT") != -1 || currentType.indexOf("DOUBLE") != -1
                            || currentType.equals("REAL") || currentType.equals("DECIMAL")
                            || currentType.indexOf("COUNTER") != -1 || currentType.equals("BYTE")
                            || currentType.equals("CURRENCY")) {
                        bodyCell.setHorizontalAlignment(pdfDataExportOptions.getNumberAlignment());
                        bodyCell.setPaddingRight(4);
                    }
                    // Set Date/Time Field Alignment.
                    if (currentType.indexOf("DATE") != -1 || currentType.indexOf("TIME") != -1
                            || currentType.indexOf("YEAR") != -1)
                        bodyCell.setHorizontalAlignment(pdfDataExportOptions.getDateAlignment());
                }

                pdfTable.addCell(bodyCell);
                columnWidths[j] = Math.min(50000,
                        Math.max(columnWidths[j], BASE_FONT.getWidth(currentString + " ")));
            }
        }
        i++;
    }
    dumpProgressBar.dispose();

    // Check to see if any data was in the summary
    // table to even be saved.

    if (pdfTable.size() <= pdfTable.getHeaderRows())
        return;

    // Create a document of the PDF formatted data
    // to be saved to the given output file.

    try {
        // Sizing & Layout
        totalWidth = 0;
        for (int width : columnWidths)
            totalWidth += width;

        if (pdfDataExportOptions.getPageLayout() == PDFExportPreferencesPanel.LAYOUT_PORTRAIT)
            pageSize = PageSize.A4;
        else {
            pageSize = PageSize.A4.rotate();
            pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f));
            pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f));
        }

        pdfTable.setWidths(columnWidths);

        // Document
        pdfDocument = new Document(pageSize);
        byteArrayOutputStream = new ByteArrayOutputStream();
        pdfWriter = PdfWriter.getInstance(pdfDocument, byteArrayOutputStream);
        pdfDocument.open();
        pdfTemplate = pdfWriter.getDirectContent().createTemplate(100, 100);
        pdfTemplate.setBoundingBox(new com.itextpdf.text.Rectangle(-20, -20, 100, 100));
        pdfWriter.setPageEvent(this);
        pdfDocument.add(pdfTable);
        pdfDocument.close();

        // Outputting
        WriteDataFile.mainWriteDataString(fileName, byteArrayOutputStream.toByteArray(), false);

    } catch (DocumentException de) {
        if (Ajqvue.getDebug()) {
            System.out.println("Failed to Create Document Needed to Output Data. \n" + de.toString());
        }
    }
}

From source file:org.cejug.yougi.web.report.EventAttendeeCertificate.java

License:Open Source License

public void generateCertificate(Attendee attendee) throws DocumentException {

    Font helvetica = new Font(Font.FontFamily.HELVETICA, 12);
    BaseFont baseFont = helvetica.getCalculatedBaseFont(false);
    canvas.saveState();//from  w w w  . j  a v a 2 s.c o m
    canvas.beginText();
    canvas.setFontAndSize(baseFont, 12);
    canvas.showTextAligned(Element.ALIGN_LEFT, "Validation code: " + attendee.getCertificateCode()
            + " ( http://www.cejug.org/certificate_validation.xhtml) ", 30, 30, 0);
    canvas.endText();
    canvas.restoreState();

    String[] contentLine = new String[8];
    contentLine[0] = "Certificamos que";
    contentLine[1] = attendee.getCertificateFullname();
    contentLine[2] = "participou do evento";
    contentLine[3] = attendee.getCertificateEvent();
    contentLine[4] = "realizado na";
    contentLine[5] = attendee.getCertificateVenue();
    contentLine[6] = "no dia "
            + TextUtils.INSTANCE.getFormattedDate(attendee.getCertificateDate(), "dd.MM.yyyy");

    Font normalFont = new Font(Font.FontFamily.HELVETICA, 24);
    Font largeFont = new Font(Font.FontFamily.HELVETICA, 28, Font.FontStyle.BOLD.ordinal());
    Paragraph p;

    for (int i = 0; i < 5; i++) {
        p = new Paragraph(" ", normalFont);
        this.document.add(p);
    }

    Font currentFont = normalFont;
    for (int i = 0; i < contentLine.length; i++) {
        p = new Paragraph(contentLine[i], currentFont);
        p.setAlignment(Element.ALIGN_CENTER);
        this.document.add(p);

        currentFont = currentFont.equals(normalFont) ? largeFont : normalFont;
    }
}

From source file:org.javad.pdf.util.PdfUtil.java

License:Apache License

protected static void improveRenderText(PdfContentByte content, String str, Font f, float x, float y) {

    BaseFont bf = f.getCalculatedBaseFont(false);
    if (i18Font == null && (System.currentTimeMillis() - LAST_CHECK_TIME > 10000)) {
        i18Font = FontRegistry.getInstance().getFont(PdfFontDefinition.ExtendedCharacters)
                .getCalculatedBaseFont(false);
        LAST_CHECK_TIME = System.currentTimeMillis();
    }/*from   w ww. j a  v  a 2  s  . c  om*/
    StringBuilder buf = new StringBuilder(str.length());
    boolean extendedCodePoint = false;
    float effWidth = 0.0f;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        int codePoint = Character.codePointAt("" + c, 0);
        boolean curCodePoint = (!bf.charExists(codePoint)) ? true : false;
        if (curCodePoint != extendedCodePoint) {
            content.setFontAndSize((extendedCodePoint) ? i18Font : bf, f.getSize());
            effWidth += content.getEffectiveStringWidth(buf.toString(), false);
            buf = new StringBuilder();
            extendedCodePoint = curCodePoint;
        }
        buf.append(c);
    }
    content.setFontAndSize(bf, f.getSize());
    effWidth += content.getEffectiveStringWidth(buf.toString(), false);
    float x_pos = x - (effWidth / 2.0f); // eq. to showTextAligned

    content.beginText();
    content.setTextMatrix(x_pos, y);
    BaseFont lastFont = bf;
    @SuppressWarnings("UnusedAssignment")
    BaseFont currentFont = lastFont;
    buf = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        int codePoint = Character.codePointAt("" + c, 0);
        if (!bf.charExists(codePoint)) {
            currentFont = i18Font;
        } else {
            currentFont = bf;
        }
        if (currentFont != lastFont) {
            content.showText(buf.toString());
            buf = new StringBuilder();
            content.setFontAndSize(currentFont, f.getSize());
            lastFont = currentFont;
        }
        buf.append(c);
    }
    if (buf.length() > 0) {
        content.showText(buf.toString());
    }
    content.endText();

}

From source file:org.yougi.web.report.EventAttendeeCertificate.java

License:Open Source License

public void generateCertificate(Attendee attendee) throws DocumentException {

    Font helvetica = new Font(Font.FontFamily.HELVETICA, 12);
    BaseFont baseFont = helvetica.getCalculatedBaseFont(false);
    canvas.saveState();//from w w  w  .j a v  a 2s .c  om
    canvas.beginText();
    canvas.setFontAndSize(baseFont, 12);
    canvas.showTextAligned(Element.ALIGN_LEFT, "Validation code: " + attendee.getCertificateCode()
            + " ( http://www.cejug.net/certificate_validation.xhtml) ", 30, 30, 0);
    canvas.endText();
    canvas.restoreState();

    String[] contentLine = new String[8];
    contentLine[0] = "Certificamos que";
    contentLine[1] = attendee.getCertificateFullname();
    contentLine[2] = "participou do evento";
    contentLine[3] = attendee.getCertificateEvent();
    contentLine[4] = "realizado na";
    contentLine[5] = attendee.getCertificateVenue();
    contentLine[6] = "no dia " + DateTimeUtils.getFormattedDate(attendee.getCertificateDate(), "dd.MM.yyyy");

    Font normalFont = new Font(Font.FontFamily.HELVETICA, 24);
    Font largeFont = new Font(Font.FontFamily.HELVETICA, 28, Font.FontStyle.BOLD.ordinal());
    Paragraph p;

    for (int i = 0; i < 5; i++) {
        p = new Paragraph(" ", normalFont);
        this.document.add(p);
    }

    Font currentFont = normalFont;
    for (int i = 0; i < contentLine.length; i++) {
        p = new Paragraph(contentLine[i], currentFont);
        p.setAlignment(Element.ALIGN_CENTER);
        this.document.add(p);

        currentFont = currentFont.equals(normalFont) ? largeFont : normalFont;
    }
}