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

发布于 2025-01-23 01:35:05 字数 2700 浏览 0 评论 0原文

作为标题。 .TTC文件已经设置为Bundleresource,并始终复制。

我尝试为itext7制作一种自定义字体,以重新布洛夫问题,即Itext7 PDF无法显示中文和日语角色。但是它无法正确加载字体。 (没有自定义字体,除了生产的PDF无法显示中文外,一切都很好。)

我试图解决这个问题。请有人帮助我。

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();

测试项目的结构看起来像。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风启觞 2025-01-30 01:35:05

问题是不支持TTC字体。

阅读 font docs

具体而言,具体是此部分

每个有效载荷必须完全包含trueType(.ttf)或Opentype(.otf)格式的一个字体文件。收集格式(.ttc或.otc)不支持。

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.

月牙弯弯 2025-01-30 01:35:05

您是否在info.plist中将文件名添加为uiappfonts?对于iOS来说,这是必不可少的一步。

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

请在文章中检查厌食症: https:///www.xamarinhelp.com/custom-com/custom-.com/custom-.com/custom-.com/custom-.com/custom-.com/custom-com/字体 - Xamarin形式/


还有另一种较新的方法可以在Xamarin中启用自定义字体,您可以将字体文件放入共享项目中,并在andy> andyblysblyInfo.cs中添加exportfont属性,以此方式我们不''''需要在平台项目中执行任何操作,请参阅链接: https:/ /devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/

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/ .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文