1 package org.simantics.help.base.internal;
4 import java.io.IOException;
6 import org.apache.pdfbox.cos.COSDocument;
7 import org.apache.pdfbox.io.RandomAccessFile;
8 import org.apache.pdfbox.pdfparser.PDFParser;
9 import org.apache.pdfbox.pdmodel.PDDocument;
10 import org.apache.pdfbox.pdmodel.PDDocumentInformation;
11 import org.apache.pdfbox.text.PDFTextStripper;
12 import org.eclipse.help.search.ISearchDocument;
15 * @author Tuukka Lehtonen
17 public class PDFUtil {
19 public static void stripText(File fromPdf, ISearchDocument doc) throws IOException {
20 PDFParser parser = new PDFParser(new RandomAccessFile(fromPdf, "r"));
23 try (COSDocument cosDoc = parser.getDocument()) {
24 try (PDDocument pdDoc = new PDDocument(cosDoc)) {
25 int numPages = pdDoc.getNumberOfPages();
26 PDFTextStripper stripper = new PDFTextStripper();
27 stripper.setStartPage(1);
28 stripper.setEndPage(numPages);
29 String text = stripper.getText(pdDoc);
30 PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
31 String title = docInfo.getTitle();
32 String subject = docInfo.getSubject();
36 doc.setSummary(subject);
37 doc.addContents(text);