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