source: trunk/src/org/expeditee/importer/pdfImporter.java@ 1532

Last change on this file since 1532 was 1532, checked in by bnemhaus, 4 years ago

New feature: when creating a frameset, if the item being used is linked and you hold shift, it will use the items on the linked frame to populate the zero frame of the new frameset

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