我想使用UTF-8而不是CP1250来显示国家字符。我找到了支持UTF-8的AddParagraph方法,但我找不到任何压模示例。
这是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?
答案 0 :(得分:7)
如果您需要使用单一字体的各种字符,则最有可能使用IDENTITY_H(或垂直文本时为IDENTITY_V)作为编码。
E.g:
public const string FONT = "c:/windows/fonts/arialbd.ttf";
BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
比照。 Webified iTextSharp Example UnicodeExample.cs用于上下文。