Java – pdfbox coded symbol currency euro

I use Apache PDF@R_745_2419 @The library creates a PDF document My problem is to encode the euro currency symbol when drawing strings on the page, because the basic font Helvetica does not provide this character How to export“ þ  convert to symbol €?

Solution

Unfortunately, PDF@R_745_2419 @The string encoding of is far from perfect (version 1.8. X) Unfortunately, it uses the same routine when encoding strings in generic PDF objects, just as it does when encoding strings in content streams, which is fundamentally wrong Therefore, you must convert yourself to the correct encoding instead of using pdpagecontentstream Drawstring (use wrong encoding)

For example Instead of using

contentStream.beginText();
    contentStream.setTextMatrix(100,100,50,100);
    contentStream.setFont(PDType1Font.HELVETICA,2);
    contentStream.drawString("€");
    contentStream.endText();
    contentStream.close();

result

You can use something like

contentStream.beginText();
    contentStream.setTextMatrix(100,8);
    byte[] commands = "(x) Tj ".getBytes();
    commands[1] = (byte) 128;
    contentStream.appendRawCommands(commands);
    contentStream.endText();
    contentStream.close();

cause

If you want to know how to use 128 as the byte code of euro, please refer to PDF specification ISO 32000-1, appendix D.2, Latin character set and encoding, which represent the octal value 200 (decimal 128) winansiencoding of euro symbol

PS: other answers also propose an alternative method. In the case of symbols, it is as follows:

contentStream.beginText();
    contentStream.setTextMatrix(100,8);
    contentStream.drawString(String.valueOf(Character.tochars(EncodingManager.INSTANCE.getEncoding(COSName.WIN_ANSI_ENCODING).getCode("Euro"))));
    contentStream.endText();
    contentStream.close();

It does also draw the 'symbol But even if this method looks cleaner (it doesn't use byte arrays and doesn't manually build the actual PDF stream operation), it gets dirty in its own way:

To use a broken method, it actually breaks its string parameters in the right way to offset errors in the method

Therefore, if PDF@R_745_2419 @The personnel decided to repair the damaged PDF@R_745_2419 @Method, the seemingly clean solution code here will begin to fail because it will provide fixed method corrupted input data

There is no denying that I doubt they will be in 2.0 0 (and fixed methods have different names in 2.0.0), but people will never know

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>