Hi,
I am working in ASP.Net 2008 using C# and somewhere in my project I need to extract text from a PDF file and store it into a text file.
I use the folowing code by adding a reference of iTextsharp dll.
This code works fine in dot net, but not working on IIS. Can any one tell me what is wrong here. Please I m in need indee
protected
void Button1_Click(object sender, EventArgs e)
{
String file = @"D:\Munendra\Personal\Stevens CVAT.pdf";
if (!File.Exists(file))
{
file = Path.GetFullPath(file);
if (!File.Exists(file))
{
Console.WriteLine("Please give in the path to the PDF file.");
}
}
ExtractText(file, "C:\\" + Path.GetFileNameWithoutExtension(file) + ".txt");
}
public
bool ExtractText(string inFileName, string outFileName)
{
arrCheck =
new ArrayList();
StreamWriter outFile = null;
try
{
// Create a reader for the given PDF file
PdfReader reader = new PdfReader(inFileName);
outFile =
new StreamWriter(outFileName, false, System.Text.Encoding.UTF8);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
String Text = "";
Text = ExtractTextFromPDFBytes(reader.GetPageContent(page)) +
" ";
Text = Text.Replace(
"\r", "");
String[] split = Text.Split('\n');
for (int iCount = 0; iCount < split.Length; iCount++)
{
arrCheck.Add(split.GetValue(iCount).ToString().Trim());
outFile.Write(split.GetValue(iCount).ToString().Trim());
outFile.WriteLine();
}
}
Response.Write(
"Success");
return true;
}
catch
{
return false; }
finally
{
if (outFile != null) outFile.Close(); }
}
private string ExtractTextFromPDFBytes(byte[] input)
{
if (input == null || input.Length == 0) return "";
try
{
string resultString = "";
bool inTextObject = false;
bool nextLiteral = false;
int bracketDepth = 0;
char[] previousCharacters = new char[_numberOfCharsToKeep];
for (int j = 0; j < _numberOfCharsToKeep; j++) previousCharacters[j] = ' ';
for (int i = 0; i < input.Length; i++)
{
char c = (char)input[i];
if (inTextObject)
{
if (bracketDepth == 0)
if (CheckToken(new string[] { "TD", "Td" }, previousCharacters))
resultString +=
"\n\r";
else
if (CheckToken(new string[] { "'", "T*", "\"" }, previousCharacters))
resultString +=
"\n";
else
if (CheckToken(new string[] { "Tj" }, previousCharacters))
resultString +=
" ";
// End of a text object, also go to a new line.
if (bracketDepth == 0 && CheckToken(new string[] { "ET" }, previousCharacters))
{
inTextObject =
false;
resultString +=
" ";
}
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)
{
if (c == '\\' && !nextLiteral)
nextLiteral =
true;
else
{
if (((c >= ' ') && (c <= '~')) || ((c >= 128) && (c < 255)))
resultString += c.ToString();
nextLiteral =
false;
}
}
}
}
}
}
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;
}
return resultString;
}
catch
{
return ""; }
}
private bool CheckToken(string[] tokens, char[] recent)
{
foreach (string token in tokens)
{
if ((recent[_numberOfCharsToKeep - 3] == token[0]) &&
(recent[_numberOfCharsToKeep - 2] == token[1]) &&
((recent[_numberOfCharsToKeep - 1] ==
' ') ||
(recent[_numberOfCharsToKeep - 1] == 0x0d) ||
(recent[_numberOfCharsToKeep - 1] == 0x0a)) &&
((recent[_numberOfCharsToKeep - 4] ==
' ') ||
(recent[_numberOfCharsToKeep - 4] == 0x0d) ||
(recent[_numberOfCharsToKeep - 4] == 0x0a))
)
return true;
}
return false;
}