如何解决pdfbox中的"No glyph for U+000A in font Helvetica-Bold"(android端口)
How to solve "No glyph for U+000A in font Helvetica-Bold" in pdfbox (android port)
我想将一些文本写入 pdf。我有 "icrResultTxt" 中的数据。在尝试写入 pdf 之前,我还将数据写入 text.txt 文件。当我尝试写入 pdf 时,我得到“字体 Helvetica-Bold 中没有 U+000A 的字形”。如何解决?我对 "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());
}
}
使用下面的代码代替“\r\n”
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("hello");
contentStream.endText();
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 690);//the second parameter mast between 800.0 and 0.0
我想将一些文本写入 pdf。我有 "icrResultTxt" 中的数据。在尝试写入 pdf 之前,我还将数据写入 text.txt 文件。当我尝试写入 pdf 时,我得到“字体 Helvetica-Bold 中没有 U+000A 的字形”。如何解决?我对 "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());
}
}
使用下面的代码代替“\r\n”
contentStream.moveTextPositionByAmount(100, 700);
contentStream.drawString("hello");
contentStream.endText();
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 690);//the second parameter mast between 800.0 and 0.0