无法使用Java打印与Unicode数学相关的字符-Itextpdf

时间:2018-11-29 20:06:26

标签: java unicode itext pdf-generation

我正在将iText与Java一起使用,我想打印大于或等于(≥),但是当您检查pdf时它将打印空白。字体:

public static final String FONT_A1L = "/opt/efact/ose/pdf/v1.0.0/conf/resources/fonts/ARIALUNI.TTF";

输入是包含符号的字符串:

String text = "Item (≥110)";

String text = "Item (\u2265 110)";

它已正确存储在包含符号≥的变量中,因此在pdf中打印时会出现问题。我正在使用PdfTable:

PdfPTable documentDetail = new PdfPTable(1);
        documentDetail.setWidths(new float[] {800});
        documentDetail.setTotalWidth(800);
        documentDetail.setLockedWidth(true);

documentDetail.addCell(createPDFPCellBorder(true, true, true, false, BaseColor.LIGHT_GRAY, generalBaseColorFont, boldGrayFont10, Rectangle.BOTTOM, IPDFConstants.HEIGHT_FIXED, heightSize, new Integer[][]{ {IPDFConstants.PADDING_TOP,-2}, {IPDFConstants.PADDING_BOTTOM, 5}},
                text, com.itextpdf.text.Element.ALIGN_CENTER, com.itextpdf.text.Element.ALIGN_CENTER, com.itextpdf.text.Element.ALIGN_RIGHT, null));

documentDetail.writeSelectedRows(0, -1, 50, getHeight(50), canvas);         

当我运行程序并检查pdf时,将写入以下内容: 项目(110)

颜色创建为(类本身在帖子的底部):

BaseColor generalBaseColorFont = new BaseColor(new Color(173, 173, 173));

如何打印此类字符?

如果可能有用,这是我正在使用的过程的代码(我自己没有编写此代码,这是工作的结果):

protected PdfPCell createPDFPCellBorder(boolean lineOver, boolean lineUnder, boolean lineLeft, boolean lineRight, BaseColor backgroundColor, BaseColor borderColor, Font font, 
        int border, Integer heightType, Integer height, Integer paddingArray[][], String nameColumn, 
        Integer alignment, Integer horizontalAlignment, Integer verticalAlignment, Integer colsPan) throws Exception {

    PdfPCell cellCont = new PdfPCell();
    cellCont.setBorder(border);

    if (lineOver){
        cellCont.setBorderWidthTop(Float.valueOf(1));   
    }
    if (lineUnder){
        cellCont.setBorderWidthBottom(Float.valueOf(1));        
    }
    else{
        cellCont.setBorderWidthBottom(Float.valueOf(0));
    }
    if (lineLeft){
        cellCont.setBorderWidthLeft(Float.valueOf(1));
    }
    if (lineRight){
        cellCont.setBorderWidthRight(Float.valueOf(1));
    }
    if (null != backgroundColor)
    {
        cellCont.setBackgroundColor(backgroundColor);
    }
    if (null != borderColor)
    {
        cellCont.setBorderColor(borderColor);
    }       
    if (null != horizontalAlignment)
    {
        cellCont.setHorizontalAlignment(horizontalAlignment);
    }
    if (null != verticalAlignment)
    {
        cellCont.setVerticalAlignment(verticalAlignment);
    }
    if (null != colsPan)
    {
        cellCont.setColspan(colsPan);
    }

    if (null != paddingArray)
    {
        for (int i=0; i<paddingArray.length; i++)
        {
            switch (paddingArray[i][0]) 
            {
                case IPDFConstants.PADDING_RIGHT:
                    if (null != paddingArray[i][0])
                        cellCont.setPaddingRight(paddingArray[i][1]); break;
                case IPDFConstants.PADDING_LEFT:
                    if (null != paddingArray[i][0]) 
                        cellCont.setPaddingLeft(paddingArray[i][1]); break;
                case IPDFConstants.PADDING_BOTTOM:
                    if (null != paddingArray[i][0]) 
                        cellCont.setPaddingBottom(paddingArray[i][1]); break;
                case IPDFConstants.PADDING_TOP:
                    if (null != paddingArray[i][0]) 
                        cellCont.setPaddingTop(paddingArray[i][1]); break;
                case IPDFConstants.PADDING_ALL:
                    if (null != paddingArray[i][0]) 
                        cellCont.setPadding(paddingArray[i][1]); break;
                default:
                break;
            }
        }
    }

    if (null != heightType)
    {
        if (IPDFConstants.HEIGHT_FIXED == heightType) 
        {
            if(null != height) cellCont.setFixedHeight(height);
        } 
        else if (IPDFConstants.HEIGHT_MINIMUN == heightType) 
        {
            if(null != height) cellCont.setMinimumHeight(height);
        }
    }

    Paragraph paragraph = new Paragraph();
    paragraph.setFont(font);
    paragraph.add(nameColumn);
    if(null != alignment)
    {
        paragraph.setAlignment(alignment);
    }
    cellCont.addElement(paragraph);

    return cellCont;
}

