ASP.NET - problem with iTextSharp on IIS...

Asked By Munendra Maheshwari on 18-Feb-09 12:48 AM

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;

}



Vasanthakumar D replied to Munendra Maheshwari on 18-Feb-09 01:15 AM

Hi,

did you check the following file D:\Munendra\Personal\Stevens CVAT.pdf is aviable in your server system...

and  has sufficein read & write permission?

Vasanthakumar D replied to Munendra Maheshwari on 18-Feb-09 01:15 AM

Hi,

did you check the following file D:\Munendra\Personal\Stevens CVAT.pdf is aviable in your server system...

and  has sufficein read & write permission?

Reply

Munendra Maheshwari replied to Vasanthakumar D on 18-Feb-09 01:17 AM
yes sure, I  have check this already...
What is the problem you are facing?
[)ia6l0 iii replied to Munendra Maheshwari on 18-Feb-09 05:11 AM
end of post
try this code
C_A P replied to Munendra Maheshwari on 18-Feb-09 05:51 AM


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Collections;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnExport_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
form.Controls.Add(GridView1);
StringWriter sw = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(hTextWriter);
string html = sw.ToString();
Document Doc = new Document();

//PdfWriter.GetInstance
//(Doc, new FileStream(Request.PhysicalApplicationPath
//+ "\\AmitJain.pdf", FileMode.Create));

PdfWriter.GetInstance
(Doc, new FileStream(Environment.GetFolderPath
(Environment.SpecialFolder.Desktop)
+ "\\AmitJain.pdf", FileMode.Create));
Doc.Open();

Chunk c = new Chunk
("Export GridView to PDF Using iTextSharp \n",
FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk
("By Amit Jain, amit_jain_online@yahoo.com \n",
FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);

Doc.Add(p);
Doc.Add(p1);

System.Xml.XmlTextReader xmlReader =
new System.Xml.XmlTextReader(new StringReader(html));
HtmlParser.Parse(Doc, xmlReader);

Doc.Close();
string Path = Environment.GetFolderPath
(Environment.SpecialFolder.Desktop)
+ "\\AmitJain.pdf";


ShowPdf(Path);


}

private void ShowPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader
("Content-Disposition","attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();

}

}
TRY THIS LINK
C_A P replied to Munendra Maheshwari on 18-Feb-09 05:57 AM

http://channel9.msdn.com/forums/TechOff/117199-ASPNET-and-iTextSharp-free-PDF-Generator-for-NET/

http://www.codeproject.com/KB/cs/iTextSharpPdfTables.aspx?fid=399569&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2182594

 

Ayman replied to Munendra Maheshwari on 03-Jun-10 08:28 AM
I think your problem is related to security, when you try to open the file from IIS, you are running under ASP.Net user account. This is the account that must have access to the file on the server.
Arpit Jodawat replied to Munendra Maheshwari on 09-Mar-12 09:16 AM
hi, i am trying to use your code, can you tell me from where value come in _numberOfCharsToKeep