package info.bukova.isspst.services.fulltext;

import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Pepa Rokos
 */
public class PdfExtractor extends AbstractExtractor implements Extractor {

	@Override
	public String extract(InputStream is) throws ExtractorException {
		try {
			PdfReader reader = new PdfReader(is);
			PdfTextExtractor extractor = new PdfTextExtractor(reader);
			StringBuilder sb = new StringBuilder();

			for (int i = 1; i <= reader.getNumberOfPages(); i++) {
				sb.append(extractor.getTextFromPage(i));
			}

			return sb.toString();

		} catch (IOException e) {
			throw new ExtractorException(e);
		}
	}

}
