Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have search more number of resource but i can't find any one. I want to convert the pdf table formate into excel. I use iTextSharp but i can't correct output all the column values are merged. So plz tell any source to convert pdf table format to excel using c#
Posted
Comments
Richard MacCutchan 28-Oct-15 10:23am    
iTextSharp is the best option. The only alternative is to read the documentation from Adobe and code it yourself; good luck.
Sinisa Hajnal 29-Oct-15 5:25am    
Third party tools are way to go. If you cannot make it work with iTextSharp, google "iTextSharp alternatives" and use another library. Programming it yourself is big waste of time. But as Richard said, you can read Adobe API and do it.

1 solution

Hi...check this code.It's working perfectly.

private void PDFToExcel(DataTable dt)
{
string strPath = "D:\\sample.pdf";
string strSplit="";
DataTable dt = new DataTable();
StringBuilder sbTxt = new StringBuilder();

using (PdfReader pReader = new PdfReader(strPath))
{
for (int i = 1; i <= pReader.NumberOfPages; i++)
{
strSplit = PdfTextExtractor.GetTextFromPage(pReader, i);
}
}

string[] strSplitArr = strSplit.Split('\n');
if (strSplitArr.Length > 0)
{
string[] strCol = strSplitArr[0].Split(' ');
foreach (var col in strCol)
{
dt.Columns.Add(col);
}

for (int i = 1; i < strSplitArr.Length; i++)
{
string[] strRow = strSplitArr[i].Split(' ');
DataRow dr = dt.NewRow();

for (int j = 0; j < strCol.Length; j++)
{
dr[strCol[j].ToString()] = strRow[j].ToString();
}
dt.Rows.Add(dr);
}

DatatableToExcel(dt);
}
}


private void DatatableToExcel(DataTable dt)
{

GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=MyPdfExcel.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);


for (int i = 0; i < GridView1.Rows.Count; i++)

{
GridView1.Rows[i].Attributes.Add("class", "textmode");

}

GridView1.RenderControl(hw);
string style = @" .textmode { mso-number-format:\@; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900