PDFbox此字体类型仅支持8位代码点

时间:2017-03-24 02:26:10

标签: android pdfbox

我对这个库有一些问题,请帮助我,我尝试使用roboto和arial ttf并且它无法正常工作,我正在尝试用PDF格式编写SigmaΣ符号,它以一个例外结束,我需要知道我是否需要对此进行编码,我该怎么做呢,提前谢谢

public void drawTable(PDPage page, PDPageContentStream contentStream,
                      float y, float margin,
                      String[][] content) throws IOException {
    final int rows = content.length;
    final int cols = content[0].length;
    final float rowHeight = 20f;
    final float tableWidth = page.getBBox().getWidth()-(2*margin);
    final float tableHeight = rowHeight * rows;
    final float colWidth = tableWidth/(float)cols;
    final float cellMargin=5f;

    //draw the rows
    float nexty = y ;
    for (int i = 0; i <= rows; i++) {
        contentStream.drawLine(margin,nexty,margin+tableWidth,nexty);
        nexty-= rowHeight;
    }

    //draw the columns
    float nextx = margin;
    for (int i = 0; i <= cols; i++) {
        contentStream.drawLine(nextx,y,nextx,y-tableHeight);
        nextx += colWidth;
    }


    //now add the text
    contentStream.setFont(PDType1Font.COURIER,12);
    PDTrueTypeFont robotoRegular = PDTrueTypeFont.loadTTF(document, getActivity().getAssets().open("arial.ttf"));
    robotoRegular.encode("WinAnsiEncoding");
    PDTrueTypeFont robotBold = PDTrueTypeFont.loadTTF(document, getActivity().getAssets().open("ariblk.ttf"));
    robotBold.encode("WinAnsiEncoding");
    float textx = margin+cellMargin;
    float texty = y-15;
    for(int i = 0; i < content.length; i++){
        for(int j = 0 ; j < content[i].length; j++){
            String text = content[i][j];
            contentStream.beginText();
            if (j==0){
                contentStream.setFont(robotBold,12);
            }else {
                contentStream.setFont(robotoRegular,12);
            }
//                byte[] commands = "Σ".getBytes();
//                commands[1] = (byte) 128;
//                contentStream.appendRawCommands(commands);
            contentStream.moveTextPositionByAmount(textx,texty);
            contentStream.drawString(text);
            contentStream.endText();
            textx += colWidth;
        }
        texty-=rowHeight;
        textx = margin+cellMargin;
    }
}

1 个答案:

答案 0 :(得分:2)

我已经过测试,使用LiberationSans作为您的字体将允许您对sigma符号进行编码。

PDFont liberationSans = PDType0Font.load(document, getActivity().getAssets().open("com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));将加载字体,然后您可以像往常一样使用drawString。