我正在阅读带有iText库4.1.6版的PDF文件,通常一切正常。现在,当我阅读由PDF打印驱动程序创建的PDF(通过MS Word打印功能)时,我得到一些ASCII字符并且无法正确转换它们。一些PDF标记被正确转换,如标记BT(开始文本),ET(结束文本)等。但是当涉及存储在PDF数组中的文本对象时(来自PDF ISO,而不是我的C#代码! )单个字符有奇怪的值。例如。我有一个'R',但在字节中它的值为'1'。在ASCII表中,R是'82'(dec)。所以我为这个字母获得了像'SOH'这样的值。其他库可以某种方式转换它。有人可以告诉我如何将这个单字节转换为它的字母'R'?我搜索了几个小时,直到现在都没有用。
以下是我最近阅读PDF文件的代码(iText v.4.1.6)
public string ExtractPureText(string filename)
{
StringBuilder sb = new StringBuilder();
// Create a reader for the given PDF file
PdfReader reader = new PdfReader(filename);
int totalLen = 68;
float charUnit = ((float)totalLen) / (float)reader.NumberOfPages;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
sb.AppendLine(ExtractPureTextFromPDFBytes(reader.GetPageContent(page), page) + " ");
}
return sb.ToString();
}
这是ExtractPureTextFromPDFBytes函数
public string ExtractPureTextFromPDFBytes(byte[] input, int pageNumber)
{
if (input == null || input.Length == 0) return "";
int readPosition = 0;
Encoding enc = new UnicodeEncoding(true, false);
try
{
string resultString = "";
// Flag showing if we are we currently inside a text object
bool inTextObject = false;
// Flag showing if the next character is literal
// e.g. '\\' to get a '\' character or '\(' to get '('
bool nextLiteral = false;
// () Bracket nesting level. Text appears inside ()
int bracketDepth = 0;
// Keep previous chars to get extract numbers etc.:
char[] previousCharacters = new char[_numberOfCharsToKeep];
for (int j = 0; j < _numberOfCharsToKeep; j++) previousCharacters[j] = ' ';
for (readPosition = 0; readPosition < input.Length; readPosition++)
{
char c = (char)input[readPosition];
if (input[readPosition] == 213)
c = "'".ToCharArray()[0];
if (inTextObject)
{
byte[] b = new byte[2];
b[0] = 0;
b[1] = input[116];
byte[] d = new byte[1];
d[0] = input[116];
string bString = System.Text.Encoding.ASCII.GetString(b);
if (readPosition >= 114)
{
String t = new String((char)(input[116] & 0xff), 1);
}
// Position the text
if (bracketDepth == 0)
{
if (CheckToken(new string[] { "TD", "Td", "'", "T*", "\"", "TJ", "Tj", "Tf" }, previousCharacters))
{
resultString += System.Environment.NewLine;
}
}
// End of a text object, also go to a new line.
if (bracketDepth == 0 && CheckToken(new string[] { "ET" }, previousCharacters))
{
resultString += System.Environment.NewLine;
inTextObject = false;
}
else
{
// Start outputting text
if ((c == '(') && (bracketDepth == 0) && (!nextLiteral))
{
bracketDepth = 1;
}
else
{
// Stop outputting text
if ((c == ')') && (bracketDepth == 1) && (!nextLiteral))
{
bracketDepth = 0;
}
else
{
// Just a normal text character:
if (bracketDepth == 1)
{
// Only print out next character no matter what.
// Do not interpret.
if (c == '\\' && !nextLiteral)
{
//resultString += c.ToString();
nextLiteral = true;
}
else
{
if (((c >= ' ') && (c <= '~')) ||
((c >= 128) && (c < 255)))
{
//resultString += c.ToString();
}
nextLiteral = false;
}
}
}
}
}
}
resultString += c.ToString();
// Store the recent characters for
// when we have to go back for a checking
for (int j = 0; j < _numberOfCharsToKeep - 1; j++)
{
previousCharacters[j] = previousCharacters[j + 1];
}
previousCharacters[_numberOfCharsToKeep - 1] = c;
// Start of a text object
if (!inTextObject && CheckToken(new string[] { "BT" }, previousCharacters))
{
inTextObject = true;
resultString += System.Environment.NewLine;
resultString += pageNumber.ToString() + " PN" + System.Environment.NewLine;
}
}
string output = string.Empty;
// clean up text, remove empty lines and trim lines
using (StringReader reader = new StringReader(resultString))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line != string.Empty)
{
output += line + System.Environment.NewLine;
}
}
}
return output;
}
catch
{
return "";
}
}
由于lincensing,我绝对没有选择获得更高版本的iText。只有拥有开发人员许可证的图书馆,但必须为每台机器支付iText许可证才能安装我自己的软件。不幸的是,这对我来说是不可取的。谢谢你的帮助