I am looking for C# code that will read document file (.doc/.docx) with all formatted words including images, bullets, font with bold/italic/underline, table, header & footer and so on.
Please Help me....
Thanks In Advance.
Know the answer? Post it. Somebody with the same question will find it here.
Dhirendra MisraRank #4,173 · 360 reputationPosted Jan 8, 2014, 11:21 PM EST
Refer the links:
http://www.codeproject.com/Articles/3959/Microsoft-Word-Documents-from-ASP-NET
http://www.mindstick.com/Articles/5cd1b721-9b94-4ea0-bd6e-2bb157401069/
Priti KumariRank #414 · 6K reputationPosted Jan 9, 2014, 12:00 AM EST
Dhirendra MisraRank #4,173 · 360 reputationPosted Jan 9, 2014, 7:22 PM EST
I came across this link which mention why format goes off while copying the content from source to target file.
http://shaunakelly.com/word/styles/formatoftextchanges.html
I think you would need to set format while copying the data. I understand it is not exact solution but you would need to take some other approach.
http://www.c-sharpcorner.com/UploadFile/mgold/CreatingandOpeningMicrosoftDocumentfrom.NETUsingCSharp11262005050939AM/CreatingandOpeningMicrosoftDocumentfrom.NETUsingCSharp.aspx
arron lee42 reputationPosted May 25, 2014, 7:07 PM EDT
Here are some C# code I have ever used to read Word documents in C#.NET platform:
using System.IO; using System.Drawing.Printing; using Yiigo.Imaging; using Yiigo.Imaging.Processing; using Yiigo.Imaging.Word; // load & save a word document YiigoImage doc= new YiigoImage(); doc.load(@"C:\ yiigo_example.docx"); doc.save(@"C:\ yiigo_example.docx"); //Extract text from Word public void ExtractText(); { doc.load(@"C:\ yiigo_example.docx"); doc.PageNumberStart = "0"; doc.PageNumberStop = "4"; doc.OutputFile = OutputFormat.txt; doc.save(@"C:/extract.txt"); }; public void ExtractText(@"C:\ yiigo_example.docx", "0","4", (@"C:/extract.txt")); // print a word document public void Print(); { doc.load(@"C:\ yiigo_example.docx"); doc.PageNumberStart = "0"; doc.PageNumberStop = "4"; Printer = (@"C:/ HP Color LaserJet 5550 PCL 6"); }; Printer. Print(@"C:\ yiigo_example.docx", "0","4", @"C:/ HP Color LaserJet 5550 PCL 6"); Or you can build a Word Doc Viewer with the help of some mannual toolkits which allow users to re-develop them. I hope you success. Good luck. Best regards, ArronKhan Abrar AhmedRank #778 · 3K reputationPosted May 25, 2014, 10:25 PM EDT
u can try like this.
private void ReadMsWord()
{
try
{
//OBJECT OF MISSING "NULL VALUE"
Object miss = System.Reflection.Missing.Value;
//OBJECTS OF FALSE AND TRUE
Object oTrue = true;
object oFalse = false;
Object readOnly = true;
//CREATING OBJECTS OF WORD AND DOCUMENT
Microsoft.Office.Interop.Word.Application oWordApp = new Microsoft.Office.Interop.Word.Application();
// writeLogFile("Document Application Open:::::>> ");
Document docs = null;
// writeLogFile("Document Open:::::>> ");
object path = lblTempFilePathForStream.Text;//ConfigSettings.GetProperty("TempRuleBookFileLoc") + lblFileName.Text;
try
{
if (oWordApp.Documents.Count != 0)
{
oWordApp.Documents.Close(miss, miss, miss);
// writeLogFile("Close any open document:::::>> " );
}
else
{
docs = (Document)oWordApp.Documents.Open(ref path, ref oFalse, ref oTrue, ref oFalse, ref miss, ref miss, ref oTrue, ref miss, ref miss, ref miss, ref miss, ref oFalse, ref oFalse, WdDocumentDirection.wdLeftToRight, ref oTrue, ref miss);
if (docs == null)
writeLogFile("document not open :::::>> ");
else
writeLogFile("Open My document :::::>> " + docs.Name);
}
getParagaphdata(docs);
}
catch (Exception ex)
{
TimeElapsedHandler();
Common.Exceptions.AppException.HandleException(ex);
}
}
catch (Exception ex)
{
TimeElapsedHandler();
Common.Exceptions.AppException.HandleException(ex);
throw ex;
}
}
private void getParagaphdata(Microsoft.Office.Interop.Word.Document docs)
{
try
{
foreach (Microsoft.Office.Interop.Word.Paragraph p in docs.Paragraphs)
{
switch (p.OutlineLevel)
{
case WdOutlineLevel.wdOutlineLevel1:
{
#region Level 1
currentLevel = 1;
if (string.IsNullOrEmpty((p.Range.ListFormat).ListString))
{
}
else
{
}
if (IsDecimal(p.Range.ListFormat.ListString))
{
}
else
{
}
break;
}
case WdOutlineLevel.wdOutlineLevelBodyText:
{
#region Level 10
string text = string.Empty;
text = p.Range.Text;
int count = p.Range.Tables.Count;
string style = ((dynamic)p).style.NameLocal;
if (count == 1)
{
Microsoft.Office.Interop.Word.Table tb = p.Range.Tables[1];
text = checkTableContent(tb, tableString);
}
break;
}
}
}
}
catch (Exception ex)
{
writeLogFile("Document Some Error:::::>> " + ex.ToString());
// Common.Exceptions.AppException.HandleException(0, "Document", ex);
throw ex;
}
}
Anupam SinghRank #643 · 3.7K reputationPosted May 25, 2014, 11:06 PM EDT
You can do everything in ms word,ms excel.. b'coz it provides ms office interop assemblies.
All what you have to do is , go and study ms office interoparability.
if you have office 2010
download it here
http://www.microsoft.com/en-us/download/details.aspx?id=3508#SystemRequirements
Hope it helps
Marina Eberhardt24 reputationPosted Nov 18, 2014, 2:14 AM EST
Hi Priti, try this .NET DLL, it will enable you to process Word document with ease.
Here are some C# samples for DOCX processing and C# samples for DOC processing.
Aishah khan12 reputationPosted Oct 18, 2015, 12:09 PM EDT
Mozart HeRank #7,298 · 244 reputationPosted Oct 18, 2015, 5:34 PM EDT
Kiran K18 reputationPosted Oct 18, 2015, 5:41 PM EDT
float fNumber1 = 12.3f;
Priyaranjan K SRank #45 · 32.5K reputationPosted Oct 18, 2015, 7:09 PM EDT
Kurt Ambrose127 reputationPosted Dec 4, 2020, 4:45 AM EST
Hello Priti,
There is a quick tutorial of how to display files in a Document Viewer using the LEADTOOLS SDK. This would be able to show Microsoft Word Docx files in the viewer.
https://www.leadtools.com/help/sdk/v21/tutorials/viewers/windows/dotnet/winforms/display-files-in-document-viewer.html
Thanks,
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts