1 | /**
|
---|
2 | * pdfImporter.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.importer;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.io.RandomAccessFile;
|
---|
24 | import java.nio.ByteBuffer;
|
---|
25 | import java.nio.channels.FileChannel;
|
---|
26 |
|
---|
27 | import org.expeditee.core.Colour;
|
---|
28 | import org.expeditee.core.Image;
|
---|
29 | import org.expeditee.core.Point;
|
---|
30 | import org.expeditee.gio.DragAndDropManager;
|
---|
31 | import org.expeditee.gio.EcosystemManager;
|
---|
32 | import org.expeditee.gio.GraphicsManager;
|
---|
33 | import org.expeditee.gui.DisplayController;
|
---|
34 | import org.expeditee.gui.Frame;
|
---|
35 | import org.expeditee.gui.FrameCreator;
|
---|
36 | import org.expeditee.gui.FrameGraphics;
|
---|
37 | import org.expeditee.gui.FrameIO;
|
---|
38 | import org.expeditee.gui.MessageBay;
|
---|
39 | import org.expeditee.items.Item;
|
---|
40 | import org.expeditee.items.Text;
|
---|
41 |
|
---|
42 | import com.sun.pdfview.PDFFile;
|
---|
43 | import com.sun.pdfview.PDFPage;
|
---|
44 |
|
---|
45 | public class pdfImporter implements FileImporter {
|
---|
46 |
|
---|
47 | public Item importFile(final File f, Point location) throws IOException {
|
---|
48 | if (location == null || f == null) {
|
---|
49 | return null;
|
---|
50 | }
|
---|
51 |
|
---|
52 | final int x = 0;
|
---|
53 | final int y = 60;
|
---|
54 | final int width = EcosystemManager.getGraphicsManager().getWindowSize().width;
|
---|
55 | System.out.println(width);
|
---|
56 |
|
---|
57 | final String name = FrameIO.ConvertToValidFramesetName(f.getName().substring(0,f.getName().lastIndexOf('.'))).toLowerCase();
|
---|
58 |
|
---|
59 | //check if the file is a pdf
|
---|
60 | if(!f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf('.')+1).toLowerCase().equals("pdf"))
|
---|
61 | {
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 |
|
---|
65 | final Text link = DragAndDropManager.importString(name, location);
|
---|
66 | link.setLink(name+"1");
|
---|
67 |
|
---|
68 | final Frame frameset;
|
---|
69 | try
|
---|
70 | {
|
---|
71 | frameset = FrameIO.CreateNewFrameset(name);
|
---|
72 | }
|
---|
73 | catch(Exception e)
|
---|
74 | {
|
---|
75 | MessageBay.displayMessage("Frameset \"" + name + "\" already exists, creating a link to it");
|
---|
76 | return link;//if the frameset exists just make a link to the existing frameset
|
---|
77 | }
|
---|
78 |
|
---|
79 | final String framesetPath = frameset.getPath()+name+File.separator;
|
---|
80 |
|
---|
81 | System.out.println("PATH = " + framesetPath);
|
---|
82 |
|
---|
83 | new Thread() {
|
---|
84 | public void run() {
|
---|
85 | try {
|
---|
86 | MessageBay.displayMessage("Importing " + f.getName() + "...");
|
---|
87 | //load a pdf from a byte buffer
|
---|
88 | RandomAccessFile raf = new RandomAccessFile(f, "r");
|
---|
89 | FileChannel channel = raf.getChannel();
|
---|
90 | ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
|
---|
91 | PDFFile pdffile = new PDFFile(buf);
|
---|
92 | int pages = pdffile.getNumPages();
|
---|
93 |
|
---|
94 | Frame currentFrame=frameset;
|
---|
95 | Frame _currentFrame=null;
|
---|
96 | Text nextButton=null, prevButton=null;
|
---|
97 | final int spacing=((Text)FrameCreator.createButton("Next", null, null, 10, 10)).getBoundsWidth() + 30;
|
---|
98 |
|
---|
99 | //make images from the pdf pages, write frames with those images
|
---|
100 | for(int i=1; i<=pages; i++)
|
---|
101 | {
|
---|
102 | //remove the title from the frame
|
---|
103 | /* if(FrameDNDTransferHandler.modifier!=1 &&
|
---|
104 | currentFrame!=null &&
|
---|
105 | currentFrame.getTitleItem()!=null)
|
---|
106 | ((Text)currentFrame.getTitleItem()).delete(); */
|
---|
107 | //get the pdf page
|
---|
108 | PDFPage page = pdffile.getPage(i);
|
---|
109 | //get the width and height for the page
|
---|
110 | int w=(int)page.getBBox().getWidth(), h=(int)page.getBBox().getHeight();
|
---|
111 | final double res=1080;
|
---|
112 | if(w<res || h<res)
|
---|
113 | {
|
---|
114 | double ws=res/w;
|
---|
115 | double hs=res/h;
|
---|
116 | if(ws>hs)
|
---|
117 | {
|
---|
118 | w*=ws;
|
---|
119 | h*=ws;
|
---|
120 | }
|
---|
121 | else
|
---|
122 | {
|
---|
123 | w*=hs;
|
---|
124 | h*=hs;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | //generate the image
|
---|
128 | Image img = Image.createImage(w, h, page);
|
---|
129 | //Create a buffered image to store the image in
|
---|
130 | Image bimg = Image.createImage(img.getWidth(), img.getHeight(), false);
|
---|
131 | GraphicsManager g = EcosystemManager.getGraphicsManager();
|
---|
132 | //Paint the image onto the buffered image
|
---|
133 | g.pushDrawingSurface(bimg);
|
---|
134 | g.drawImage(img, new Point(0, 0));
|
---|
135 | g.popDrawingSurface();
|
---|
136 | //save it as a file
|
---|
137 | File out = new File(framesetPath+i+".png");
|
---|
138 | bimg.writeToDisk("png", out);
|
---|
139 | //generate a frame with that image
|
---|
140 | System.out.println(width);
|
---|
141 | System.out.println("@i: "+framesetPath+i+".png "+width);
|
---|
142 | currentFrame.addText(x, y, "@i: "+framesetPath+i+".png "+width, null);
|
---|
143 | if(i>1)
|
---|
144 | {
|
---|
145 | //put a next button on the previous frame (points to current frame)
|
---|
146 | nextButton=(Text)FrameCreator.createButton("Next", null, null, 10, 10);
|
---|
147 | nextButton.setID(_currentFrame.getNextItemID());
|
---|
148 | nextButton.addAction("next");
|
---|
149 | _currentFrame.addItem(nextButton);
|
---|
150 | //put a previous button on the current frame (points to previous frame)
|
---|
151 | prevButton=(Text)FrameCreator.createButton("Prev", null, null, (i<pages)?spacing:10, 10);
|
---|
152 | prevButton.setID(currentFrame.getNextItemID());
|
---|
153 | prevButton.addAction("previous");
|
---|
154 | currentFrame.addItem(prevButton);
|
---|
155 | }
|
---|
156 | else
|
---|
157 | {
|
---|
158 | prevButton=(Text)FrameCreator.createButton("Home", null, null, (i<pages)?spacing:10, 10);
|
---|
159 | prevButton.setID(currentFrame.getNextItemID());
|
---|
160 | prevButton.addAction("GotoHome");
|
---|
161 | currentFrame.addItem(prevButton);
|
---|
162 | }
|
---|
163 | FrameIO.SaveFrame(_currentFrame,true);
|
---|
164 | _currentFrame=currentFrame;
|
---|
165 | if(i<pages) currentFrame = FrameIO.CreateFrame(frameset.getFramesetName(), name, null);
|
---|
166 | }
|
---|
167 | prevButton=(Text)FrameCreator.createButton("Home", null, null, spacing, 10);
|
---|
168 | prevButton.setID(currentFrame.getNextItemID());
|
---|
169 | prevButton.addAction("gotohome");
|
---|
170 | currentFrame.addItem(prevButton);
|
---|
171 | FrameIO.SaveFrame(currentFrame,true);
|
---|
172 | MessageBay.displayMessage(f.getName() + " import complete", Colour.GREEN);
|
---|
173 | DisplayController.requestRefresh(true);
|
---|
174 | } catch (Exception e) {
|
---|
175 | e.printStackTrace();
|
---|
176 | MessageBay.errorMessage(e.getMessage());
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }.start();
|
---|
180 | DisplayController.requestRefresh(true);
|
---|
181 | //return source;
|
---|
182 | return link;
|
---|
183 | }
|
---|
184 | }
|
---|