如何在iTextSharp PDF Stamper中使用UTF-8编码

本文关键字:UTF-8 编码 Stamper iTextSharp PDF | 更新日期: 2023-09-27 18:01:05

我想使用UTF-8而不是CP1250来显示国家字符。我找到了支持UTF-8的AddParagraph方法,但找不到任何stamper示例。

这是CP1250:的代码片段

        PdfReader reader = new PdfReader(templatePath);
        byte[] bytes;
        using (MemoryStream ms = new MemoryStream())
        {
            using (PdfStamper stamper = new PdfStamper(reader, ms))
            {
                PdfContentByte cb = stamper.GetOverContent(1);
                BaseFont bf = BaseFont.CreateFont(
                   BaseFont.COURIER_BOLD, 
                   BaseFont.CP1250, 
                   true);
                //Begin text command
                cb.BeginText();
                //Set the font information                        
                cb.SetFontAndSize(bf,12f);
                //Position the cursor for drawing
                cb.MoveText(field.X, field.Y);
                //Write some text
                cb.ShowText(field.Text);
                //End text command
                cb.EndText();
                //Flush the PdfStamper's buffer
                stamper.Close();
                //Get the raw bytes of the PDF
                bytes = ms.ToArray();
            }
        }

如何使用UTF-8?

如何在iTextSharp PDF Stamper中使用UTF-8编码

如果您需要单个字体中的大量字符,则很可能应该使用IDENTITY_H(如果是垂直文本,则使用IDENTITY_V(作为编码。

例如:

public const string FONT = "c:/windows/fonts/arialbd.ttf";
BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

参见。在上下文中使用的Webized iTextSharp示例UnicodeExample.cs