KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > extractor > PDFExtractor


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/extractor/PDFExtractor.java,v 1.1.2.1 2004/09/29 15:01:26 unico Exp $
3  * $Revision: 1.1.2.1 $
4  * $Date: 2004/09/29 15:01:26 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.extractor;
25
26 import org.pdfbox.util.PDFTextStripper;
27 import org.pdfbox.pdfparser.PDFParser;
28 import org.pdfbox.pdmodel.PDDocument;
29
30 import java.io.*;
31
32 /**
33  * Author: Ryan Rhodes
34  * Date: Jun 26, 2004
35  * Time: 4:03:00 AM
36  */

37 public class PDFExtractor extends AbstractContentExtractor
38 {
39
40     public PDFExtractor(String JavaDoc uri, String JavaDoc contentType, String JavaDoc namespace)
41     {
42         super(uri, contentType, namespace);
43     }
44
45     public Reader extract(InputStream content) throws ExtractorException
46     {
47         try
48         {
49             PDFParser parser = new PDFParser( content );
50             parser.parse();
51
52             PDDocument document = parser.getPDDocument();
53
54             CharArrayWriter writer = new CharArrayWriter();
55
56             PDFTextStripper stripper = new PDFTextStripper();
57             stripper.setLineSeparator("\n");
58             stripper.writeText(document, writer);
59
60             document.close();
61             writer.close();
62
63             return new CharArrayReader(writer.toCharArray());
64         }
65         catch(Exception JavaDoc e )
66         {
67             throw new ExtractorException(e.getMessage());
68         }
69     }
70
71     public static void main(String JavaDoc[] args) throws Exception JavaDoc
72     {
73         FileInputStream in = new FileInputStream(args[0]);
74
75         PDFExtractor ex = new PDFExtractor(null, null, null);
76
77         Reader reader = ex.extract(in);
78
79         int c = 0;
80         do
81         {
82             c = reader.read();
83             System.out.print((char)c);
84         }
85         while(c != -1);
86     }
87 }
88
Popular Tags