我正在使用以下代码将html转换为pdf.But生成pdf时没有显示阿拉伯语。以下代码存在什么问题。
//Create a byte array that will eventually hold our final PDF
Byte[] bytes;
using (var ms = new MemoryStream())
{
FontFactory.Register(Server.MapPath("~/fonts/TRADBDO.TTF"));
using (var doc = new Document())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
var example_html = @"<p>This <em>is البرامج الدراسية المطروحة البرامج الدراسية المطروحةالبرامج الدراسية المطروحةالبرامج الدراسية المطروحةالبرامج الدراسية المطروحةالبرامج الدراسية المطروحة</em>55555<span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
var example_css = @".headline{font-size:200%}";
FontFactory.Register(Server.MapPath("~/fonts/TRADBDO.TTF"));
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
doc.Close();
}
}
bytes = ms.ToArray();
}
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test_123_345.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
答案 0 :(得分:0)
ParseXHtml()
采用一个参数,让您手动处理字体,这可能是最简单的事情。如果您继承FontFactoryImp
,则可以覆盖GetFont
方法并指定自己的字体。下面的代码没有太多的逻辑,并且几乎说“我不关心在HTML中指定了什么字体,总是使用这个”,这应该适合你。
public class FontOverrider : FontFactoryImp {
private readonly BaseFont baseFont;
/// <summary>
/// Create a new font factory that always uses the provided font.
/// </summary>
/// <param name="fullPathToFontFileToUse">The full path to the font file to use.</param>
/// <param name="encoding">The type of encoding to use. Default BaseFont.IDENTITY_H. See <see cref="http://api.itextpdf.com/itext/com/itextpdf/text/pdf/BaseFont.html#createFont(java.lang.String, java.lang.String, boolean)"/> for details.</param>
/// <param name="embedded">Whether or not to embed the entire font. Default True. See <see cref="http://api.itextpdf.com/itext/com/itextpdf/text/pdf/BaseFont.html#createFont(java.lang.String, java.lang.String, boolean)"/> for details.</param>
public FontOverrider( string fullPathToFontFileToUse, string encoding = BaseFont.IDENTITY_H, bool embedded = BaseFont.EMBEDDED ) {
//If you are using this class then this font is required and a missing font should be a fatal error
if (!System.IO.File.Exists(fullPathToFontFileToUse)) {
throw new System.IO.FileNotFoundException("Could not find the supplied font file", fullPathToFontFileToUse);
}
//Create our embedded base font
baseFont = BaseFont.CreateFont(fullPathToFontFileToUse, encoding, embedded);
}
public override iTextSharp.text.Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached) {
return new iTextSharp.text.Font(baseFont, size, style, color);
}
}
要使用它,您只需将ParseXHtml()
来电更改为:
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss, System.Text.Encoding.UTF8, new FontOverrider(myFont));
其中myFont
是您的全字体的Server.MapPath()
(或其他)。
请注意,我在网上查找了您提到的字体,我发现的免费版本无法合法地嵌入PDF中。如果我尝试使用它,我实际上会收到一条消息告诉我。此代码假定您已经处理了许可协议并拥有有效许可的嵌入字体。为了我的示例目的,我只使用了Arial Unicode MS。