简体   繁体   English

如何使用iText7从Xamarin.iOS中的资源文件夹中正确获取字体文件

[英]How to get font file correctly from Resources folder in Xamarin.iOS using iText7

as title.如题。 The.ttc file already set as BundleResource and always copy. .ttc 文件已设置为 BundleResource 并始终复制。

I try to make a custom font for iText7 to reslove issue that iText7 PDF can't show Chinese and Japanese characters.我尝试为 iText7 制作自定义字体以解决 iText7 PDF 无法显示中文和日文字符的问题。 But it can not load font correctly.但它无法正确加载字体。 (Without custom font, everything works fine except pdf produced can't show Chinese.) (没有自定义字体,一切正常,除了 pdf 产生的不能显示中文。)

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.测试项目的结构如下所示。

The problem is that ttc fonts are not supported.问题是不支持 ttc fonts。

ReadFont Docs阅读字体文档

Specifically this part具体这部分

Each payload must contain exactly one font file in trueType (.ttf) or OpenType (.otf) format.每个负载必须仅包含一个 trueType (.ttf) 或 OpenType (.otf) 格式的字体文件。 Collection formats (.ttc or.otc) are not supported.不支持集合格式(.ttc 或 .otc)。

Do you add file name as UIAppFonts in info.plist?您是否在 info.plist 中将文件名添加为UIAppFonts It is an indispensable step for iOS.是iOS必不可少的一步。

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

Please check detials in the article: https://www.xamarinhelp.com/custom-fonts-xamarin.forms/ .请查看文章中的详细信息: 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/ . xamarin 中还有另一种启用自定义字体的新方法,您可以将字体文件放在共享项目中,并在AssemblyInfo.cs中添加ExportFont attribute ,这样我们就不需要在平台项目中做任何事情,参考链接: https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin.forms/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM