简体   繁体   English

使用ByteArrayOutputStream的UTF-8编码

[英]UTF-8 encoding with ByteArrayOutputStream

I am having issues with the character encoding for non english characters. 我遇到非英语字符的字符编码问题。 Here I am using itext library to generate the pdf. 在这里,我正在使用itext库生成pdf。

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Document document = new Document();
document.setMargins(-50.0f, -50.0f, 15.0f, 0.0f);
PdfWriter.getInstance(document, byteArrayOutputStream);
document.open();

----- -----

myObj.setByteArrayInputStream(new byteArrayInputStream(byteArrayOutputStream.toByteArray()));

I tried with the following code 我尝试了以下代码

myObj.setByteArrayInputStream(new ByteArrayInputStream(byteArrayOutputStream.toString("UTF-8").getBytes()));

PDF has been designed with a too close coupling between encodings and fonts and is – according to today's standards – unnecessarily difficult to use for non-Western text. PDF的设计在编码和字体之间的耦合太紧密,按照当今的标准,非西方文本不必要地难以使用。

The problem in your code is most likely far more fundamental that just the encoding of the final output stream. 代码中的问题很可能比仅最终输出流的编码更根本。 It has been correctly pointed out that PDF is a binary format. 已经正确指出PDF是二进制格式。 So don't try to introduce any encoding in the output stream. 因此,请勿尝试在输出流中引入任何编码。

Instead, have a look at the iText font examples, eg https://itextpdf.com/en/resources/examples/itext-7/itext-7-building-blocks-chapter-1-examples-pdffont and in particular at the examples dealing with foreign text and using PdfEncodings.IDENTITY_H . 相反,请查看iText字体示例,例如https://itextpdf.com/en/resources/examples/itext-7/itext-7-building-blocks-chapter-1-examples-pdffont ,尤其是在处理外文并使用PdfEncodings.IDENTITY_H

If you still have problem, add the relevant code to your question, in particular the code that sets the font and adds the text to the PDF page. 如果仍然有问题,请在问题中添加相关代码,尤其是设置字体并将文本添加到PDF页面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM