PDFBOX : U+000A ('controlLF') is not available in this font Helvetica encoding: WinAnsiEncoding

独自空忆成欢 提交于 2019-12-01 17:33:11
razvan

[PROBLEM] The String you are trying to display contains a newline character.

[SOLUTION] Replace the String with a new one and remove the newline:

text = text.replace("\n", "").replace("\r", "");

if you are trying to set a new line using "\n" in a string . you can try PDPageContentStream.newLineAtOffset(x,y) to add a new line

  PDFont font =  PDType1Font.HELVETICA ; 

  PDDocument doc    = new PDDocument();
  PDPage page = new PDPage();
  PDPageContentStream content = new PDPageContentStream(doc, page);
  content.beginText();
  content.moveTextPositionByAmount(10, 700);
  content.setFont(font, 12);
  content.drawString("start text   ");
  content.newLineAtOffset(0, -15);
  content.drawString("text in new line  ");        
  content.endText();
  content.close();
  doc.addPage(page);
  doc.save("file.pdf");

and the pdf

Sometimes you have to change a font like :

PDFont font = PDType0Font.load(document, new File("C:\\Users\\dw\\Desktop\\FZLTXHJW.TTF"));

Replace "FZLTXHJW.TTF" with the font you have and it should support your text encoding.

If you'd like to retain the newline addition, i.e you indeed want your text to split and appear the later part in new line, then you can simply replace the \n with an HTML break tag, like below below .

return text.replace("\n","<br>");

:)

The answer selected for this post works, replacing all instances of \n and \r from your string, if you know that it's a \n or \r character that's causing your problem. I've discovered that there are lots of different characters that will produce this error. Here's a sampling of those that I've found:

U+2010 ('hyphentwo') is not available in this font Helvetica encoding: WinAnsiEncoding
U+2033 ('second') is not available in this font Helvetica encoding: WinAnsiEncoding
U+00A0 ('nbspace') is not available in this font Helvetica encoding: WinAnsiEncoding
U+FFFD ('.notdef') is not available in this font Helvetica encoding: WinAnsiEncoding
U+03BC ('mugreek') is not available in this font Helvetica encoding: WinAnsiEncoding
U+039C ('Mu') is not available in this font Helvetica encoding: WinAnsiEncoding
U+2212 ('minus') is not available in this font Helvetica encoding: WinAnsiEncoding
U+0141 ('Lslash') is not available in this font Helvetica encoding: WinAnsiEncoding
U+2103 ('centigrade') is not available in this font Helvetica encoding: WinAnsiEncoding
U+25AA ('H18543') is not available in this font Helvetica encoding: WinAnsiEncoding

In my case, I've simply chosen to remove any special character that's not included in my font. I used the solution from this page:

https://cmsdk.com/java/remove-illegal-characters-from-string-with-pdfbox.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!