使用Winforms将ASCII字符127写入PDF文件

时间:2012-05-24 15:11:24

标签: c# .net ascii itextsharp

我正在开发一个Winforms应用程序的小问题。我有一些全局热键可以将一些ASCII字符插入到文本框中。然后使用iTextSharp将字符写入PDF文件。这个工作正常,除了一个字符:方形符号(#127)。这个角色在windows应用程序中看起来很好,但是一旦我将它写入PDF,它就会显示为一个子弹点,并且在它之后应该出现的任何角色(而不是出现在它前面)。我无法弄清楚为什么会这样。我写的所有其他ASCII字符都没有问题。有什么建议吗?

作为参考,这是我正在使用的字符代码表:http://yorktown.cbe.wwu.edu/sandvig/shared/ASCIICodes.aspx

以下是将字符插入表单的代码:

if(focusedTextbox != null)
{
    if (inKey == 'S')
    {
        focusedTextbox.Text += Convert.ToChar(127);//Square symbol
    }
    else if (inKey == 'P')
    {
        focusedTextbox.Text += Convert.ToChar(177);//Plus-minus symbol
    }
    //Places the cursor after the newly-inserted symbol
    focusedTextbox.Select(focusedTextbox.Text.Length, 0);
}

以下是使用iTextSharp将数据写入PDF的代码:

//Measurement A
cb.BeginText();
string mesA = mainForm.txtMesA.Text.Trim();
if (mesA.Equals(""))
{
    text = "";
}
else
{
    text = mesA + " " + mainForm.txtUnit.Text;
}
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text, offSetStartAt, topRow, 0);
offSetStartAt += colOffset;
cb.EndText();

//Create a PDF page and add the content
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);

//Close the streams
document.Close();
fs.Close();
writer.Close();
reader.Close();

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:3)

ASCII 127是control character - 具体而言,Delete。控制字符无法打印。

我认为广场上没有ASCII聊天功能。在我的浏览器中,问题列表中的127显示为空白:

enter image description here

如果您看到正方形,则可能是您的浏览器字体尝试呈现不受支持的字符的结果。

如果您需要渲染正方形,则可以使用两个方括号[](代码91和93)作为近似值。