KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > metadata > PdfBoxMetadataExtracter


1 /*
2  * Copyright (C) 2005 Jesper Steen Møller
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.metadata;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.model.ContentModel;
26 import org.alfresco.repo.content.MimetypeMap;
27 import org.alfresco.service.cmr.repository.ContentReader;
28 import org.alfresco.service.namespace.QName;
29 import org.pdfbox.pdmodel.PDDocument;
30 import org.pdfbox.pdmodel.PDDocumentInformation;
31
32 /**
33  *
34  * @author Jesper Steen Møller
35  */

36 public class PdfBoxMetadataExtracter extends AbstractMetadataExtracter
37 {
38     public PdfBoxMetadataExtracter()
39     {
40         super(MimetypeMap.MIMETYPE_PDF, 1.0, 1000);
41     }
42
43     public void extractInternal(ContentReader reader, Map JavaDoc<QName, Serializable JavaDoc> destination) throws Throwable JavaDoc
44     {
45         PDDocument pdf = null;
46         InputStream JavaDoc is = null;
47         try
48         {
49             is = reader.getContentInputStream();
50             // stream the document in
51
pdf = PDDocument.load(is);
52             // Scoop out the metadata
53
PDDocumentInformation docInfo = pdf.getDocumentInformation();
54
55             trimPut(ContentModel.PROP_AUTHOR, docInfo.getAuthor(), destination);
56             trimPut(ContentModel.PROP_TITLE, docInfo.getTitle(), destination);
57             trimPut(ContentModel.PROP_DESCRIPTION, docInfo.getSubject(), destination);
58
59             Calendar JavaDoc created = docInfo.getCreationDate();
60             if (created != null)
61                 destination.put(ContentModel.PROP_CREATED, created.getTime());
62         }
63         finally
64         {
65             if (is != null)
66             {
67                 try { is.close(); } catch (IOException JavaDoc e) {}
68             }
69             if (pdf != null)
70             {
71                 try { pdf.close(); } catch (Throwable JavaDoc e) { e.printStackTrace(); }
72             }
73         }
74     }
75 }
76
Popular Tags