From fa8d29cdc26faa657009482a960776affb2c8fc3 Mon Sep 17 00:00:00 2001 From: tkellner Date: Wed, 10 Apr 2013 18:55:32 +0000 Subject: Allow document to be changed in PDFViewerComposite git-svn-id: https://joinup.ec.europa.eu/svn/pdf-over/trunk@41 174cde9d-5d70-4d2a-aa98-46368bc2aaf7 --- .../pdfover/gui/composites/PDFViewerComposite.java | 54 +++++++++++++++------- 1 file changed, 38 insertions(+), 16 deletions(-) (limited to 'pdf-over-gui/src') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java index 1637da9e..7792fa3f 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java @@ -50,6 +50,38 @@ public class PDFViewerComposite extends Composite { */ protected int page; + /** + * AWT Canvas displaying the document + */ + private Canvas canvas = null; + + /** + * Dimension of each page at default zoom + */ + Dimension[] base_dimensions; + + /** + * Set the document to be displayed + * @param document PDF document to be displayed + * @throws PDFException Error parsing PDF document + * @throws PDFSecurityException Error decrypting PDF document (not supported) + * @throws IOException I/O Error + */ + public void setDocument(File document) throws PDFException, PDFSecurityException, IOException { + this.document = new Document(); + this.document.setFile(document.getPath()); + int pages = this.document.getNumberOfPages(); + + this.base_dimensions = new Dimension[pages]; + for (int page = 0; page < pages; ++page) + this.base_dimensions[page] = this.document.getPageDimension(page, 0f, 1.0f).toDimension(); + + this.page = pages - 1; + + if (this.canvas != null) + this.canvas.repaint(); + } + /** * Get the currently selected page in the document * @return current page @@ -67,13 +99,10 @@ public class PDFViewerComposite extends Composite { this.canvas.repaint(); } - /** - * AWT Canvas displaying the document - */ - private Canvas canvas; - /** * Create the PDF Viewer composite. + * Displays a PDF document. + * Starts on the last page. * @param parent parent Composite * @param style * @param document @@ -83,15 +112,8 @@ public class PDFViewerComposite extends Composite { */ public PDFViewerComposite(Composite parent, int style, File document) throws PDFException, PDFSecurityException, IOException { super(parent, style); - this.document = new Document(); - this.document.setFile(document.getPath()); - int pages = this.document.getNumberOfPages(); - - final Dimension[] base_dimensions = new Dimension[pages]; - for (int page = 0; page < pages; ++page) - base_dimensions[page] = this.document.getPageDimension(page, 0f, 1.0f).toDimension(); - this.page = pages - 1; + setDocument(document); Frame frame = SWT_AWT.new_Frame(this); this.canvas = new Canvas() { @@ -104,13 +126,13 @@ public class PDFViewerComposite extends Composite { int page = getPage(); // Make page always fit to window Rectangle2D clip = g.getClip().getBounds2D(); - double h_zoom = clip.getWidth() / base_dimensions[page].width; - double v_zoom = clip.getHeight() / base_dimensions[page].height; + double h_zoom = clip.getWidth() / PDFViewerComposite.this.base_dimensions[page].width; + double v_zoom = clip.getHeight() / PDFViewerComposite.this.base_dimensions[page].height; float zoom = (float) (h_zoom < v_zoom ? h_zoom : v_zoom); if (v_zoom < h_zoom) { // Page is narrower than window, center it - g.translate((int) ((clip.getWidth() - (base_dimensions[page].width * zoom)) / 2), 0); + g.translate((int) ((clip.getWidth() - (PDFViewerComposite.this.base_dimensions[page].width * zoom)) / 2), 0); } PDFViewerComposite.this.document.paintPage(page, g, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, 0f, zoom); -- cgit v1.2.3