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