iTextSharp 5波兰人物

dza*_*dol 7 c# itextsharp

我使用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();
    }
Run Code Online (Sandbox Code Playgroud)

并尝试使用它:

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

我尝试设置:

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

        writer.DirectContent.SetFontAndSize(bf, 16);
Run Code Online (Sandbox Code Playgroud)

但它不起作用

你有什么主意吗??

问候

Chr*_*aas 7

就像@Mark Storer所说的那样:

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();
}
Run Code Online (Sandbox Code Playgroud)

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

createPDF("<font face=""arial unicode ms"">?????????óÓ??????</font>");
Run Code Online (Sandbox Code Playgroud)


Ral*_*h N 7

我得到了回答!=)(特别针对抛光)我觉得有必要把它放在这个旧线程中,因为我确定我不会是最后找到它.

我对此没有任何好的答案感到非常失望......他们中的大多数人建议在Windows FONTS文件夹中使用ARIALUNI.TTF,这会导致您的PDF文件变大很多倍.解决方案不需要如此激烈......

许多其他人建议使用cp1252进行编码的示例在Arial上失败,而对于波兰语文本则不适用于Helvetica.

我正在使用iTextSharp 4.1.6 ......技巧是...... cp1257!你可以将它与BaseFont.Courier,BaseFont.Helvetica,BaseFont.Times-Roman一起使用

这工作......我的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();
Run Code Online (Sandbox Code Playgroud)

我将稍后进行测试,并确保除了Windows 7之外,我还可以在Windows XP和Mac OSX中打开和阅读这些内容.