]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.help.base/src/org/simantics/help/base/internal/PDFUtil.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.help.base / src / org / simantics / help / base / internal / PDFUtil.java
1 package org.simantics.help.base.internal;
2
3 import java.io.File;
4 import java.io.IOException;
5
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;
13
14 /**
15  * @author Tuukka Lehtonen
16  */
17 public class PDFUtil {
18
19     public static void stripText(File fromPdf, ISearchDocument doc) throws IOException {
20         PDFParser parser = new PDFParser(new RandomAccessFile(fromPdf, "r"));
21         parser.parse();
22
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();
33                 if (title != null)
34                     doc.setTitle(title);
35                 if (subject != null)
36                     doc.setSummary(subject);
37                 doc.addContents(text);
38             }
39         }
40     }
41
42 }