【发布时间】:2018-03-13 12:37:01
【问题描述】:
我尝试生成 PDF。拉丁字母显示功能正确,但俄语根本不显示。 这是我的功能。这里有什么问题?
public string CreatePDFfromHTML(
string htmlText
, string fileName
, InvoiceData _invoiceData
, int Pages
, bool OpenPDF = true
, bool landscape = false)
{
Document document = null;
FileStream file = null;
Uri uri = null;
try
{
var directory = new Java.IO.File(
Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim), "pdf").ToString();
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
var absoluteFilePath = System.IO.Path.Combine(directory, fileName);
if (System.IO.File.Exists(absoluteFilePath))
System.IO.File.Delete(absoluteFilePath);
file = System.IO.File.Create(absoluteFilePath);
if (landscape)
document = new Document(PageSize.A4.Rotate(), 30, 20, 20, 60);
else
document = new Document(PageSize.A4, 30, 20, 40, 60);
PdfWriter _writer = PdfWriter.GetInstance(document, file);
HeaderFooterPageEvent _event = new HeaderFooterPageEvent(_invoiceData, Pages);
_writer.PageEvent = _event;
document.Open();
string FontPath = "arialunicodems.ttf#arialunicodems";
XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontImp.Register(FontPath);
FontFactory.FontImp = fontImp;
byte[] byteArray = Encoding.UTF8.GetBytes(htmlText);
Stream htmlInputStream = new MemoryStream(byteArray,true);
XMLWorkerHelper.GetInstance().ParseXHtml(_writer, document, htmlInputStream, null, Encoding.UTF8, fontImp);
document.Close();
file.Close();
uri = Uri.Parse("file:///" + absoluteFilePath);
if (OpenPDF)
{
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, "application/pdf");
intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
Forms.Context.StartActivity(intent);
}
return absoluteFilePath;
}
catch (Exception e)
{
UserDialogs.Instance.AlertAsync("CreatePDFfromHTML: " + e.Message + "'", AppResources.error);
e.PrintStackTrace();
document.Close();
file.Close();
}
catch (System.Exception e)
{
UserDialogs.Instance.AlertAsync("CreatePDFfromHTML: " + e.Message + "'", AppResources.error);
document.Close();
file.Close();
}
return null;
}
【问题讨论】:
-
已解决:在Android项目中,我将字体文件保存在本地并确定位置: