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.awt.Color;
|
---|
22 | import java.awt.Graphics;
|
---|
23 | import java.awt.Image;
|
---|
24 | import java.awt.Point;
|
---|
25 | import java.awt.Rectangle;
|
---|
26 | import java.awt.image.BufferedImage;
|
---|
27 | import java.io.File;
|
---|
28 | import java.io.IOException;
|
---|
29 | import java.io.RandomAccessFile;
|
---|
30 | import java.nio.ByteBuffer;
|
---|
31 | import java.nio.channels.FileChannel;
|
---|
32 |
|
---|
33 | import javax.imageio.ImageIO;
|
---|
34 |
|
---|
35 | import org.expeditee.gui.Browser;
|
---|
36 | import org.expeditee.gui.Frame;
|
---|
37 | import org.expeditee.gui.FrameCreator;
|
---|
38 | import org.expeditee.gui.FrameGraphics;
|
---|
39 | import org.expeditee.gui.FrameIO;
|
---|
40 | import org.expeditee.gui.MessageBay;
|
---|
41 | import org.expeditee.items.Item;
|
---|
42 | import org.expeditee.items.Text;
|
---|
43 |
|
---|
44 | import com.sun.pdfview.PDFFile;
|
---|
45 | import com.sun.pdfview.PDFPage;
|
---|
46 |
|
---|
47 | public class pdfImporter implements FileImporter {
|
---|
48 |
|
---|
49 | public Item importFile(final File f, Point location) throws IOException {
|
---|
50 | if (location == null || f == null) {
|
---|
51 | return null;
|
---|
52 | }
|
---|
53 |
|
---|
54 | final int x = 0;
|
---|
55 | final int y = 60;
|
---|
56 | final int width = Browser._theBrowser.getSize().width;
|
---|
57 | System.out.println(width);
|
---|
58 |
|
---|
59 | final String name = FrameIO.ConvertToValidFramesetName(f.getName().substring(0,f.getName().lastIndexOf('.'))).toLowerCase();
|
---|
60 |
|
---|
61 | //check if the file is a pdf
|
---|
62 | if(!f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf('.')+1).toLowerCase().equals("pdf"))
|
---|
63 | {
|
---|
64 | return null;
|
---|
65 | }
|
---|
66 |
|
---|
67 | final Text link = FrameDNDTransferHandler.importString(name, location);
|
---|
68 | link.setLink(name+"1");
|
---|
69 |
|
---|
70 | final Frame frameset;
|
---|
71 | try
|
---|
72 | {
|
---|
73 | frameset = FrameIO.CreateNewFrameset(name);
|
---|
74 | }
|
---|
75 | catch(Exception e)
|
---|
76 | {
|
---|
77 | MessageBay.displayMessage("Frameset \"" + name + "\" already exists, creating a link to it");
|
---|
78 | return link;//if the frameset exists just make a link to the existing frameset
|
---|
79 | }
|
---|
80 |
|
---|
81 | final String framesetPath = frameset.getPath()+name+File.separator;
|
---|
82 |
|
---|
83 | System.out.println("PATH = " + framesetPath);
|
---|
84 |
|
---|
85 | new Thread() {
|
---|
86 | public void run() {
|
---|
87 | try {
|
---|
88 | MessageBay.displayMessage("Importing " + f.getName() + "...");
|
---|
89 | //load a pdf from a byte buffer
|
---|
90 | RandomAccessFile raf = new RandomAccessFile(f, "r");
|
---|
91 | FileChannel channel = raf.getChannel();
|
---|
92 | ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
|
---|
93 | PDFFile pdffile = new PDFFile(buf);
|
---|
94 | int pages = pdffile.getNumPages();
|
---|
95 |
|
---|
96 | Frame currentFrame=frameset;
|
---|
97 | Frame _currentFrame=null;
|
---|
98 | Text nextButton=null, prevButton=null;
|
---|
99 | final float spacing=((Text)FrameCreator.createButton("Next", null, null, 10F, 10F)).getBoundsWidth() + 30F;
|
---|
100 |
|
---|
101 | //make images from the pdf pages, write frames with those images
|
---|
102 | for(int i=1; i<=pages; i++)
|
---|
103 | {
|
---|
104 | //remove the title from the frame
|
---|
105 | /* if(FrameDNDTransferHandler.modifier!=1 &&
|
---|
106 | currentFrame!=null &&
|
---|
107 | currentFrame.getTitleItem()!=null)
|
---|
108 | ((Text)currentFrame.getTitleItem()).delete(); */
|
---|
109 | //get the pdf page
|
---|
110 | PDFPage page = pdffile.getPage(i);
|
---|
111 | //get the width and height for the page
|
---|
112 | int w=(int)page.getBBox().getWidth(), h=(int)page.getBBox().getHeight();
|
---|
113 | final double res=1080;
|
---|
114 | if(w<res || h<res)
|
---|
115 | {
|
---|
116 | double ws=res/w;
|
---|
117 | double hs=res/h;
|
---|
118 | if(ws>hs)
|
---|
119 | {
|
---|
120 | w*=ws;
|
---|
121 | h*=ws;
|
---|
122 | }
|
---|
123 | else
|
---|
124 | {
|
---|
125 | w*=hs;
|
---|
126 | h*=hs;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | //generate the image
|
---|
130 | Image img = page.getImage(w, h, //width & height
|
---|
131 | new Rectangle(0, 0, (int)page.getBBox().getWidth(), (int)page.getBBox().getHeight()), //clip rect
|
---|
132 | null, //null for the ImageObserver
|
---|
133 | true, //fill background with white
|
---|
134 | true //block until drawing is done
|
---|
135 | );
|
---|
136 | //Create a buffered image to store the image in
|
---|
137 | BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
|
---|
138 | Graphics g = bimg.createGraphics();
|
---|
139 | //Paint the image onto the buffered image
|
---|
140 | g.drawImage(img, 0, 0, null);
|
---|
141 | g.dispose();
|
---|
142 | //save it as a file
|
---|
143 | File out = new File(framesetPath+i+".png");
|
---|
144 | ImageIO.write(bimg, "png", out);
|
---|
145 | //generate a frame with that image
|
---|
146 | System.out.println(width);
|
---|
147 | System.out.println("@i: "+framesetPath+i+".png "+width);
|
---|
148 | currentFrame.addText(x, y, "@i: "+framesetPath+i+".png "+width, null);
|
---|
149 | if(i>1)
|
---|
150 | {
|
---|
151 | //put a next button on the previous frame (points to current frame)
|
---|
152 | nextButton=(Text)FrameCreator.createButton("Next", null, null, 10F, 10F);
|
---|
153 | nextButton.setID(_currentFrame.getNextItemID());
|
---|
154 | nextButton.addAction("next");
|
---|
155 | _currentFrame.addItem(nextButton);
|
---|
156 | //put a previous button on the current frame (points to previous frame)
|
---|
157 | prevButton=(Text)FrameCreator.createButton("Prev", null, null, (i<pages)?spacing:10F, 10F);
|
---|
158 | prevButton.setID(currentFrame.getNextItemID());
|
---|
159 | prevButton.addAction("previous");
|
---|
160 | currentFrame.addItem(prevButton);
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | prevButton=(Text)FrameCreator.createButton("Home", null, null, (i<pages)?spacing:10F, 10F);
|
---|
165 | prevButton.setID(currentFrame.getNextItemID());
|
---|
166 | prevButton.addAction("GotoHome");
|
---|
167 | currentFrame.addItem(prevButton);
|
---|
168 | }
|
---|
169 | FrameIO.SaveFrame(_currentFrame,true);
|
---|
170 | _currentFrame=currentFrame;
|
---|
171 | if(i<pages) currentFrame = FrameIO.CreateFrame(frameset.getFramesetName(), name, null);
|
---|
172 | }
|
---|
173 | prevButton=(Text)FrameCreator.createButton("Home", null, null, spacing, 10F);
|
---|
174 | prevButton.setID(currentFrame.getNextItemID());
|
---|
175 | prevButton.addAction("gotohome");
|
---|
176 | currentFrame.addItem(prevButton);
|
---|
177 | FrameIO.SaveFrame(currentFrame,true);
|
---|
178 | MessageBay.displayMessage(f.getName() + " import complete", Color.GREEN);
|
---|
179 | FrameGraphics.requestRefresh(true);
|
---|
180 | } catch (Exception e) {
|
---|
181 | e.printStackTrace();
|
---|
182 | MessageBay.errorMessage(e.getMessage());
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }.start();
|
---|
186 | FrameGraphics.refresh(true);
|
---|
187 | //return source;
|
---|
188 | return link;
|
---|
189 | }
|
---|
190 | }
|
---|