我对使用itextSharp的波兰字符有问题。我想从html中创建pdf。一切都很好,但波兰的性格缺失了。我使用的函数较低:
private void createPDF(string html)
{
//MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Test.pdf", FileMode.Create));
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);
// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
}试着用它:
createPDF(“ĄąćęĘłŁŃńóŚśŹźŻż”);
我试试看:
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,Encoding.UTF8.HeaderName,BaseFont.EMBEDDED);
writer.DirectContent.SetFontAndSize(bf, 16);但这是行不通的
你知不知道??
问候
发布于 2011-02-04 21:40:23
就像马克·斯托勒所说的那样:
private void createPDF(string html)
{
//MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Test.pdf", FileMode.Create));
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);
// step 4: we open document and start the worker on the document
document.Open();
// step 4.1: register a unicode font and assign it an allias
FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
// step 4.2: create a style sheet and set the encoding to Identity-H
iTextSharp.text.html.simpleparser.StyleSheet ST = New iTextSharp.text.html.simpleparser.StyleSheet();
ST.LoadTagStyle("body", "encoding", "Identity-H");
// step 4.3: assign the style sheet to the html parser
worker.Style = ST;
worker.StartDocument();
// step 5: parse the html into the document
worker.Parse(reader);
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();
}当您调用它时,使用您在上面注册的名称将文本包装成字体:
createPDF("<font face=""arial unicode ms"">ĄąćęĘłŁŃńóÓŚśŹźŻż</font>");发布于 2012-01-04 20:16:51
我得到了答案!=)我觉得有义务把它放在这条旧线里,因为我肯定我不会是最后一个找到它的人。
我非常失望没有任何好的答案.他们中的大多数建议在您的Windows字体文件夹中使用ARIALUNI.TTF,这会导致您的PDF文件大很多倍。解决方案不需要这么激烈..。
许多其他的例子显示了使用cp1252进行编码的例子,它在Arial上失败,而对于波兰文本则不适用于Helvetica。
我用iTextSharp 4.1.6.诀窍是..。cp1257!你可以和BaseFont.Courier,BaseFont.Helvetica,BaseFont.Times,一起使用
这很管用..。我的PDF文件很小(3kb!)
document.Open();
var bigFont = FontFactory.GetFont(BaseFont.COURIER, BaseFont.CP1257, 18, Font.BOLD);
var para = new Paragraph("Oryginał", bigFont);
document.Add(pgDocType);
document.Close();我将在稍后进行测试,并确保除了Windows 7之外,我还可以在Windows和Mac中打开和阅读这些内容。
发布于 2011-02-04 21:03:08
在创建BaseFont时,需要指定要使用UniCode字符。这个答案展示了如何。
https://stackoverflow.com/questions/4902033
复制相似问题