All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.genexus.search.TextWordDocHandler Maven / Gradle / Ivy

Go to download

Core classes for the runtime used by Java and Android apps generated with GeneXus

The newest version!
package com.genexus.search;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

public class TextWordDocHandler implements IDocumentHandler
{
	public String getText(String filename)
	{
		try
		{
			InputStream is = new FileInputStream(filename);
			String bodyText = "";
			try
			{
				if (filename.endsWith(".doc"))
				{
					HWPFDocument doc = new HWPFDocument(is);
					WordExtractor ex = new WordExtractor(doc);
					bodyText = ex.getText();
					ex.close();
				}
				else 
				{						
					XWPFDocument docx = new XWPFDocument(is);
					XWPFWordExtractor ex = new XWPFWordExtractor(docx);
					bodyText = ex.getText();
					ex.close();
				}							
			}
			catch (Exception e)
			{
				System.out.println("Cannot extract text from a Word document" + e.getMessage());
			}
			
			is.close();
			return bodyText;
		}
		catch (IOException ex)
		{

		}
		return "";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy