首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iTextSharp 5波兰字符

iTextSharp 5波兰字符
EN

Stack Overflow用户
提问于 2011-02-04 19:21:58
回答 5查看 19.4K关注 0票数 7

我对使用itextSharp的波兰字符有问题。我想从html中创建pdf。一切都很好,但波兰的性格缺失了。我使用的函数较低:

代码语言:javascript
复制
    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);

代码语言:javascript
复制
        writer.DirectContent.SetFontAndSize(bf, 16);

但这是行不通的

你知不知道??

问候

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-02-04 21:40:23

就像马克·斯托勒所说的那样:

代码语言:javascript
复制
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();
}

当您调用它时,使用您在上面注册的名称将文本包装成字体:

代码语言:javascript
复制
createPDF("<font face=""arial unicode ms"">ĄąćęĘłŁŃńóÓŚśŹźŻż</font>");
票数 8
EN

Stack Overflow用户

发布于 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!)

代码语言:javascript
复制
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中打开和阅读这些内容。

票数 8
EN

Stack Overflow用户

发布于 2011-02-04 21:03:08

在创建BaseFont时,需要指定要使用UniCode字符。这个答案展示了如何。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4902033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档