Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jun 25, 2024 9:19 am

am using spire.pdf on xamarin platform, the goal is to export data as pdf, the the code that will add below is working perfectly but i faced a problem with arabic language.

Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
using System.Drawing;
using Spire.Pdf.ColorSpace;
using System.Collections.ObjectModel;
using System.Data;

namespace Test2
{
    public partial class MainPage : ContentPage
    {
        List<Car> cars;
        public MainPage()
        {
            InitializeComponent();
            cars = new List<Car>()
            {
                new Car{ Name="مرسيدس",Model="E350",Color="Brown"},
                new Car{ Name="BMW",Model="i530",Color="Red"},
                new Car{ Name="Toyota",Model="Corolla",Color="White"},
                new Car{ Name="Hunda",Model="Civic",Color="Silver"},
                new Car{ Name="Toyota",Model="Carina",Color="Green"}
            };
        }

        private void btnPrint_Clicked(object sender, EventArgs e)
        {

            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(40));

            //Create a PdfTable object
            PdfTable table = new PdfTable();

            //Draw the text - alignment
            PdfTrueTypeFont font = new PdfTrueTypeFont("Arial", 12f,PdfFontStyle.Bold,true);
            PdfSolidBrush brush = new PdfSolidBrush(System.Drawing.Color.Blue);

            PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            page.Canvas.DrawString("عبدالله للتجارة والمقاولات", font, brush, page.Canvas.ClientSize.Width, 30, rightAlignment);
            page.Canvas.DrawString("نقليات-حفريات-مواد بناء", font, brush, page.Canvas.ClientSize.Width, 60, rightAlignment);


            //Set font for header and the rest cells
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont("Arial", 12f, PdfFontStyle.Regular,true);
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont("Arial", 12f, PdfFontStyle.Bold,true);

            //The data presented by list converted to datatable
            ListtoDataTableConverter converter = new ListtoDataTableConverter();
            DataTable dt = converter.ToDataTable(cars);

            //Set the datatable as the data source of table
            table.DataSource = dt;

            //Show header(the header is hidden by default)
            table.Style.ShowHeader = true;

            //Set font color and backgroud color of header row
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Gray;
            table.Style.HeaderStyle.TextBrush = PdfBrushes.White;

            //Set text alignment in header row
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            //Set text alignment in other cells
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            }

            //Register with BeginRowLayout event
            table.BeginRowLayout += Table_BeginRowLayout;

            //Draw table on the page
            table.Draw(page, new PointF(0, 80));

            doc.SaveToFile("/storage/emulated/0/Download/2.pdf");
            DisplayAlert("Create PDF", "PDF has been created succcessfully", "OK");
        }

        //Event handler
        private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            //Set row height
            args.MinimalHeight = 20f;

            //Alternate row color
            if (args.RowIndex < 0)
            {
                return;
            }
            if (args.RowIndex % 2 == 1)
            {
                args.CellStyle.BackgroundBrush = PdfBrushes.LightGray;
            }
            else
            {
                args.CellStyle.BackgroundBrush = PdfBrushes.White;
            }
        }
    }
}


am testing the app on my physical phone samsumg galaxy m12.

i thought that the problem is related to the font type on my physical phone, so i tried to change the default but i didn't know how to do that because there are costanst or fixed fonts you can choose.

khodor1985
 
Posts: 5
Joined: Tue Jun 25, 2024 9:02 am

Wed Jun 26, 2024 10:02 am

Hello,

Thanks for your inquiry.
To help us investigate further, could you please provide us with your current results document? You can upload it here as an attachment or send it to this email address: [email protected]. Thanks in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Wed Jun 26, 2024 4:40 pm

Firstly, thanks for you reply, the text in red outline is عبد الله
Code: Select all
 PdfTrueTypeFont font = new PdfTrueTypeFont("Arial", 10f, PdfFontStyle.Bold,false);

if the unicode argument is false it will show the text عبد الله as in the attachment file

Code: Select all
 PdfTrueTypeFont font = new PdfTrueTypeFont("Arial", 10f, PdfFontStyle.Bold,true);

if the unicode is true it doesn't show anything

Code: Select all
PdfTrueTypeFont font = new PdfTrueTypeFont("Arial", 10f, PdfFontStyle.Bold);


if the unicode is not assigned it shows the following exception
System.Exception: 'Cannot open file: Arial for reading.'

khodor1985
 
Posts: 5
Joined: Tue Jun 25, 2024 9:02 am

Thu Jun 27, 2024 2:40 am

Hello,

Thanks for your reply.
Regarding your question, my test results seem to be the opposite of yours. For Arabic, you need to specify "unicode" as "true" when creating the font. In addition, the rule of Arabic fonts is from right to left, which can be set through PdfStringFormat. I simplified the code you provided earlier and attached my test results for your reference. Could you please run it on your side and send us the result? Looking forward to your feedback.
Code: Select all
 //Create a PdfDocument object
 PdfDocument doc = new PdfDocument();
 //Add a page
 PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(40));
 //Draw the text - alignment
 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 15f, FontStyle.Regular), true);
 PdfSolidBrush brush = new PdfSolidBrush(System.Drawing.Color.Blue);
 PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
 PdfStringFormat pdfStringFormat = new PdfStringFormat { Alignment = PdfTextAlignment.Right, LineAlignment =
 PdfVerticalAlignment.Middle, RightToLeft = true };
 page.Canvas.DrawString("عبد الله", font, brush, page.Canvas.ClientSize.Width, 30, pdfStringFormat);
 doc.SaveToFile("res.pdf");

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Thu Jun 27, 2024 7:53 am

Thank you for your reply,
i tested the same code you added above on desktop app it worked as a charm, but the problem still on mobile app, am using xamarin platform, i tested the same code on mobile app the result was empty pdf.

On mobile app the PdfTrueTypeFont is written :
Code: Select all
PdfTrueTypeFont font = new PdfTrueTypeFont("Arial", 10f, PdfFontStyle.Bold, true);

On Desktop app the PdfTrueTypeFont is written :
Code: Select all
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 15f, FontStyle.Regular), true);

khodor1985
 
Posts: 5
Joined: Tue Jun 25, 2024 9:02 am

Fri Jun 28, 2024 10:08 am

Hello,

Thanks for your feedback.
Regarding your issue, we need more time to build the environment and test it. I will give you feedback in time if there is any progress. Thank you for your understanding.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Mon Jul 01, 2024 3:50 am

hi, thank you again for your reply, good luck,
waiting you reply...

khodor1985
 
Posts: 5
Joined: Tue Jun 25, 2024 9:02 am

Tue Jul 02, 2024 8:36 am

Hello,

Thank you for your patience.
I have built an Android test environment and reproduced the problem you mentioned. I have logged this issue into our tracking system with ticket SPIREPDF-6876. Our dev team will further investigate and fix it. Once there is any progress, I will inform you as soon as possible.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Tue Jul 16, 2024 4:21 pm

hi again, any solutions about my issue !

khodor1985
 
Posts: 5
Joined: Tue Jun 25, 2024 9:02 am

Wed Jul 17, 2024 6:06 am

Hello,

Thanks for your following up.
Sorry there is no significant progress so far on your issue.Our Dev team is doing more investigations now. Once there is any updates, I will let you know asap. Thanks for your patience.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF