Mohammad Etedali
Mohammad Etedali

Reputation: 33

how to create PDF in android with unicode support

I would like to create a Pdf file in the android. I use "itextpdf" and create pdf file but after putting Persian text, does not show me anything. below you could find a source which I used https://medium.com/android-school/exploring-itext-to-create-pdf-in-android-5577881998c8 Although I change the font, still have the problem.

            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(fileName));
            document.open();

            document.setPageSize(PageSize.A4);
            document.addCreationDate();
            document.addAuthor("Sample");
            document.addCreator("Sample");
            BaseColor mColorAccent = new BaseColor(0, 153, 204, 255);
            float mHeadingFontSize = 20.0f;
            float mValueFontSize = 26.0f;
            BaseFont urName = BaseFont.createFont("assets/fonts/brandon_bold.otf", "UTF-8", BaseFont.EMBEDDED);
            LineSeparator lineSeparator = new LineSeparator();
            lineSeparator.setLineColor(new BaseColor(0, 0, 0, 68));


            // Title Order Details...
            // Adding Title....
            Font mOrderDetailsTitleFont = new Font(urName, 36.0f, Font.NORMAL, BaseColor.BLACK);
            Chunk mOrderDetailsTitleChunk = new Chunk("تست", mOrderDetailsTitleFont);
            Paragraph mOrderDetailsTitleParagraph = new Paragraph(mOrderDetailsTitleChunk);
            mOrderDetailsTitleParagraph.setAlignment(Element.ALIGN_CENTER);
            document.add(mOrderDetailsTitleParagraph);

            // Fields of Order Details...
            // Adding Chunks for Title and value
            Font mOrderIdFont = new Font(urName, mHeadingFontSize, Font.NORMAL, mColorAccent);
            Chunk mOrderIdChunk = new Chunk("Order No:", mOrderIdFont);
            Paragraph mOrderIdParagraph = new Paragraph(mOrderIdChunk);
            document.add(mOrderIdParagraph);

            Font mOrderIdValueFont = new Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK);
            Chunk mOrderIdValueChunk = new Chunk("#123123", mOrderIdValueFont);
            Paragraph mOrderIdValueParagraph = new Paragraph(mOrderIdValueChunk);
            document.add(mOrderIdValueParagraph);

            // Adding Line Breakable Space....
            document.add(new Paragraph(""));
            // Adding Horizontal Line...
            document.add(new Chunk(lineSeparator));
            // Adding Line Breakable Space....
            document.add(new Paragraph(""));

            // Fields of Order Details...
            Font mOrderDateFont = new Font(urName, mHeadingFontSize, Font.NORMAL, mColorAccent);
            Chunk mOrderDateChunk = new Chunk("Order Date:", mOrderDateFont);
            Paragraph mOrderDateParagraph = new Paragraph(mOrderDateChunk);
            document.add(mOrderDateParagraph);

          Font mOrderDateValueFont = new Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK);
          Chunk mOrderDateValueChunk = new Chunk("06/07/2017", mOrderDateValueFont);
          Paragraph mOrderDateValueParagraph = new Paragraph(mOrderDateValueChunk);
          document.add(mOrderDateValueParagraph);

          document.add(new Paragraph(""));
          document.add(new Chunk(lineSeparator));
          document.add(new Paragraph(""));

            // Fields of Order Details...
            Font mOrderAcNameFont = new Font(urName, mHeadingFontSize, Font.NORMAL, mColorAccent);
            Chunk mOrderAcNameChunk = new Chunk("Account Name:", mOrderAcNameFont);
            Paragraph mOrderAcNameParagraph = new Paragraph(mOrderAcNameChunk);
            document.add(mOrderAcNameParagraph);

            Font mOrderAcNameValueFont = new Font(urName, mValueFontSize, Font.NORMAL, BaseColor.BLACK);
            Chunk mOrderAcNameValueChunk = new Chunk("Pratik Butani", mOrderAcNameValueFont);
            Paragraph mOrderAcNameValueParagraph = new Paragraph(mOrderAcNameValueChunk);
            document.add(mOrderAcNameValueParagraph);

            document.add(new Paragraph(""));
            document.add(new Chunk(lineSeparator));
            document.add(new Paragraph(""));

            document.close();

Upvotes: 0

Views: 1040

Answers (2)

yashar
yashar

Reputation: 1

hello you should use itext's language

if you use following code your problem most be solve.

public void createPdf(String path , String text) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter Writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        // step 3: we open the document
        document.open();
        // step 4
        ArabicLigaturizer pe = new ArabicLigaturizer();
        Writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        BaseFont bf;
        BufferedReader reader = new BufferedReader(new StringReader(text));
        String line;
        StringBuilder trueText = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            trueText.append(pe.process(line));
            trueText.append("\n");
        }
            bf = BaseFont.createFont("assets/your font.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            document.add(new Paragraph(String.valueOf(trueText), new Font(bf, 12)));
            document.addCreationDate();

        // step 5: we close the document
        document.close();
    }

good luck.

Upvotes: 0

Mohammad Etedali
Mohammad Etedali

Reputation: 33

Below site is very useful for Unicode PDF https://developers.itextpdf.com/examples/font-examples/examples-font-tutorial#17-f05_russian_correct_encoding.java

We have to use a font with Unicode support. Also, change below code

BaseFont urName = BaseFont.createFont("assets/fonts/Freesans.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

Font mOrderDetailsTitleFont = new Font(urName, 36.0f, Font.NORMAL, BaseColor.BLACK);
        Chunk mOrderDetailsTitleChunk = new Chunk(new StringBuilder("some unicode character").reverse().toString(),mOrderDetailsTitleFont);
        Paragraph mOrderDetailsTitleParagraph = new Paragraph(mOrderDetailsTitleChunk);
        mOrderDetailsTitleParagraph.setAlignment(Element.ALIGN_CENTER);
        document.add(mOrderDetailsTitleParagraph);

Upvotes: 2

Related Questions