0

I am using Itext to create pdf files in android using this code:

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(" "));
    }
}
}

when i use english words it works fine, but when i use arabic or persian words it shows empty cells in my tables >>

like this:

enter image description here

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
  • Did you check if the fonts support Arabic? Please do not blame iText for not displaying Arabic if you don't provide a font that supports Arabic! – Bruno Lowagie Nov 19 '16 at 12:14
  • Use TIMES_ROMAN for `catFont` and check the result is same or not. – coder Nov 19 '16 at 12:18
  • Apart from selecting the correct font, you also need to set the run direction, otherwise you Arabic characters will be written from left to right instead of from right to left, and no ligatures will be made. If you'd use iText 7 in combination with the pdfCalligraph addon, you wouldn't have to worry about run direction. Is there any reason why you're not using iText 7? – Bruno Lowagie Nov 19 '16 at 12:21
  • @ramineftekhari `TIMES_ROMAN` is a Standard Type 1 font that only supports Western glyphs. It doesn't support Arabic, nor does it support `IDENTITY-H`. – Bruno Lowagie Nov 19 '16 at 12:23
  • @BrunoLowagie yes it is (arabic transparent) font ,, im using itext5 – Ahmed M Moharm Nov 19 '16 at 12:29
  • @ramineftekhari yes it is the same result ! – Ahmed M Moharm Nov 19 '16 at 12:30
  • try this: `catFont = new Font(BaseFont.createFont("assets/artro.ttf", "UTF-8",BaseFont.EMBEDDED), 18); – coder Nov 19 '16 at 12:55
  • Did you check if `assets/artro.ttf"` was actually found? Chances are that `"assets/artro.ttf"` could not be reached, in which case Helvetica is used instead. No exception is thrown in that case, because `FontFactory` isn't supposed to throw exceptions. ASk `catFont` for its `BaseFont` and ask that `BaseFont` for its PostScript name. – Bruno Lowagie Nov 19 '16 at 13:41

0 Answers0