如何在使用itextpdf创建的pdf文档中添加Unicode带圆圈的数字①②③

时间:2018-11-06 06:51:19

标签: java android pdf

我正在使用itextpdf5在android中创建PDF文档,我的问题是我不能使用unicode字符,例如(\ U24ea(⓪)\ U2460(①).........②③),我尝试了很多在itextpdf 5文档中找到的示例,但是没有用,但是在logcat中它们通常会显示出来。谢谢您的任何建议,这是代码:

public class MainActivity extends AppCompatActivity {
    public static final String DEST = "/file.pdf";
    public static final String cnfreebd = "resources/font/cnfreebd.ttf";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            createPdf();
            PDFView pdfView = findViewById(R.id.pdfView);
            File file = getPublicAlbumStorageDir(DEST);
            pdfView.fromFile(file).load();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    public File getPublicAlbumStorageDir(String albumName) {
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), albumName);
        return file;
    }

    public void createPdf() throws IOException, DocumentException {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        0);
            }
        } else {
        }
        if (isExternalStorageWritable()) {

            File file = getPublicAlbumStorageDir(DEST);
            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();

            PdfPCell UnicodeCercleNumber1 = new PdfPCell(new Paragraph("\u2460"));
            PdfPCell UnicodeCercleNumber2 = new PdfPCell(new Paragraph("\u2461"));
            PdfPCell UnicodeCercleNumber3 = new PdfPCell(new Paragraph("\u2462"));

            Log.i("tag","(1) => \u2460");
            Log.i("tag","(1) => \u2461");
            Log.i("tag","(1) => \u2462");

            table.addCell(UnicodeCercleNumber1);
            table.addCell(UnicodeCercleNumber2);
            table.addCell(UnicodeCercleNumber3);

            document.add(table);
            document.close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您使用的文件名cnfreebd.ttf的CombiNumerals字体不包含Enclosed Alphanumerics U+2460..U+2473的Unicode范围内的任何字符。 / p>

它使用非标准的符号映射来定义“带圆圈的数字”,并将“左手”和“右手”分开:

character map, partial detail only

因此,要获取⓪ ① ②而不是"\u2460 \u2461 \u2462"的输出,您需要输入"\uf070 \uf071 \uf077"