【发布时间】:2017-01-27 17:19:40
【问题描述】:
我想将一些文本写入 pdf。我在“icrResultTxt”中有数据。在尝试写入 pdf 之前,我还将数据写入 text.txt 文件。当我尝试写入 pdf 时,我得到“No glyph for U+000A in font Helvetica-Bold”。如何解决?我对“Helvetica-Bold”没有任何喜爱。我愿意更改为任何字体。
@Override
protected Boolean doInBackground(Void... params) {
Boolean ret = false;
PDDocument document = new PDDocument();
try {
float scale = 0.8f; // alter this value to set the image size
formPreference = getSharedPreferences(SHARD_PREFERENCES,
MODE_PRIVATE);
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(
document, page, false, true);
PDFont pdfFont = PDType1Font.HELVETICA_BOLD;
float fontSize = 25;
float leading = 1.5f * fontSize;
PDRectangle mediabox = page.getMediaBox();
float margin = 72;
float width = mediabox.getWidth() - 2 * margin;
float startX = mediabox.getLowerLeftX() + margin;
float startY = mediabox.getUpperRightY() - margin;
// icrResultTxt;//
writeToFile(icrResultTxt);
String text = icrResultTxt; // "I am trying to create a PDF file with a lot of text contents in the document. I am using PDFBox";
List<String> lines = new ArrayList<String>();
int lastSpace = -1;
while (text.length() > 0) {
int spaceIndex = text.indexOf(' ', lastSpace + 1);
if (spaceIndex < 0) {
lines.add(text);
text = "";
} else {
String subString = text.substring(0, spaceIndex);
float size = fontSize
* pdfFont.getStringWidth(subString) / 1000;
if (size > width) {
if (lastSpace < 0) // So we have a word longer than
// the line... draw it anyways
lastSpace = spaceIndex;
subString = text.substring(0, lastSpace);
lines.add(subString);
text = text.substring(lastSpace).trim();
lastSpace = -1;
} else {
lastSpace = spaceIndex;
}
}
}
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.moveTextPositionByAmount(startX, startY);
for (String line : lines) {
contentStream.drawString(line);
contentStream.moveTextPositionByAmount(0, -leading);
}
contentStream.endText();
contentStream.close();
File fz = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + "hello.pdf");
if (!fz.exists()) {
fz.createNewFile();
} else {
fz.delete();
fz.createNewFile();
}
document.save(fz);
} catch (Exception e) {
Log.v("mango", e.getMessage());
}
private void writeToFile(String data) {
try {
File d = GetImageFile("test.txt");
if (d.exists()) {
d.delete();
d.createNewFile();
} else {
d.createNewFile();
}
FileOutputStream stream = new FileOutputStream(d);
try {
stream.write(data.getBytes());
} finally {
stream.close();
}
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
【问题讨论】:
-
只是不要使用字体不支持的代码。 U+000A 不是字形,它是换行符。删除它。
-
我正在从 Abbyy Mobile Ocr SDK 获取字符串。我无法删除任何内容 :(,你能建议我使用任何其他不会出现换行问题的字体吗?
-
可以添加自定义字体吗? google.com/get/noto 涵盖了您需要的大多数字母。
-
他们都会遇到麻烦,因为换行符不是字形。换行意味着您应该开始一个新行。您应该做的是修复程序的其余部分,拆分您获得的内容并在 PDF 中开始新的一行。或者这样做: line = line.replaceAll("\r\n","?") 你会得到 "?"而不是 LF / CR。这当然看起来很奇怪,而且没有意义。
-
按照我的规定:line=replaceAll("[\\t\\n\\r]","...");但现在得到“此字体类型仅支持 8 位代码点”。老哥怎么办?