Home > Enterprise >  How to get font file correctly from Resources folder in Xamarin.iOS using iText7
How to get font file correctly from Resources folder in Xamarin.iOS using iText7

Time:04-21

as title. The .ttc file already set as BundleResource and always copy.

I try to make a custom font for iText7 to reslove issue that iText7 PDF can't show Chinese and Japanese characters. But it can not load font correctly. (Without custom font, everything works fine except pdf produced can't show Chinese.)

I've tried to solve this for days. Plz somebody help me.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using iText;
using iText.Layout;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.IO.Font;
using Foundation;
using System.Reflection;
using iText.IO.Font.Constants;

namespace iTextSharpTest
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private void BtnPDF_Clicked(object sender, EventArgs e)
        {
            CreatePdfiOS();
        }
        private void CreatePdfiOS()
        {
            string nowTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            //Create PDFwriter and Setting pdf file name & path
            string defaultPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string filename = Path.Combine(defaultPath, nowTime   ".pdf");
            PdfWriter pdfWriter = new PdfWriter(filename);
            PdfDocument pdf = new PdfDocument(pdfWriter);
            Document _document = new Document(pdf);
        
            string filePath = NSBundle.MainBundle.PathForResource("kaiu", "ttf");
            NSData data = NSData.FromFile(filePath);
            MemoryStream ms = new MemoryStream();
            data.AsStream().CopyTo(ms);
            byte[] buffer = ms.ToArray();
            PdfFont pdfFont = PdfFontFactory.CreateFont(buffer, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED);
            
            _document.SetFont(pdfFont);
            //Header
            Paragraph header = new Paragraph("測 試 股 份 有 限 公 司Test Company")
                .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
                .SetFontSize(20);
            _document.Add(header);
            //Sub Header
            Paragraph subHeader = new Paragraph("轉  帳  傳  票Transefer Invoice")
                .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
                .SetFontSize(20);
            _document.Add(subHeader);
            //Separator
            LineSeparator line = new LineSeparator(new SolidLine());
            _document.Add(line);
            _document.Close();

The structure of test project looks like.

CodePudding user response:

The problem is that ttc fonts are not supported.

Read Font Docs

Specifically this part

Each payload must contain exactly one font file in trueType (.ttf) or OpenType (.otf) format. Collection formats (.ttc or .otc) are not supported.

CodePudding user response:

Do you add file name as UIAppFonts in info.plist ? It is an indispensable step for iOS .

<key>UIAppFonts</key>
<array>
    <string>kaiu.ttf</string>
</array>

Please check detials in the article : https://www.xamarinhelp.com/custom-fonts-xamarin-forms/ .


And there is another newer way to enable custom font in xamarin , you can place font file in shared project and add ExportFont attribute in AssemblyInfo.cs , in this way we don't need to do anything in platform project , refer to the link : https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/ .

  • Related