printing Arabic letters iText

29 Views Asked by At

private void PrintButton(object sender, RoutedEventArgs e) { string filePath = "output.pdf";

using (iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(filePath))
{
    using (iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(writer))
    {
        Document document = new Document(pdf);
        
        
        string fontPath = "C:\\Users\\tiish\\source\\repos\\ArbatiMotorcycle\\ArbatiMotorcycle\\Assets\\NRT-Reg.ttf"; 
        iText.Kernel.Font.PdfFont font = PdfFontFactory.CreateFont(fontPath, iText.IO.Font.PdfEncodings.IDENTITY_H);

        List<Product> allProducts = LoadAllProducts();

        List<List<Product>> productPages = DivideIntoPages(allProducts, 24); 

        for (int i = 0; i < CalculateTotalPages(); i++)
        {
            Paragraph header = new Paragraph("ARBATI MOTORS")
                .SetFont(font)
                .SetFontSize(16)
                .SetMarginTop(10)
                .SetMarginBottom(10)
                .SetBackgroundColor(ColorConstants.LIGHT_GRAY);
            document.Add(header);

            Table table = new Table(5); 
            table.SetWidth(UnitValue.CreatePercentValue(100));



           
            Cell idHeader = new Cell().Add(new Paragraph(("پارچە".ArabicWithFontGlyphsToPfd())).SetFont(font).SetBaseDirection(BaseDirection.RIGHT_TO_LEFT));
            Cell nameHeader = new Cell().Add(new Paragraph(("ناو".ArabicWithFontGlyphsToPfd())).SetFont(font).SetBaseDirection(BaseDirection.RIGHT_TO_LEFT).SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT));
            Cell imageHeader = new Cell().Add(new Paragraph(("وێنە".ArabicWithFontGlyphsToPfd())).SetFont(font).SetBaseDirection(BaseDirection.RIGHT_TO_LEFT).SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT));
            Cell dateHeader = new Cell().Add(new Paragraph( ("بەروار")).SetFont(font).SetBaseDirection(BaseDirection.RIGHT_TO_LEFT).SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT));
            Cell piecesHeader = new Cell().Add(new Paragraph(("پارچە".ArabicWithFontGlyphsToPfd())).SetFont(font).SetBaseDirection(BaseDirection.RIGHT_TO_LEFT).SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT));

            table.AddHeaderCell(idHeader);
            table.AddHeaderCell(nameHeader);
            table.AddHeaderCell(imageHeader);
            table.AddHeaderCell(dateHeader);
            table.AddHeaderCell(piecesHeader);

            foreach (Product product in productPages[i])
            {
                table.AddCell(new Cell().Add(new Paragraph(product.ID.ToString()).SetFont(font)));
                table.AddCell(new Cell().Add(new Paragraph(product.Name).SetFont(font)));

               string imagePath = product.ProductImage.ToString();
                ImageData imageData = ImageDataFactory.Create(imagePath);
                Image image = new Image(imageData);

                image.ScaleToFit(64, 64);

                Paragraph imageParagraph = new Paragraph().Add(image);

                Cell imageCell = new Cell().Add(imageParagraph);

                table.AddCell(imageCell);

                table.AddCell(new Cell().Add(new Paragraph(product.Date.ToString()).SetFont(font)));
                table.AddCell(new Cell().Add(new Paragraph(product.Pieces.ToString()).SetFont(font)));
                
            }

           
            document.Add(table);

            Paragraph footer = new Paragraph("Page " + (i + 1) + " of " + CalculateTotalPages())
                .SetFont(font)
                .SetFontSize(12)
                .SetMarginTop(10);
            document.Add(footer);

           
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
        }
    }
}

MessageBox.Show("PDF file saved successfully.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);

}

I'm currently facing an issue with rendering Arabic text correctly when generating PDF documents using iText. Despite following the documentation and various online resources, the Arabic text in the generated PDF appears distorted or incorrectly aligned.

0

There are 0 best solutions below