iTextSharp的5润色人物 [英] iTextSharp 5 polish character

查看:156
本文介绍了iTextSharp的5润色人物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用iTextSharp的抛光人品问题。我想创建HTML PDF。一切工作正常,但抛光性格丢失。我使用功能低:

 私人无效createPDF(字符串HTML)
    {
        // MemoryStream的msOutput =新的MemoryStream();
        读者的TextReader =新StringReader(HTML); //第1步:创建一个文件对象的
        文档的文档=新的文件(PageSize.A4,30,30,30,30);        // 第2步:
        //我们创建侦听到文档中的作家
        //和指导XML的流文件
        PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream(的test.pdf,FileMode.Create));        //第3步:我们创建了一个工人解析文档
        HTMLWorker工人=新HTMLWorker(文件);        //步骤4:我们打开文件,并开始对文档的工人
        document.Open();
        worker.StartDocument();        //第5步:解析HTML到文档
        worker.Parse(读卡器);        //步骤6:关闭文档和工人
        worker.EndDocument();
        worker.Close();
        document.Close();
    }

,并尝试使用它:


  

createPDF(ĄąćęĘłŁŃńóÓŚśŹźŻż);


我尝试设置:


  

BASEFONT BF = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,Encoding.UTF8.HeaderName,BaseFont.EMBEDDED);


  writer.DirectContent.SetFontAndSize(BF,16);

但忽略了最低工作

你有什么想法?

问候


解决方案

只是为了一起推出什么@马克斯托勒说:

 私人无效createPDF(字符串HTML)
{
    // MemoryStream的msOutput =新的MemoryStream();
    读者的TextReader =新StringReader(HTML); //第1步:创建一个文件对象的
    文档的文档=新的文件(PageSize.A4,30,30,30,30);    // 第2步:
    //我们创建侦听到文档中的作家
    //和指导XML的流文件
    PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream(的test.pdf,FileMode.Create));    //第3步:我们创建了一个工人解析文档
    HTMLWorker工人=新HTMLWorker(文件);    //步骤4:我们打开文件,并开始对文档的工人
    document.Open();    // 4.1步:注册一个​​UNI code字型,并为其分配allias
    FontFactory.Register(C:\\\\ \\\\的Windows \\\\字体ARIALUNI.TTF,宋体UNI code MS);    // 4.2步:创建一个样式表并设置编码标识-H
    iTextSharp.text.html.simpleparser.StyleSheet ST =新iTextSharp.text.html.simpleparser.StyleSheet();
    ST.LoadTagStyle(身体,编码,身份-H);    // 4.3步:指定样式表的HTML解析器
    worker.Style = ST;    worker.StartDocument();    //第5步:解析HTML到文档
    worker.Parse(读卡器);    //步骤6:关闭文档和工人
    worker.EndDocument();
    worker.Close();
    document.Close();
}

当你骂它使用您在上面登记的名称在包装的字体文本:

  createPDF(<字型=宋体UNI code MS>ĄąćęĘłŁŃńóÓŚśŹźŻż< / FONT>中);

I have problem with polish character using itextSharp. I want to create pdf from html. Everything works fine but polish character are missing. I use function lower:

    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();
    }

And Try use it:

createPDF("ĄąćęĘłŁŃńóÓŚśŹźŻż");

I try set:

BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, Encoding.UTF8.HeaderName, BaseFont.EMBEDDED);

        writer.DirectContent.SetFontAndSize(bf, 16);

But it dosen't work

Do you have any idea??

Regards

解决方案

Just to roll together what @Mark Storer said:

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();
}

And when you call it wrap your text in a font using the name you registered above:

createPDF("<font face=""arial unicode ms"">ĄąćęĘłŁŃńóÓŚśŹźŻż</font>");

这篇关于iTextSharp的5润色人物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