Convert the .doc file into .txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geethu03
    New Member
    • Dec 2006
    • 45

    #1

    Convert the .doc file into .txt file

    Hello
    For my Application i want to convert .doc file (MS Word) into the
    .txt file (Text). All of as says to use the (file) Save As method to do it. But i want to convert the file in Unix Using java. How is it possible, help to solve this.
    Is there is any predifined tool to do this.

    Thanks in Advance,
    Geethu.
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    You would need to find a description of exactly how .doc files are stored (which is no simple matter), and then remove all the tags around the text.....

    I'm not aware of any really simple way to do it, although you may find someone on the broader net has a public solution, because it seems to be a problem that comes up quite often....

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Geethu03
      Hello
      For my Application i want to convert .doc file (MS Word) into the
      .txt file (Text). All of as says to use the (file) Save As method to do it. But i want to convert the file in Unix Using java. How is it possible, help to solve this.
      Is there is any predifined tool to do this.

      Thanks in Advance,
      Geethu.
      You can only read text portions of the word document into the text file.

      Use the poi package to read the .doc file and write its contents to a file using FileWriter

      Comment

      • rupanu
        New Member
        • Mar 2007
        • 3

        #4
        Originally posted by r035198x
        You can only read text portions of the word document into the text file.

        Use the poi package to read the .doc file and write its contents to a file using FileWriter
        Hi r035198x,
        I have just joined this forum..
        I am using POI for converting doc to txt...but it reads/wites only first line of the text....I don't know where its going wrong...Can u help me out with some code snippets?
        Thanx rupanu

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          hello newbie ....

          what is POI ??????

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by rupanu
            Hi r035198x,
            I have just joined this forum..
            I am using POI for converting doc to txt...but it reads/wites only first line of the text....I don't know where its going wrong...Can u help me out with some code snippets?
            Thanx rupanu
            Post your code so we can take a look at it.

            Comment

            • rupanu
              New Member
              • Mar 2007
              • 3

              #7
              hi there,
              After trying with a few code snippets I found the following code working,but how do I read tables in the document???

              //CODE

              FileInputStream fis=new FileInputStream (filename);

              HWPFDocument doc=new HWPFDocument(fi s);

              String str="";

              Object[] textStack=doc.g etTextTable().g etTextPieces(). toArray();

              for (int i=0; i<textStack.len gth; i++)
              {
              if(str=="")
              str=((TextPiece )textStack[i]).getStringBuff er().toString() ;
              else
              str=str+" "+((TextPiece)t extStack[i]).getStringBuff er().toString() ;

              }
              //System.out.prin tln(str);
              FileWriter fw=new FileWriter("F:\ \newdoc.txt");

              fw.write(str);

              fw.close();
              ///END of CODE

              Please reply....

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by rupanu
                hi there,
                After trying with a few code snippets I found the following code working,but how do I read tables in the document???

                //CODE

                FileInputStream fis=new FileInputStream (filename);

                HWPFDocument doc=new HWPFDocument(fi s);

                String str="";

                Object[] textStack=doc.g etTextTable().g etTextPieces(). toArray();

                for (int i=0; i<textStack.len gth; i++)
                {
                if(str=="")
                str=((TextPiece )textStack[i]).getStringBuff er().toString() ;
                else
                str=str+" "+((TextPiece)t extStack[i]).getStringBuff er().toString() ;

                }
                //System.out.prin tln(str);
                FileWriter fw=new FileWriter("F:\ \newdoc.txt");

                fw.write(str);

                fw.close();
                ///END of CODE

                Please reply....
                You will have to check the documentation for HWPF for that.
                You should also consider using wrapping the FileWriter in a BufferedWriter

                Comment

                • rupanu
                  New Member
                  • Mar 2007
                  • 3

                  #9
                  Hello r035198x
                  The above code is unable to read openoffice.org documents i.e Open Document Text(.odt) format files.Can You pleas tell me how to read odt documents,I mean are there other APIs available for that?Please reply....

                  Comment

                  • 989898
                    New Member
                    • Sep 2007
                    • 4

                    #10
                    Hi,
                    i have seen ur code which is very interesting,her e i would like to know
                    What is Role of "Text Piece". i am getting an erro saying that it can't be resolved to type.
                    can u help me here, i know it is hard to ask u coz it is posted long back

                    thank u
                    vijay

                    Comment

                    • 989898
                      New Member
                      • Sep 2007
                      • 4

                      #11
                      hi as per the code here i have just copied and done the same thing but iam getting an errorlike this

                      ava.io.FileNotF oundException: D:\123\12345 (The system cannot find the file specified)
                      at java.io.FileInp utStream.open(N ative Method)
                      at java.io.FileInp utStream.<init> (Unknown Source)
                      at java.io.FileInp utStream.<init> (Unknown Source)
                      at com.poi.Testwor d.main(Testword .java:15)
                      but i have that file in that palce.can u help me here

                      vijay

                      Comment

                      Working...