public void addCell(PdfPCell cell) {
    rowCompleted = false;
    PdfPCell ncell = new PdfPCell(cell);

    int colspan = ncell.getColspan();
    colspan = Math.max(colspan, 1);
    colspan = Math.min(colspan, currentRow.length - currentRowIdx);
    ncell.setColspan(colspan);

    if (colspan != 1)
        isColspan = true;
    int rdir = ncell.getRunDirection();
    if (rdir == PdfWriter.RUN_DIRECTION_DEFAULT)
        ncell.setRunDirection(runDirection);

    skipColsWithRowspanAbove();

    boolean cellAdded = false;
    if (currentRowIdx < currentRow.length) {
        currentRow[currentRowIdx] = ncell;
        currentRowIdx += colspan;
        cellAdded = true;
    }

    skipColsWithRowspanAbove();

    while (currentRowIdx >= currentRow.length) {
        int numCols = getNumberOfColumns();
        if (runDirection == PdfWriter.RUN_DIRECTION_RTL) {
            PdfPCell rtlRow[] = new PdfPCell[numCols];
            int rev = currentRow.length;
            for (int k = 0; k < currentRow.length; ++k) {
                PdfPCell rcell = currentRow[k];
                int cspan = rcell.getColspan();
                rev -= cspan;
                rtlRow[rev] = rcell;
                k += cspan - 1;
            }
            currentRow = rtlRow;
        }
        PdfPRow row = new PdfPRow(currentRow);
        if (totalWidth > 0) {
            row.setWidths(absoluteWidths);
            totalHeight += row.getMaxHeights();
        }
        rows.add(row);
        currentRow = new PdfPCell[numCols];
        currentRowIdx = 0;
        skipColsWithRowspanAbove();
        rowCompleted = true;
    }

    if (!cellAdded) {
        currentRow[currentRowIdx] = ncell;
        currentRowIdx += colspan;
    }
}

public class BaseColor {
public static final BaseColor WHITE = new BaseColor(255, 255, 255);
public static final BaseColor LIGHT_GRAY = new BaseColor(192, 192, 192);
public static final BaseColor GRAY = new BaseColor(128, 128, 128);
public static final BaseColor DARK_GRAY = new BaseColor(64, 64, 64);
public static final BaseColor BLACK = new BaseColor(0, 0, 0);
public static final BaseColor RED = new BaseColor(255, 0, 0);
public static final BaseColor PINK = new BaseColor(255, 175, 175);
public static final BaseColor ORANGE = new BaseColor(255, 200, 0);
public static final BaseColor YELLOW = new BaseColor(255, 255, 0);
public static final BaseColor GREEN = new BaseColor(0, 255, 0);
public static final BaseColor MAGENTA = new BaseColor(255, 0, 255);
public static final BaseColor CYAN = new BaseColor(0, 255, 255);
public static final BaseColor BLUE = new BaseColor(0, 0, 255);
private static final double FACTOR = 0.7;
private int value;

public BaseColor(int red, int green, int blue, int alpha) {
    validate(red);
    validate(green);
    validate(blue);
    validate(alpha);
    value = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
}

public BaseColor(int red, int green, int blue) {
    this(red, green, blue, 255);
}

public BaseColor(float red, float green, float blue, float alpha) {
    this((int)(red * 255 + .5), (int)(green * 255 + .5), (int)(blue * 255 + .5), (int)(alpha * 255 + .5));
}

public BaseColor(float red, float green, float blue) {
    this(red, green, blue, 1f);
}

public BaseColor(int argb) {
    value = argb;
}

public BaseColor(java.awt.Color color) {
    value = color.getRGB();
}

public int getRGB() {
    return value;
}

public int getRed() {
    return (getRGB() >> 16) & 0xFF;
}

public int getGreen() {
    return (getRGB() >> 8) & 0xFF;
}

public int getBlue() {
    return (getRGB() >> 0) & 0xFF;
}

public int getAlpha() {
    return (getRGB() >> 24) & 0xff;
}

public BaseColor brighter() {
    int r = getRed();
    int g = getGreen();
    int b = getBlue();

    int i = (int) (1.0 / (1.0 - FACTOR));
    if (r == 0 && g == 0 && b == 0) {
        return new BaseColor(i, i, i);
    }
    if (r > 0 && r < i)
        r = i;
    if (g > 0 && g < i)
        g = i;
    if (b > 0 && b < i)
        b = i;

    return new BaseColor(Math.min((int) (r / FACTOR), 255),
            Math.min((int) (g / FACTOR), 255),
            Math.min((int) (b / FACTOR), 255));
}

public BaseColor darker() {
    return new BaseColor(Math.max((int) (getRed() * FACTOR), 0),
            Math.max((int) (getGreen() * FACTOR), 0),
            Math.max((int) (getBlue() * FACTOR), 0));
}

public boolean equals(Object obj) {
    return obj instanceof BaseColor && ((BaseColor) obj).value == this.value;
}

public int hashCode() {
    return value;
}


private static void validate(int value) {
    if (value < 0 || value > 255)
        throw new IllegalArgumentException(MessageLocalization.getComposedMessage("color.value.outside.range.0.255"));
}

}

0 个答案:

没有答案