Showing posts with label Text to word. Show all posts
Showing posts with label Text to word. Show all posts

Monday, 6 August 2012

Extract Text from Word File with C#, VB.NET

In my last article, I introduce a pretty easy way to extract images from word fileToday, I will share another method to extract text from word document with C#, VB.NET. In our daily work, word document always plays an indispensable role. It has powerful functions to edit the text with different formats, such as font, header and footer, comments, hyperlink and so on. At the same time, it is just because this reason, we need to extract only the content without its format in a .txt file, while not using document.SaveToFile method to save Word as Text directly.

Easy way to extract text from word document with C#, VB.NET

I am very happy that Spire.Doc, an MS Word component, can be my best hand to finish this task. Using Spire.Doc, I only need three simple steps to realize the text extraction function. If necessary,you can use the source code to freely download it.
Procedure

Step1. Create a new project

1.     Create a new project in Visual Studio and set its Target framework to be .NET Framework 4.

2.     Add Spire.Doc DLL as reference.

3.     Add below using at the top of the method.
C#
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;

VB.NET
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents

Step2. Extract text from word document with C#, VB.NET

1.     Load a Word document from system.

C# CODE:
         Document doc = new Document();
         doc.LoadFromFile(@"D:\michelle\JaneEyre.doc", FileFormat.Doc);
VB.NET CODE:
         Dim doc As New Document()    
         doc.LoadFromFile("D:\michelle\JaneEyre.doc", FileFormat.Doc)

2.     Extract text from word document.
C# Code:
            //new a stringBuilder to extract text from word document            StringBuilder sb = new StringBuilder();                        //extract text from word document            foreach (Section section in doc.Sections)
            {
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    sb.AppendLine(paragraph.Text);
                }
            }

VB.NET Code:

                  'new a stringBuilder to extract text from word document                 Dim sb As New StringBuilder()
                  'extract text from word document                 For Each section As Section In doc.Sections
                     For Each paragraph As Paragraph In section.Paragraphs
                          sb.AppendLine(paragraph.Text)
                       Next
                   Next

Step3. Save the text to a .txt file and launch the .txt file.

C# Code:
            //write the text of word document into a txt file            File.WriteAllText(@"result.txt", sb.ToString());
            //launch the text file            System.Diagnostics.Process.Start(@"result.txt");
VB.NET Code:

         'write the text of word document into a txt file         File.WriteAllText("result.txt", sb.ToString())
           'launch the text file         System.Diagnostics.Process.Start("result.txt")

Code Source:  

Sunday, 5 August 2012

Three Steps to Save Html File as Word Document with C#, VB.NET

Introduction
Html format has its specified character which cannot be replaced by other formats. So we often need to convert one format to multiple formats. Thus, the conversion software or applications with the conversion function is necessary. For word application Spire.Doc users, it not only can save word as different formats but also can be converted by other formats, such as html and xml. Today, I am glad to share with the method how to save  html as word with C#,VB.NET.
You may say that you do not need to save html as word with C#/VB.NET since you can use the copy and paste function, or you can right click the html file to edit it and then it automatically opened as word document. Both ways can realize the conversion. In some sense, you are right. However, for some basic and simple html files without image, you can easily do that through the methods above. But for those have images and a little complex, that measure is not available. When you edit the html, CSS is very likely to be ignored. So, using the method of save html as word with C#,VB.NET becomes a best and most reliable mean. I use Spire.Doc to help me finish this task in a fast way.


Procedure

Step1. Load a html file from the system.

Step2. Convert HTML to Word.

Step3. Preview the effect.


Main Code:
C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
namespace html_to_word
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"E:\TestScript\Sample.html", FileFormat.Html, XHTMLValidationType.None);
            document.SaveToFile("test.doc", FileFormat.Doc);
        }
    }
}
 
VB.NET Code:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace Html2Doc
        Friend Class Program
               Shared Sub Main(ByVal args() As String)
                       Dim document As New Document()
                                      document.LoadFromFile("E:\TestScript\Sample.html",FileFormat.Html,XHTMLValidationType.None)
                       document.SaveToFile("test.doc", FileFormat.Doc)
               End Sub
        End Class
End Namespace

Preview

                   
From the above procedure, you can obviously see that using Spire.Doc to save html as word with C#,VB.NET is not more complex than copy and paste function and edit methods. It is the most effective way to help people realize the conversion function. Not only html to word and xml to word conversion, but also word to epub, html, xml, tiff, rtf, tiff image, image and so on.


Saturday, 4 August 2012

State Management in ASP.NET part-1 (Cookies)

Hi,

I am Back with State Management Series.
In this post we are going to Talk about Cookies. After Reading this, you must be able to do following things:

  • Create Cookie
  • Access Cookie
  • Can answer Some Basic Q & A about Cookie
What is Need of Cookie?
Cookies are Required to Store Temporary data in Users system which may be Required during Session or after Session.
How Many Type of Cookie are there?
Cookies are generally of Two Type. Session wide or Persistent. Sessional cookies Expire as soon as User Closed Session means Browser. Persistent cookies expire after Defined Time. This can be Defined at the Time of Creation.
Who Make Cookies?
Cookie is always made by Application (Server) in Users/ Clients Browser and stored in Hard Disk of User. These are used to store Users Customized Appearance, other Values , Login Time etc for future Retrieval so that website can server him better.
Limitations of Cookie?
  • Cookies are open Invitations to Hackers.
  • Cookies can only store Little and Only String Type Data.
Now Lets Learn with Coding. First We will Create a Cookie named myCookie and Solve Earlier Problem with the Help of Cookie.

Now Code is Like Below:
 protected void Page_Load(object sender, EventArgs e)
    {

    }
    int x;    // Made a Variable with Datatype Int for Future Use
    HttpCookie myCookie;   // Defined a Cookie named myCookie.
    protected void Button1_Click(object sender, EventArgs e)
    { 
        x = Convert.ToInt32(TextBox1.Text);  // Converted textbox Value which we earlier Did.
        myCookie = new HttpCookie("txtValue");  // Assigned Variable myCookie.
        myCookie.Value = Convert.ToString(x);   // Value assigned to myCookie.
        Response.Cookies.Add(myCookie); //This Simple Made a Cookie that will Expire in Session End.
        myCookie.Expires = DateTime.Now.AddMinutes(15);  // We made is Persistent and stored it for Next 15 minutes.
      
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
       
        HttpCookie storedCook = Request.Cookies["txtValue"];  // Requested from Server for Cookie
        if (storedCook != null)  
            x = Convert.ToInt32(storedCook.Value);  // Converted to Integer again.
        TextBox1.Text = Convert.ToString(x * x);    // Now Printed Value in Textbox.
    }
Now Check....
Cool na, I'll be waiting for Feedback.

Popular posts