Author: kmorin Date: 2014-09-30 17:09:55 +0200 (Tue, 30 Sep 2014) New Revision: 655 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/655 Log: impression avec pdfbox Modified: trunk/faxtomail-service/src/main/resources/pdf/demande.mustache trunk/faxtomail-ui-swing/pom.xml trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUI.css trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIHandler.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIModel.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java trunk/pom.xml Modified: trunk/faxtomail-service/src/main/resources/pdf/demande.mustache =================================================================== --- trunk/faxtomail-service/src/main/resources/pdf/demande.mustache 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/faxtomail-service/src/main/resources/pdf/demande.mustache 2014-09-30 15:09:55 UTC (rev 655) @@ -1,6 +1,9 @@ <html> <head> <style> + body { + font-family: "Trebuchet MS", Helvetica, sans-serif; + } .title { text-align:center; } Modified: trunk/faxtomail-ui-swing/pom.xml =================================================================== --- trunk/faxtomail-ui-swing/pom.xml 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/faxtomail-ui-swing/pom.xml 2014-09-30 15:09:55 UTC (rev 655) @@ -221,11 +221,6 @@ </dependency> <dependency> - <groupId>org.swinglabs</groupId> - <artifactId>pdf-renderer</artifactId> - </dependency> - - <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> </dependency> Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUI.css =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUI.css 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUI.css 2014-09-30 15:09:55 UTC (rev 655) @@ -59,14 +59,14 @@ #prevPageButton { actionIcon: left; - enabled: { getModel().getCurrentPageIndex() > 1 }; + enabled: { getModel().getCurrentPageIndex() > 0 }; toolTipText: "faxtomail.pdfEditor.button.previousPage"; } #pageNumber { bean: { model }; property: "currentPageIndex"; - modelText: { String.valueOf(model.getCurrentPageIndex()) }; + modelText: { String.valueOf(model.getCurrentPageIndex() + 1) }; useFloat: false; useSign: false; autoPopup: false; @@ -81,7 +81,7 @@ #nextPageButton { actionIcon: right; - enabled: { getModel().getCurrentPageIndex() < getModel().getPages().length }; + enabled: { getModel().getCurrentPageIndex() < getModel().getPages().length - 1 }; toolTipText: "faxtomail.pdfEditor.button.nextPage"; } Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIHandler.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIHandler.java 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIHandler.java 2014-09-30 15:09:55 UTC (rev 655) @@ -26,16 +26,12 @@ import static org.nuiton.i18n.I18n.t; -import java.awt.Component; -import java.awt.Cursor; -import java.awt.Dimension; -import java.awt.Image; -import java.awt.Insets; -import java.awt.Rectangle; +import java.awt.*; import java.awt.event.ContainerEvent; import java.awt.event.ContainerListener; import java.awt.event.MouseEvent; import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -65,6 +61,10 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.pdfbox.io.RandomAccess; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.nuiton.jaxx.application.swing.util.Cancelable; import org.nuiton.jaxx.application.swing.util.CloseableUI; @@ -79,8 +79,6 @@ import com.franciaflex.faxtomail.ui.swing.util.FaxToMailUIUtil; import com.franciaflex.faxtomail.ui.swing.util.JImagePanel; import com.itextpdf.text.DocumentException; -import com.sun.pdfview.PDFFile; -import com.sun.pdfview.PDFPage; /** * @author Kevin Morin (Code Lutin) @@ -93,7 +91,7 @@ protected ComponentMover cm = new ComponentMover(); protected ComponentResizer cr = new ComponentResizer(); - protected PDFFile pdf; + protected PDDocument pdf; @Override public void afterInit(PDFEditorUI pdfEditorUI) { @@ -194,27 +192,21 @@ } } if (FaxToMailUIUtil.isFileAPDF(attachmentFile)) { - RandomAccessFile raf = null; try { - raf = new RandomAccessFile(file, "r"); - FileChannel channel = raf.getChannel(); - ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); - pdf = new PDFFile(buf); + pdf = PDDocument.load(file); - model.setPageNumber(pdf.getNumPages()); - model.setCurrentPageIndex(1); - raf.close(); + model.setPageNumber(pdf.getNumberOfPages()); + model.setCurrentPageIndex(0); + } catch (IOException e) { if (log.isErrorEnabled()) { log.error("", e); } getContext().getErrorHelper().showErrorDialog(t("faxtomail.pdfEditor.readPdf.error")); - } finally { - IOUtils.closeQuietly(raf); } } getUI().setCursor(Cursor.getDefaultCursor()); - model.firePropertyChanged(PDFEditorUIModel.PROPERTY_CURRENT_PAGE_INDEX, null, 1); + model.firePropertyChanged(PDFEditorUIModel.PROPERTY_CURRENT_PAGE_INDEX, null, 0); } } } @@ -359,9 +351,10 @@ } protected void displayPage(int pageNb, float previousZoom, int previousRotation) { - PDFPage page = pdf.getPage(pageNb); + List<PDPage> pages = pdf.getDocumentCatalog().getAllPages(); + PDPage page = pages.get(pageNb); // create the image - Rectangle2D bBox = page.getBBox(); + PDRectangle bBox = page.findCropBox(); Rectangle rect = new Rectangle(0, 0, (int) bBox.getWidth(), (int) bBox.getHeight()); @@ -370,157 +363,165 @@ int width = (int) (zoom * rect.width); int height = (int) (zoom * rect.height); - Image image = page.getImage(width, height, // width & height - rect, // clip rect - null, // null for the ImageObserver - true, // fill background with white - true // block until drawing is done - ); + try { + Image image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 72); + // page.getImage(width, height, // width & height + // rect, // clip rect + // null, // null for the ImageObserver + // true, // fill background with white + // true // block until drawing is done + // ); - JPanel container = getUI().getContainer(); - Dimension containerSize = new Dimension(rotation % 180 == 0 ? width : height, - rotation % 180 == 0 ? height : width); - container.setPreferredSize(containerSize); - container.setMinimumSize(containerSize); - container.setMaximumSize(containerSize); - container.setSize(containerSize); + JPanel container = getUI().getContainer(); + Dimension containerSize = new Dimension(rotation % 180 == 0 ? width : height, + rotation % 180 == 0 ? height : width); + container.setPreferredSize(containerSize); + container.setMinimumSize(containerSize); + container.setMaximumSize(containerSize); + container.setSize(containerSize); - JImagePanel documentPanel = ui.getDocumentPanel(); - documentPanel.setRotation(rotation); - documentPanel.setImage(image); + JImagePanel documentPanel = ui.getDocumentPanel(); + documentPanel.setRotation(rotation); + documentPanel.setImage(image); - Insets insets = container.getInsets(); - rect = container.getBounds(); + Insets insets = container.getInsets(); + rect = container.getBounds(); - float zoomRatio = zoom / previousZoom; - int rotationDiff = rotation - previousRotation; + float zoomRatio = zoom / previousZoom; + int rotationDiff = rotation - previousRotation; - PDFEditorUIModel.Page p = getModel().getPage(pageNb); + PDFEditorUIModel.Page p = getModel().getPage(pageNb); - for (PDFEditorNoteUI panel : p.getNotes()) { - panel.setVisible(true); + for (PDFEditorNoteUI panel : p.getNotes()) { + panel.setVisible(true); - panel.setZoom(zoom); - Rectangle bounds = panel.getBounds(); - int x, y; + panel.setZoom(zoom); + Rectangle bounds = panel.getBounds(); + int x, y; - if (rotationDiff == 0) { - x = bounds.x; - y = bounds.y; + if (rotationDiff == 0) { + x = bounds.x; + y = bounds.y; - } else if (rotationDiff == 90 || rotationDiff == -270) { - x = rect.width - bounds.height / 2 - bounds.width / 2 - bounds.y; - y = bounds.x + bounds.width / 2 - bounds.height / 2; + } else if (rotationDiff == 90 || rotationDiff == -270) { + x = rect.width - bounds.height / 2 - bounds.width / 2 - bounds.y; + y = bounds.x + bounds.width / 2 - bounds.height / 2; - } else { - x = bounds.y + bounds.height / 2 - bounds.width / 2; - y = rect.height - bounds.height / 2 - bounds.width / 2 - bounds.x; + } else { + x = bounds.y + bounds.height / 2 - bounds.width / 2; + y = rect.height - bounds.height / 2 - bounds.width / 2 - bounds.x; + } + + panel.setBounds((int) (zoomRatio * x) + insets.left, + (int) (zoomRatio * y) + insets.top, + (int) (zoomRatio * bounds.width), (int) (zoomRatio * bounds.height)); + } - panel.setBounds((int)(zoomRatio * x) + insets.left, - (int)(zoomRatio * y) + insets.top, - (int)(zoomRatio * bounds.width), (int)(zoomRatio * bounds.height)); + boolean orientation180 = Math.abs(rotationDiff) % 180 == 0; - } + for (PDFEditorCrossUI panel : p.getCrosses()) { + panel.setVisible(true); - boolean orientation180 = Math.abs(rotationDiff) % 180 == 0; + Rectangle bounds = panel.getBounds(); - for (PDFEditorCrossUI panel : p.getCrosses()) { - panel.setVisible(true); + int newWidth = orientation180 ? bounds.width : bounds.height; + int newHeight = orientation180 ? bounds.height : bounds.width; - Rectangle bounds = panel.getBounds(); + int x, y; - int newWidth = orientation180 ? bounds.width : bounds.height; - int newHeight = orientation180 ? bounds.height : bounds.width; + if (rotationDiff == 0) { + x = bounds.x; + y = bounds.y; - int x, y; + } else if (rotationDiff == 90 || rotationDiff == -270) { + x = rect.width - newWidth - bounds.y; + y = bounds.x; - if (rotationDiff == 0) { - x = bounds.x; - y = bounds.y; + } else { + x = bounds.y; + y = rect.height - newHeight - bounds.x; + } - } else if (rotationDiff == 90 || rotationDiff == -270) { - x = rect.width - newWidth - bounds.y; - y = bounds.x; + panel.setBounds((int) (zoomRatio * x) + insets.left, + (int) (zoomRatio * y) + insets.top, + (int) (zoomRatio * newWidth), (int) (zoomRatio * newHeight)); - } else { - x = bounds.y; - y = rect.height - newHeight - bounds.x; } - panel.setBounds((int) (zoomRatio * x) + insets.left, - (int) (zoomRatio * y) + insets.top, - (int) (zoomRatio * newWidth), (int) (zoomRatio * newHeight)); + List<PDFEditorLineUI> lines = p.getLines(); + cr.deregisterComponent(lines.toArray(new PDFEditorLineUI[lines.size()])); - } + for (PDFEditorLineUI panel : lines) { + panel.setVisible(true); - List<PDFEditorLineUI> lines = p.getLines(); - cr.deregisterComponent(lines.toArray(new PDFEditorLineUI[lines.size()])); + Rectangle bounds = panel.getBounds(); - for (PDFEditorLineUI panel : lines) { - panel.setVisible(true); + boolean horizontal = panel.isHorizontal(); + panel.setHorizontal(orientation180 ? horizontal : !horizontal); - Rectangle bounds = panel.getBounds(); + int newWidth = orientation180 ? bounds.width : bounds.height; + int newHeight = orientation180 ? bounds.height : bounds.width; - boolean horizontal = panel.isHorizontal(); - panel.setHorizontal(orientation180 ? horizontal : ! horizontal); + int x, y; - int newWidth = orientation180 ? bounds.width : bounds.height; - int newHeight = orientation180 ? bounds.height : bounds.width; + if (rotationDiff == 0) { + x = bounds.x; + y = bounds.y; - int x, y; + } else if (rotationDiff == 90 || rotationDiff == -270) { + x = rect.width - newWidth - bounds.y; + y = bounds.x; - if (rotationDiff == 0) { - x = bounds.x; - y = bounds.y; + } else { + x = bounds.y; + y = rect.height - newHeight - bounds.x; + } - } else if (rotationDiff == 90 || rotationDiff == -270) { - x = rect.width - newWidth - bounds.y; - y = bounds.x; + panel.setBounds((int) (zoomRatio * x) + insets.left, + (int) (zoomRatio * y) + insets.top, + (int) (zoomRatio * newWidth), (int) (zoomRatio * newHeight)); - } else { - x = bounds.y; - y = rect.height - newHeight - bounds.x; + cr.registerComponent(panel.isHorizontal() ? ComponentResizer.DIRECTION_HORIZONTAL : ComponentResizer.DIRECTION_VERTICAL, + panel); } - panel.setBounds((int) (zoomRatio * x) + insets.left, - (int) (zoomRatio * y) + insets.top, - (int) (zoomRatio * newWidth), (int) (zoomRatio * newHeight)); + for (PDFEditorHighlighterUI panel : p.getHighlighters()) { + panel.setVisible(true); - cr.registerComponent(panel.isHorizontal() ? ComponentResizer.DIRECTION_HORIZONTAL : ComponentResizer.DIRECTION_VERTICAL, - panel); - } + Rectangle bounds = panel.getBounds(); - for (PDFEditorHighlighterUI panel : p.getHighlighters()) { - panel.setVisible(true); + int newWidth = orientation180 ? bounds.width : bounds.height; + int newHeight = orientation180 ? bounds.height : bounds.width; - Rectangle bounds = panel.getBounds(); + int x, y; - int newWidth = orientation180 ? bounds.width : bounds.height; - int newHeight = orientation180 ? bounds.height : bounds.width; + if (rotationDiff == 0) { + x = bounds.x; + y = bounds.y; - int x, y; + } else if (rotationDiff == 90 || rotationDiff == -270) { + x = rect.width - newWidth - bounds.y; + y = bounds.x; - if (rotationDiff == 0) { - x = bounds.x; - y = bounds.y; + } else { + x = bounds.y; + y = rect.height - newHeight - bounds.x; + } - } else if (rotationDiff == 90 || rotationDiff == -270) { - x = rect.width - newWidth - bounds.y; - y = bounds.x; + panel.setBounds((int) (zoomRatio * x) + insets.left, + (int) (zoomRatio * y) + insets.top, + (int) (zoomRatio * newWidth), (int) (zoomRatio * newHeight)); - } else { - x = bounds.y; - y = rect.height - newHeight - bounds.x; } - panel.setBounds((int) (zoomRatio * x) + insets.left, - (int) (zoomRatio * y) + insets.top, - (int) (zoomRatio * newWidth), (int) (zoomRatio * newHeight)); + container.updateUI(); + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Cannot convert page into image", e); + } } - - container.updateUI(); } public File convertFileToPdf(AttachmentFile attachmentFile) throws IOException, DocumentException { Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIModel.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIModel.java 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/pdfeditor/PDFEditorUIModel.java 2014-09-30 15:09:55 UTC (rev 655) @@ -124,7 +124,6 @@ protected Page[] pages; - // first page is 1 not 0 protected int currentPageIndex = -1; protected float zoom = 1.0f; @@ -194,17 +193,17 @@ } public Page getCurrentPage() { - if (pages == null || currentPageIndex < 1 || currentPageIndex > pages.length) { + if (pages == null || currentPageIndex < 0 || currentPageIndex >= pages.length) { return null; } - return pages[currentPageIndex - 1]; + return pages[currentPageIndex]; } public Page getPage(int i) { - if (pages == null || i < 1 || i > pages.length) { + if (pages == null || i < 0 || i >= pages.length) { return null; } - return pages[i - 1]; + return pages[i]; } public int getCurrentPageIndex() { @@ -213,9 +212,9 @@ public void setCurrentPageIndex(int currentPageIndex) { Object oldValue = getCurrentPageIndex(); - currentPageIndex = Math.max(1, currentPageIndex); + currentPageIndex = Math.max(0, currentPageIndex); if (pages != null) { - currentPageIndex = Math.min(pages.length, currentPageIndex); + currentPageIndex = Math.min(pages.length - 1, currentPageIndex); } this.currentPageIndex = currentPageIndex; firePropertyChange(PROPERTY_CURRENT_PAGE_INDEX, oldValue, currentPageIndex); Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java 2014-09-30 15:09:55 UTC (rev 655) @@ -73,6 +73,7 @@ import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; +import com.itextpdf.text.RectangleReadOnly; import jaxx.runtime.JAXXObject; import jaxx.runtime.JAXXUtil; @@ -86,10 +87,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.PDPage; -import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; -import org.apache.pdfbox.pdmodel.font.PDFont; -import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.nuiton.jaxx.application.ApplicationTechnicalException; import org.nuiton.jaxx.application.swing.util.ApplicationUIUtil; import org.nuiton.util.FileUtil; Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-09-30 12:22:29 UTC (rev 654) +++ trunk/pom.xml 2014-09-30 15:09:55 UTC (rev 655) @@ -361,12 +361,6 @@ <version>${swingXVersion}</version> </dependency> - <dependency> - <groupId>org.swinglabs</groupId> - <artifactId>pdf-renderer</artifactId> - <version>1.0.5</version> - </dependency> - <!-- Logging --> <dependency> <groupId>org.apache.logging.log4j</groupId>