我正在使用Itext在Android中使用以下代码创建pdf文件:
public class CreatingPDF {
private List<SearchResult> mSearchResult;
private Context mContext;
private String mCost;
private String mPdfTitle;
private PdfWriter mPdfWriter;
private static Font catFont;
private static Font redFont;
private static Font subFont;
private static Font smallBold;
public CreatingPDF(Context context, List<SearchResult> searchResults, String title, String cost) throws IOException, DocumentException {
catFont = FontFactory.getFont("assets/artro.ttf",BaseFont.IDENTITY_H,18,Font.ITALIC);
redFont = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL, BaseColor.RED);
subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
mContext = context;
mSearchResult = searchResults;
mPdfTitle = title;
mCost = cost;
createPDF();
}
private void createPDF() {
Date date = new Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
String filename = mContext.getResources().getString(R.string.report_title) + timeStamp;
File pdfFolder = new File(Environment.getExternalStorageDirectory() + "/" +
mContext.getResources().getString(R.string.app_name));
File myFile = new File( pdfFolder + "/" + filename + ".pdf");
if (pdfFolder.exists()) {
Log.e("File : ", "FileAlreadyHere");
} else {
Log.e("File : ", "FileNotHere");
pdfFolder.mkdirs();
}
try {
Document document = new Document();
OutputStream output = new FileOutputStream(myFile);
mPdfWriter = PdfWriter.getInstance(document, output);
document.open();
addMetaData(document);
addTitlePage(document);
document.close();
Log.e("CreatingPdf ", "Pdf Created");
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(myFile), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
mContext.startActivity(intent);
((Activity) mContext).finish();
} catch (ActivityNotFoundException e) {
Log.e("ActivityEx ",e.getMessage());
}
} catch (Exception e) {
Log.e("CreatingPdf Exception ", e.getMessage() + "");
}
}
private void addMetaData(Document document) {
document.addTitle("Report");
document.addAuthor("Ahmed Moharm");
document.addCreator("Ahmed Moharm");
}
private void addTitlePage(Document document) throws DocumentException, UnsupportedEncodingException {
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
preface.add(new Paragraph(mContext.getResources().getString(R.string.report_about_title) + mPdfTitle, catFont));
addEmptyLine(preface, 1);
// Will create: Report generated by: _name, _date
preface.add(new Paragraph(
mContext.getResources().getString(R.string.report_generated_title) +
mContext.getResources().getString(R.string.app_name) + ", " + new Date(), smallBold));
addEmptyLine(preface, 1);
preface.add(new Paragraph(mCost, smallBold));
addEmptyLine(preface, 3);
document.add(preface);
createTable(document);
}
private void createTable(Document document) throws DocumentException, UnsupportedEncodingException {
PdfPTable table = new PdfPTable(4);
PdfPCell c1 = new PdfPCell(new Phrase(mContext.getResources().getString(R.string.search_results_CatName), catFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase(mContext.getResources().getString(R.string.search_results_Cost), catFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase(mContext.getResources().getString(R.string.search_results_Distance), catFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase(mContext.getResources().getString(R.string.search_results_Note), catFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
for (int i = 0; i < mSearchResult.size(); i++) {
table.addCell(new PdfPCell(new Phrase(mSearchResult.get(i).getCatName(), catFont)));
table.addCell(new PdfPCell(new Phrase(mSearchResult.get(i).getCost(), catFont)));
table.addCell(new PdfPCell(new Phrase(mSearchResult.get(i).getDistance(), catFont)));
table.addCell(new PdfPCell(new Phrase(mSearchResult.get(i).getNote(), catFont)));
}
document.add(table);
}
private void addEmptyLine(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
}
当我使用英语单词时它工作正常,但当我使用阿拉伯语或波斯语时,它会在我的表格中显示空单元格&gt;&gt;
像这样: