Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:37:23+00:00 2026-06-02T17:37:23+00:00

The strings I’m (programmatically) getting from MS Word files when using Apache POI are

  • 0

The strings I’m (programmatically) getting from MS Word files when using Apache POI are not the same text I can look at when I open the files with MS Word.

When using the following code:

File someFile = new File("some\\path\\MSWFile.doc");
InputStream inputStrm = new FileInputStream(someFile);
HWPFDocument wordDoc = new HWPFDocument(inputStrm);
System.out.println(wordDoc.getText());

the output is a single line with many ‘invalid’ characters (yes, the ‘boxes’), and many unwanted strings, like “FORMTEXT“, “HYPERLINK \l "_Toc##########"” (‘#’ being numeric digits), “PAGEREF _Toc########## \h 4“, etc.

The following code “fixes” the single-line problem, but maintains all the invalid characters and unwanted text:

File someFile = new File("some\\path\\MSWFile.doc");
InputStream inputStrm = new FileInputStream(someFile);
WordExtractor wordExtractor = new WordExtractor(inputStrm);
for(String paragraph:wordExtractor.getParagraphText()){
  System.out.println(paragraph);
}

I don’t know if I’m using the wrong method for extracting the text, but that’s what I’ve come up with when looking at POI’s quick-guide. If I am, what is the correct approach?

If that output is correct, is there a standard way for getting rid of the unwanted text, or will I have to write a filter of my own?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:37:25+00:00Added an answer on June 2, 2026 at 5:37 pm

    There are two options, one provided directly in Apache POI, the other via Apache Tika (which uses Apache POI internally).

    The first option is to use WordExtractor, but wrap it in a call to stripFields(String) when calling it. That will remove the text based fields included in the text, things like HYPERLINK that you’ve seen. Your code would become:

    NPOIFSFileSystem fs = new NPOIFSFileSytem(file);
    WordExtractor extractor = new WordExtractor(fs.getRoot());
    
    for(String rawText : extractor.getParagraphText()) {
    String text = extractor.stripFields(rawText);
    System.out.println(text);
    }
    

    The other option is to use Apache Tika. Tika provides text extraction, and metadata, for a wide variety of files, so the same code will work for .doc, .docx, .pdf and many others too. To get clean, plain text of your word document (you can also get XHTML if you’d rather), you’d do something like:

    TikaConfig tika = TikaConfig.getDefaultConfig();
    TikaInputStream stream = TikaInputStream.get(file);
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    tika.getParser().parse(input, handler, metadata, new ParseContext());
    String text = handler.toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can my strings have different hash codes but the same text value of
I am comparing two strings using following code string1.Contains(string2) but i am not getting
I usually add some strings from a text file into a list or array
For Strings.. How can i Detect if the string name has the text Hello
I have strings that I download from the web that look like this: משה
Adding strings localization files in iPhone bundle is very simple and perfectly fine but
Are strings compared lexicographical when using the overriden bool operator<(const std::string & rhs) operator?
Using magic strings in C# really unsettles me, so I'm thinking of using the
I'm storing localized strings in a single datatable using MS Sql (2008 or whatever).
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.