【问题标题】:How to draw text containing characters like € in PDF using PDFBox如何使用PDFBox在PDF中绘制包含€等字符的文本
【发布时间】:2015-07-13 17:52:42
【问题描述】:

我有一个要在 pdf 上绘制的文本,例如 500 €/hour。我正在使用PdfBox-Android 库。

我试图将上面的字符串写成如下,

pageContentStream.drawString("500 " + Html.fromHtml(getString(R.string.euro)).toString() + "/hour");

其中eurostrings.xml 中定义为

<string name="euro">(&#8364;)</string>
<string name="pound">(&#163;)</string>

上面的代码PdfBox-Android正在写一些乱码。

我找到了一个解决方案 here 使用 pdfbox 编写 ,它运行良好。

我的问题是怎么写旁边的文字登录一下..?

我需要先写,然后将文本移到它旁边,然后再写剩余的文本吗?我觉得这不是correct 解决方案。

【问题讨论】:

  • 如您所指的答案中所述,PDFBox 的字符串编码远非完美(1.8.x 版)。这实际上是一种很好的表达方式它坏了。如果您使用 PDFBox 1.8.*(或更早版本),Unicode 代码超过 255 的字符只能使用自定义方法显示,而不是 PDFBox' @ 987654333@.

标签: java android pdf pdfbox


【解决方案1】:

通过参考this,我设法实现了我所需要的。请参考以下代码sn-p。

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

【讨论】:

    【解决方案2】:

    需要传递货币符号的HTML代码

    Html.fromHtml((String) currency_symbol).toString()
    
    Html.fromHtml((String) "&#8364;").toString() //for euro
    
    Html.fromHtml((String) "&#163;").toString()  //for pound
    

    【讨论】:

    • @Rupesh:你不应该只'Accept'这个答案,还应该'Upvote'它!
    • 请原谅我..我正在恢复它..我用£尝试了上述解决方案,并且有效..但它不适用于和其他角色...
    • 你用的是什么代码??参考这个w3schools.com/charsets/ref_utf_currency.asp 因为我试过它工作正常
    • @WISHI 和你的一样.. 但问题是我在pdf 上绘制文本。 PageContentStream还在用上面的方法画乱码..
    • @Rupesh 看看这个如果它解决了你的问题stackoverflow.com/questions/22260344/…
    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 2013-02-24
    • 2014-10-04
    • 1970-01-01
    • 2015-10-21
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多