Reputation: 31
I need some help with iText on android. I'm creating a PDF and need it to display characters like: ā, ū, ē, ķ, č. Here is the code I'm using to create the custom font.
BaseFont bf;
{
try {
bf = BaseFont.createFont("c:/windows/fonts/CAMBRIA.TTC", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Font smallFont = new Font(bf, 12);
And here is an example of how I'm using it.
PdfPTable table_start = new PdfPTable(3);
PdfPCell name = new PdfPCell(new Paragraph("Objekts: " + view0.getText().toString(), smallFont));
When I display the String I get from another activity in the view, the text looks normal but when the PDF is created all the special characters are gone.
Upvotes: 1
Views: 4129
Reputation: 4224
I used BaseFont.IDENTITY_H in encoding parameter in call to BaseFont.createFont API and it worked.
Upvotes: 0
Reputation: 31
The answer was actually fairly simple. All I had to do was to put a font that supports Unicode characters in the assets folder and change:
bf = BaseFont.createFont("c:/windows/fonts/CAMBRIA.TTC", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
to
bf_smallfont = BaseFont.createFont("assets/times.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Upvotes: 1
Reputation: 1092
Follow below steps:
formattedMsg = ANSIObj.ConvertToANSIText(outletObj.banglaName);
public String Convert(String line) throws UnsupportedEncodingException {
//Your conversion Code
return line;
}
public void CreateBanglaMemoTableFormat(List<PDFTableData> items) {
Document doc = new Document(PageSize.A4.rotate());
try {
setYourFont();
doc = new Document(new Rectangle(818.3f, 609f));
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyPDF";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, "newPDFFile.pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph();
p1.setAlignment(Paragraph.ALIGN_LEFT);
p1.setFont(f_textFont);
p1.add(formattedMsg);
PdfPTable tt = masteraTableCreation(p1);
doc.add(tt);
}
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
} finally {
doc.close();
}
}
public void setYourFont() throws DocumentException, IOException {
try {
bf = BaseFont.createFont("/fontname", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
f_textFont = new Font(bf, 10);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private PdfPTable masteraTableCreation(Paragraph celV1) {
float[] columnWidths = {236};
PdfPTable mainTable = new PdfPTable(columnWidths);
mainTable.getDefaultCell().setPaddingRight(5);
mainTable.setTotalWidth(786f);
mainTable.setLockedWidth(true);
PdfPCell cell;
//region Row 1
// column 1
cell = new PdfPCell(new Phrase(celV1));
cell.setBorder(Rectangle.NO_BORDER);
mainTable.addCell(cell);
return mainTable;
}
Upvotes: 1