I wish to Read a word file without using Interop.word dll...Do not want to install word in IIS..Actualy I made a keyword search by converting word file into txt file and reading from it..I tried using Open xml SDK but it doesn't read old doc files correctly..Also found spire.doc which is payment type..Provide a complete code with solution at the earliest...
Code as follows:
private void SearchWord(string[] str1)
{
string filename1 = "";
string randomName = "";
string fname = "";
Session["cids"] = "";
object missingType = Type.Missing;
object readOnly = true;
object isVisible = false;
object documentFormat = 8;
string s12 = "select id,docfilename from Uploadeddocsmaster";
dt = cn.viewdatatable(s12);
int dtcount = dt.Rows.Count * 2;
string[] ids = new string[dtcount];
for (int k = 0; k < dt.Rows.Count; k++)
{
string id = dt.Rows[k]["id"].ToString();
filename1 = dt.Rows[k]["docfilename"].ToString();
string fileName = Server.MapPath("~/UploadedFiles/") + filename1;
string ext = Path.GetExtension(fileName);
if (ext == ".doc" || ext == ".docx")
{
RichEditDocumentServer server = new RichEditDocumentServer();
server.LoadDocument("document.doc", DocumentFormat.Doc);
server.ExportToPdf(memoryStream);
Application applicationclass = new Application();
string[] crefids = filename1.Split('.');
for (int mj = 0; mj < crefids.Length; mj++)
{
randomName = crefids[0].ToString();
}
object Source = fileName;
object Target = Server.MapPath("~/Temp/" + randomName + ".txt");
fname = Target.ToString();
applicationclass.Documents.Open(ref Source,
ref readOnly,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref isVisible,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType);
applicationclass.Visible = false;
Document document = applicationclass.ActiveDocument;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatUnicodeText;
document.SaveAs(ref Target, ref format, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType, ref missingType, ref missingType,
ref missingType);
document.Close(ref missingType, ref missingType, ref missingType);
foreach (string str in str1)
{
using (StreamReader sr = new StreamReader(fname))
{
if (string.IsNullOrEmpty(str) == false)
{
string szReadAll = sr.ReadToEnd().ToLower();
if (Regex.IsMatch(szReadAll, str.ToLower()))
{
if (!ids.Contains(id))
{
ids[mn] = id;
}
Session["ids"] = ids;
}
}
}
}
}
else if (ext == ".pdf")
{
string randomName1 = DateTime.Now.Ticks.ToString();
string fname1 = "";
object Target1 = Server.MapPath("~/Temp/" + randomName1 + ".txt");
fname1 = Target1.ToString();
PDDocument doc = PDDocument.load(fileName);
PDFTextStripper stripper = new PDFTextStripper();
string s = stripper.getText(doc).ToLower();
System.IO.StreamWriter LogFile = new System.IO.StreamWriter(fname1, true);
LogFile.WriteLine(s);
LogFile.Close();
foreach (string str in str1)
{
using (StreamReader sr = new StreamReader(fname1))
{
if (string.IsNullOrEmpty(str) == false)
{
string szReadAll = sr.ReadToEnd().ToLower();
if (Regex.IsMatch(szReadAll, str.ToLower()))
{
if (!ids.Contains(id))
{
ids[mn] = id;
}
Session["ids"] = ids;
}
}
}
}
}
mn++;
}
}
[Edit]Code block added[/Edit]