public PDFFormFillerResult CreateHtmlToPdfMemoryStream(string sHtmlText)
{
//Create a stream that we can write to, in this case a MemoryStream
using (var ms = new MemoryStream())
{
//Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
var doc = new iTextSharp.text.Document();
//Create a writer that's bound to our PDF abstraction and our stream
var writer = PdfWriter.GetInstance(doc, ms);
//Open the document for writing
doc.Open();
var example_html = sHtmlText;
//Create a new HTMLWorker bound to our document
var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc);
FontFactory.Register(Path.Combine(Environment.CurrentDirectory + "\\Library\\arial.ttf"), "Arial"); // just give a path of arial.ttf
StyleSheet css = new StyleSheet();
css.LoadTagStyle("body", "face", "Garamond");
css.LoadTagStyle("body", "encoding", "Identity-H");
css.LoadTagStyle("body", "size", "12pt");
htmlWorker.SetStyleSheet(css);
// htmlWorker.StartDocument();
//HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
using (var sr = new StringReader(example_html)) // using (TextReader sr = new StreamReader(example_html, Encoding.UTF8))
{
//Parse the HTML
htmlWorker.Parse(sr);
}
doc.Close();
return new PDFFormFillerResult(ms, PDFFormFillerResultType.Success, string.Empty);
}
}
我需要输出为ByteStream,因为我正在合并多个文档以在最后创建一个pdf。这适用于英语,但是当我尝试使用俄语,韩语,Combodian等其他语言时,只会显示模板中的英文文本。