c# extract data from pdf file - c#

I was trying to extract the data from pdf file using iTextSharp, but i go two errors. Actually I want to extract the data from pdf file and store it into database.
Here is my code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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 System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.util.collections;
using System.Linq;
public partial class frm_CreatePDF : System.Web.UI.Page
{
public string P_InputStream3 = "~/My Documents/List Of Holidays 2012";
protected void Page_Load(object sender, EventArgs e)
{
ExtractText();
}
private string ExtractText()
{
PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new LocationTextExtractionStrategy());
return txt;
}
}
And the error is:
The name 'PdfTextExtractor' does not exist in the current context
The type or namespace name 'LocationTextExtractionStrategy

Make sure you have brought the namespace in which those two classes are defined into scope by adding the using directive:
using iTextSharp.text.pdf.parser;

I guess you need to use another namespace where LocationTextExtractionStrategy is defined

First of all you need to add a reference to the ItextSharp dll, after wich you can add a using statement to acces the namespace that contains that static class
http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.100%29.aspx

This is a late answer, but I've found what your issue is:
You are missing the iTextSharp.text.pdf.parser using statement. Add this below your using iTextSharp.text.pdf; code:
using iTextSharp.text.pdf.parser;
That should be able to find your LocationTextExtractionStrategy pretty well.
You can also just add iTextSharp.text.pdf.parser directly behind your LocationTextExtractionStrategy if you want.
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
But I would recommend the former. It's cleaner and easier to read.

Related

How to read text from word file? C#

I am trying to read a text from document file in C#
I can't create new Document in C#.
I try to using "using Microsoft.Office.Interop.Word"
or "using System.Windows.Documents" but it does'nt recognize the code "Document doc=new Document".
In addition how can I read a text from .docx file?
The code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Interop.Word; //didnt recognize "Office"
namespace DocumentTest1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Application word = new Application();
Document doc = new Document();
}
}
What could be the problem? tnx
Hi Please see below code
List<string> data = new List<string>();
Application app = new Application();
Document doc = app.Documents.Open(ref readFromPath);
foreach (Paragraph objParagraph in doc.Paragraphs)
data.Add(objParagraph.Range.Text.Trim());

write to memoerystream using epplus

I have this assembly that basically creates an empty excel file using EPPLUS library. The file is created directly to desk, but i want first to write it in a memoerystream and then save the memoerystream to desk. I have this so far.
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationXLSX
{
class Program
{
static void Main(string[] args)
{
Createxlsx("XLSX");
}
public static void Createxlsx(string filename)
{
FileInfo newFile = new FileInfo("C:\\ConsoleApplicationXLSX\\" + filename + ".xlsx");
MemoryStream ms = new MemoryStream();
//create a package
using (ExcelPackage package = new ExcelPackage(ms))
{
package.Workbook.Worksheets.Add("worksheet");
package.Save();
}
}
}
}
Try
package.SaveAs(ms)
That should fill your stream with the excelfile-content.

System.IO.File is a type not a namespace

Im tring to create a file using a System.Io.File namespace howeever im using it on MVC witch im new to and i get this error when i publish my proyect "A using namespace directive can only be applied to namespaces; 'System.IO.File' is a type not a namespace"
This is my using Statement:
using System;
using System.Reflection;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.IO.File;
using System.Text;
using (var reader = System.IO.File.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
{
// Starting outer json array
reader.WriteLine("[");
for (var rowIndex = 0; rowIndex < myTable.Rows.Count; rowIndex++)
{
var row = myTable.Rows[rowIndex];
var rowValues = new List<string>(); // can be reused if needed
foreach (DataColumn column in myTable.Columns)
rowValues.Add(row[column].ToString());
var jsonRow = JsonConvert.SerializeObject(rowValues);
// Write current row
reader.Write(jsonRow);
// Add separating comma
if (rowIndex != myTable.Rows.Count - 1)
reader.WriteLine(",");
}
// End outer json array
reader.WriteLine("]");
}
the using keyword have different semantics depending on where it is located.
When put directly in a file it's to tell which namespaces to import. In that context you can not have a using statement for a class directly. Well. You can, but the syntax is different. MSDN.
The other usage is to dispose an object when it goes out of scope. In this case you can enter the fully qualified class name (namespace + class name) or just the class name. MSDN
In your code you have mixed the two.
Alternative 1
Completely remove the using statement in the file and just specify the full class name in the statement.
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
//in your method
using (var reader = System.IO.File.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
Alternative 2
Remove the namespace from the statement and the class name from the directive:
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
//in your method
using (var reader = File.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
Alternative 3
Rename the class using a directive. You typically use this when the compiler can't distinguish between different identifiers (like having the same class name in different imported namespaces).
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using IoFile = System.IO.File; //this
using System.Text;
//in your method
using (var reader = IoFile.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
Your code is missing class and method declarations. System.IO.File is in fact a type and you shouldn't be referencing it in your using statements. All you need to reference is System.IO and then you can call File.CreateText().
using System;
using System.IO;
public class MyClass
{
public void CreateFile()
{
string path = #"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
}
}
MSDN on File.CreateText()
Try this.
using (StreamWriter sw = File.CreateText(fileName))
{
sw.WriteLine("New file created: {0}", DateTime.Now.ToString());
}

cannot access object of class XmlDocument on C#

I was trying to write an automatic method that retrieve a value from an XML file.
as i wrote the following code:
XmlDocument xDoc = new XmlDocument();
I found out that when I'm trying to access xDoc object while typing xDoc. , it does nothing, means no option to manipulate the object...
my usings are:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Office.Interop;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;
using System.Collections.Generic;
Any ideas ?
Sounds like you need to make a class to put the XMLDocument in if (per your comment) that is the next line you could have attached.
What I mean is, as Jon Skeet said, are you sure Adam that you're not declaring the XMLDocument inside of the namespace instead of inside of a class. Should be something like:
using System.Xml;
namespace SomeNameSpace
{
public class MyClass
{
XMLDocument xDoc = new XMLDocument();
public void MyMethod() {
xDoc.DocumentType;
}
}
}

how to send xml file to android application using c#

I want to send data from c#(aspx) to android application.
My c#(aspx)page have code which is called from android application, that retrieve values from sql database and put it into an XML file using DataSet and return back xml file to android application.
The code for retrieving data from database and storing in xml is as below:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;
class ExecuteXmlReader
{
public static void Main()
{
String sConnection = "server=.\\SQLExpress;Integrated Security=SSPI;database=employee";
SqlConnection mySqlConnection = new SqlConnection(sConnection);
mySqlConnection.Open();
// Get the same data through the provider.
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("SELECT FirstName,LastName,Title,City from employees", sConnection);
DataSet myDataSet2 = new DataSet();myDataSet2.DataSetName = "Region";
mySqlDataAdapter.Fill(myDataSet2);
// Write data to files: data1.xml and data2.xml for comparison.
myDataSet2.WriteXml("c:/temp/employees.xml");
mySqlConnection.Close();
}
}
now how do I return this xml file to android application. In android I am using DOM XML parser to extract data from file.
Please help me in this.. any tips will be accepted.
use this it has sample wcf webservice with consumption in android-http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx.hope this helps you

Categories

Resources