]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ImportPdfReader.java
Cache PropertyInfoRequests in getStandardChildDomainPropertyVariables
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / pdf / ImportPdfReader.java
1 package org.simantics.export.core.pdf;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import com.lowagie.text.pdf.PRStream;
10 import com.lowagie.text.pdf.PdfArray;
11 import com.lowagie.text.pdf.PdfDictionary;
12 import com.lowagie.text.pdf.PdfName;
13 import com.lowagie.text.pdf.PdfReader;
14 import com.lowagie.text.pdf.PdfString;
15
16 public class ImportPdfReader {
17
18         /** File if opened from file */
19         public File file;
20         
21         /** iText pdf reader */
22         public PdfReader reader;
23         
24         List<Attachment> attachments;
25         
26         public ImportPdfReader() {              
27         }
28         
29         public ImportPdfReader(File file) throws IOException {
30                 this.file = file;
31                 reader = new PdfReader(file.getAbsolutePath());
32         }
33
34         public List<Attachment> getAttachments() {
35                 if ( attachments != null ) return attachments;          
36                 attachments = new ArrayList<Attachment>();
37                 PdfDictionary root = reader.getCatalog();
38                 PdfDictionary names = (PdfDictionary)PdfReader.getPdfObject(root.get(PdfName.NAMES));
39                 if (names == null)
40                         return attachments;
41                 PdfDictionary embeddedfiles = names.getAsDict(PdfName.EMBEDDEDFILES);
42                 if (embeddedfiles == null)
43                         return attachments;
44                 PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
45                 if (filespecs == null)
46                         return attachments;
47                 for (int i = 0; i < filespecs.size();) {
48                         Attachment a = new Attachment();
49                         filespecs.getAsString(i++);
50                         a.filespec = filespecs.getAsDict(i++);
51                         a.refs = a.filespec.getAsDict(PdfName.EF);
52                         for (Object _key : a.refs.getKeys()) {
53                                 a.key = (PdfName) _key;
54                                 a.filename = a.filespec.getAsString(a.key);
55                                 attachments.add( a );
56                         }
57                 }               
58                 return attachments;
59         }
60         
61         public List<Attachment> getAttachments(String extension) {
62                 ArrayList<Attachment> result = new ArrayList<Attachment>();
63                 for ( Attachment a : getAttachments() ) {
64                         if ( a.getFilename().endsWith(extension) ) {
65                                 result.add( a );
66                         }
67                 }
68                 return result;
69         }
70         
71         public void close() {
72                 if ( reader!=null ) {
73                         reader.close();
74                         reader = null;
75                 }
76         }
77
78         public static class Attachment {
79                 PdfDictionary filespec;
80                 PdfDictionary refs;
81                 PdfName key;
82                 PdfString filename;
83                 
84                 public String getFilename() {
85                         return filename.toUnicodeString();
86                 }
87                 
88                 public void saveTo(File file) throws IOException {
89                         FileOutputStream fos;
90                         PRStream stream;
91                         if ( !file.exists() ) file.createNewFile();
92                         fos = new FileOutputStream(file); 
93                         try {
94                                 stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
95                                 fos.write(PdfReader.getStreamBytes(stream));
96                                 fos.flush();
97                         } finally {
98                                 fos.close();
99                         }
100                 }
101                 
102         }
103         
104 }