[919] | 1 | /**
|
---|
| 2 | * PdfFramesetWriter.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 |
|
---|
[226] | 19 | package org.expeditee.io;
|
---|
| 20 |
|
---|
| 21 | import java.awt.Dimension;
|
---|
[286] | 22 | import java.awt.Graphics2D;
|
---|
[226] | 23 | import java.awt.Image;
|
---|
| 24 | import java.io.FileOutputStream;
|
---|
| 25 | import java.io.IOException;
|
---|
[306] | 26 | import java.io.Writer;
|
---|
[226] | 27 | import java.util.Collection;
|
---|
| 28 | import java.util.List;
|
---|
| 29 |
|
---|
| 30 | import org.expeditee.gui.Frame;
|
---|
| 31 | import org.expeditee.gui.FrameGraphics;
|
---|
[286] | 32 | import org.expeditee.items.Circle;
|
---|
[226] | 33 | import org.expeditee.items.Item;
|
---|
[286] | 34 | import org.expeditee.items.Line;
|
---|
[226] | 35 | import org.expeditee.items.Picture;
|
---|
| 36 | import org.expeditee.items.Text;
|
---|
[286] | 37 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
[570] | 38 | import org.expeditee.settings.UserSettings;
|
---|
[226] | 39 |
|
---|
| 40 | import com.lowagie.text.Document;
|
---|
| 41 | import com.lowagie.text.DocumentException;
|
---|
| 42 | import com.lowagie.text.Font;
|
---|
| 43 | import com.lowagie.text.FontFactory;
|
---|
| 44 | import com.lowagie.text.Rectangle;
|
---|
| 45 | import com.lowagie.text.pdf.PdfContentByte;
|
---|
| 46 | import com.lowagie.text.pdf.PdfWriter;
|
---|
| 47 |
|
---|
[282] | 48 | /**
|
---|
| 49 | *
|
---|
| 50 | * @author root
|
---|
[286] | 51 | *
|
---|
[282] | 52 | */
|
---|
| 53 | public class PdfFramesetWriter extends DefaultFramesetWriter {
|
---|
[226] | 54 |
|
---|
| 55 | private Document _pdfDocument;
|
---|
| 56 |
|
---|
| 57 | private PdfWriter _pdfWriter;
|
---|
| 58 |
|
---|
[286] | 59 | private boolean _showFrameNames;
|
---|
| 60 |
|
---|
| 61 | public PdfFramesetWriter(long firstFrame, long maxFrame,
|
---|
| 62 | boolean showFrameNames) {
|
---|
[282] | 63 | super(firstFrame, maxFrame);
|
---|
[226] | 64 | Dimension d = FrameGraphics.getMaxSize();
|
---|
| 65 | _pdfDocument = new Document(new Rectangle(d.width, d.height));
|
---|
[286] | 66 | _showFrameNames = showFrameNames;
|
---|
[226] | 67 | }
|
---|
[286] | 68 |
|
---|
[282] | 69 | public PdfFramesetWriter() {
|
---|
[286] | 70 | this(1, Long.MAX_VALUE, false);
|
---|
[282] | 71 | }
|
---|
[226] | 72 |
|
---|
| 73 | @Override
|
---|
[286] | 74 | protected String getFileName(Frame start) {
|
---|
| 75 | String fileName = start.getFramesetName();
|
---|
| 76 | if (_maxFrame < Long.MAX_VALUE) {
|
---|
| 77 | fileName += "_" + _firstFrame + "-" + _maxFrame;
|
---|
| 78 | }
|
---|
| 79 | return fileName;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Override
|
---|
[306] | 83 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
[226] | 84 | _format = ".pdf";
|
---|
[306] | 85 | super.initialise(start, writer);
|
---|
[286] | 86 |
|
---|
[226] | 87 | try {
|
---|
| 88 | _pdfWriter = PdfWriter.getInstance(_pdfDocument,
|
---|
| 89 | new FileOutputStream(_filename));
|
---|
| 90 | _pdfDocument.open();
|
---|
| 91 | _pdfDocument.addCreationDate();
|
---|
[655] | 92 | _pdfDocument.addAuthor(UserSettings.UserName.get());
|
---|
[226] | 93 | _pdfDocument.addCreator("Expeditee");
|
---|
| 94 | } catch (DocumentException e) {
|
---|
| 95 | e.printStackTrace();
|
---|
| 96 | throw new IOException(e.getMessage());
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | @Override
|
---|
| 101 | protected void writeTitle(Text toWrite, List<Item> items)
|
---|
| 102 | throws IOException {
|
---|
| 103 | _pdfDocument.addTitle(toWrite.getText());
|
---|
| 104 | writeText(toWrite);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[286] | 107 | protected void writeStartFrame(Frame starting) throws IOException {
|
---|
| 108 | // TODO color in the frame background color
|
---|
| 109 |
|
---|
| 110 | super.writeStartFrame(starting);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[226] | 113 | protected void writeEndFrame(Frame ending) throws IOException {
|
---|
[286] | 114 | if (_showFrameNames)
|
---|
| 115 | writeText((Text) ending.getNameItem());
|
---|
[226] | 116 | super.writeEndFrame(ending);
|
---|
[282] | 117 | // Move to the next page
|
---|
[226] | 118 | _pdfDocument.newPage();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | @Override
|
---|
| 122 | protected String finalise() throws IOException {
|
---|
| 123 | _pdfDocument.close();
|
---|
| 124 | return super.finalise();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Override
|
---|
| 128 | protected void writeText(Text text) throws IOException {
|
---|
| 129 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
| 130 | // we tell the ContentByte we're ready to draw text
|
---|
| 131 | cb.beginText();
|
---|
| 132 | Font font = FontFactory.getFont(
|
---|
| 133 | Conversion.getPdfFont(text.getFamily()), text.getSize(), text
|
---|
| 134 | .getPaintFont().getStyle(), text.getPaintColor());
|
---|
| 135 | cb.setFontAndSize(font.getBaseFont(), text.getSize());
|
---|
| 136 | // cb.setColorStroke(text.getPaintColor());
|
---|
| 137 | cb.setColorFill(text.getPaintColor());
|
---|
| 138 |
|
---|
| 139 | // we draw some text on a certain position
|
---|
| 140 | cb.setTextMatrix(text.getX(), _pdfWriter.getPageSize().getHeight()
|
---|
| 141 | - text.getY());
|
---|
| 142 | List<String> textLines = text.getTextList();
|
---|
| 143 | // cb.showText(textLines.get(0));
|
---|
| 144 | for (int i = 0; i < textLines.size(); i++) {
|
---|
| 145 | cb.showText(textLines.get(i));
|
---|
| 146 | cb.moveText(0, -text.getLineHeight());
|
---|
| 147 | }
|
---|
| 148 | // we tell the contentByte, we've finished drawing text
|
---|
| 149 | cb.endText();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | @Override
|
---|
| 153 | protected void writePicture(Picture pic) throws IOException {
|
---|
| 154 | Image image = pic.getCroppedImage();
|
---|
[286] | 155 | if (image == null)
|
---|
[282] | 156 | return;
|
---|
[226] | 157 | try {
|
---|
| 158 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
| 159 | com.lowagie.text.Image iTextImage = com.lowagie.text.Image
|
---|
| 160 | .getInstance(image, null);
|
---|
| 161 | iTextImage.setAbsolutePosition(pic.getX(), _pdfWriter.getPageSize()
|
---|
| 162 | .getHeight()
|
---|
| 163 | - pic.getY() - pic.getHeight());
|
---|
| 164 | cb.addImage(iTextImage);
|
---|
| 165 | } catch (DocumentException e) {
|
---|
| 166 | e.printStackTrace();
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | @Override
|
---|
[286] | 171 | protected void writeLine(Line lineEnd) throws IOException {
|
---|
| 172 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
| 173 | // Color fill = lineEnd.getFillColor();
|
---|
| 174 | // if (fill != null) {
|
---|
| 175 | // cb.setColorFill(fill);
|
---|
| 176 | // }
|
---|
| 177 | Graphics2D g = cb.createGraphicsShapes(
|
---|
| 178 | FrameGraphics.getMaxSize().width,
|
---|
| 179 | FrameGraphics.getMaxSize().height);
|
---|
| 180 | lineEnd.paint(g);
|
---|
| 181 | // if (fill != null) {
|
---|
| 182 | // g.setPaint(fill);
|
---|
| 183 | // }
|
---|
| 184 | // lineEnd.paintFill(g);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | @Override
|
---|
[226] | 188 | protected Collection<Item> getItemsToWrite(Frame toWrite) {
|
---|
| 189 | return toWrite.getVisibleItems();
|
---|
| 190 | }
|
---|
[286] | 191 |
|
---|
| 192 | @Override
|
---|
| 193 | protected void writeCircle(Circle toWrite) throws IOException {
|
---|
| 194 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
| 195 | Graphics2D g = cb.createGraphicsShapes(
|
---|
| 196 | FrameGraphics.getMaxSize().width,
|
---|
| 197 | FrameGraphics.getMaxSize().height);
|
---|
| 198 | toWrite.paint(g);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | @Override
|
---|
| 203 | protected void writeWidget(InteractiveWidget toWrite) throws IOException {
|
---|
| 204 | PdfContentByte cb = _pdfWriter.getDirectContent();
|
---|
| 205 | Graphics2D g = cb.createGraphicsShapes(
|
---|
| 206 | FrameGraphics.getMaxSize().width,
|
---|
| 207 | FrameGraphics.getMaxSize().height);
|
---|
| 208 | toWrite.paint(g);
|
---|
| 209 | }
|
---|
[226] | 210 | }
|
---|