如何使用itext7从Xamarin.ios中的资源文件夹正确获取字体文件
作为标题。 .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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是不支持TTC字体。
阅读 font docs
具体而言,具体是此部分
The problem is that ttc fonts are not supported.
Read Font Docs
Specifically this part
您是否在info.plist中将文件名添加为
uiappfonts
?对于iOS来说,这是必不可少的一步。请在文章中检查厌食症: 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 .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
inAssemblyInfo.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/ .