如何使用 PDFBox 在 PDF 中绘制包含 € 等字符的文本
How to draw text containing characters like € in PDF using PDFBox
我有一段文字要在 pdf 上绘制,类似于 500 €/hour
。我正在使用 PdfBox-Android 库。
我试着把上面的字符串写成如下,
pageContentStream.drawString("500 " + Html.fromHtml(getString(R.string.euro)).toString() + "/hour");
其中 euro
在 strings.xml
中定义为
<string name="euro">(€)</string>
<string name="pound">(£)</string>
上面的代码PdfBox-Android
正在写一些乱码。
我找到了一种使用 pdfbox 编写 €
的解决方案 here,效果很好。
我的问题是如何一次性在 €
登录旁边写下文字..?
我是否需要先写 €
,然后将文本移到它旁边,然后再写剩余的文本?我认为这不是 correct
解决方案。
您需要传递货币符号的HTML代码
Html.fromHtml((String) currency_symbol).toString()
Html.fromHtml((String) "€").toString() //for euro
Html.fromHtml((String) "£").toString() //for pound
通过参考 this 我设法实现了我需要的东西。请参考以下代码片段。
contentStream.beginText();
/*x will act as placeholder here*/
byte[] commands = "(500 x/year**) Tj ".getBytes();
/* commands[index_of_x] = (byte)128, where 128 is decimal value of octal
* 200. (char code for '€' in WinAnsiEncoding).
* you may want to refer annex D.2, Latin Character Set and Encodings of
* PDF specification ISO 32000-1
*/
commands[5] = (byte) 128;
contentStream.appendRawCommands(commands);
contentStream.endText();
contentStream.close();
PDF 规范ISO 32000-1
我有一段文字要在 pdf 上绘制,类似于 500 €/hour
。我正在使用 PdfBox-Android 库。
我试着把上面的字符串写成如下,
pageContentStream.drawString("500 " + Html.fromHtml(getString(R.string.euro)).toString() + "/hour");
其中 euro
在 strings.xml
中定义为
<string name="euro">(€)</string>
<string name="pound">(£)</string>
上面的代码PdfBox-Android
正在写一些乱码。
我找到了一种使用 pdfbox 编写 €
的解决方案 here,效果很好。
我的问题是如何一次性在 €
登录旁边写下文字..?
我是否需要先写 €
,然后将文本移到它旁边,然后再写剩余的文本?我认为这不是 correct
解决方案。
您需要传递货币符号的HTML代码
Html.fromHtml((String) currency_symbol).toString()
Html.fromHtml((String) "€").toString() //for euro
Html.fromHtml((String) "£").toString() //for pound
通过参考 this 我设法实现了我需要的东西。请参考以下代码片段。
contentStream.beginText();
/*x will act as placeholder here*/
byte[] commands = "(500 x/year**) Tj ".getBytes();
/* commands[index_of_x] = (byte)128, where 128 is decimal value of octal
* 200. (char code for '€' in WinAnsiEncoding).
* you may want to refer annex D.2, Latin Character Set and Encodings of
* PDF specification ISO 32000-1
*/
commands[5] = (byte) 128;
contentStream.appendRawCommands(commands);
contentStream.endText();
contentStream.close();
PDF 规范ISO 32000-1