简体   繁体   中英

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.

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.

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. 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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