1
我想用UTF-8而不是CP1250來顯示國家字符。我發現支持UTF-8的AddParagraph方法,但是我找不到任何壓模示例。如何在iTextSharp PDF Stamper中使用UTF-8編碼?
這是代碼片段與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?
奇怪,但cb.ShowText(field.Text);在解決方案中出現錯誤。 cb.ShowText(field.Text +「」);也工作得很好。 – 2013-02-26 13:52:37
你還知道哪個錯誤? – mkl 2013-02-26 14:52:01
如果field.Text爲null,那麼它將無法使用UTF-8格式。沒問題,我添加「」使其爲空而不是空。 – 2013-02-27 15:50:46