diff options
| author | tkellner <tkellner@3a0b52a2-8410-0410-bc02-ff6273a87459> | 2012-08-24 17:05:48 +0000 | 
|---|---|---|
| committer | tkellner <tkellner@3a0b52a2-8410-0410-bc02-ff6273a87459> | 2012-08-24 17:05:48 +0000 | 
| commit | 0da217b5325ddfea9434a79f2f191bbb7f6031d8 (patch) | |
| tree | f4cbe8eeaa2d4eb11db47829c58ede4df7c878e3 /trunk/pdf-over-gui/src/main/java/at | |
| parent | 30406f6cb400c518b3e7aaf875eccc092e50b90a (diff) | |
| download | pdf-over-0da217b5325ddfea9434a79f2f191bbb7f6031d8.tar.gz pdf-over-0da217b5325ddfea9434a79f2f191bbb7f6031d8.tar.bz2 pdf-over-0da217b5325ddfea9434a79f2f191bbb7f6031d8.zip | |
add PDFViewerComposite
git-svn-id: https://svn.iaik.tugraz.at/svn/egiz/prj/current/12PDF-OVER-4.0@12398 3a0b52a2-8410-0410-bc02-ff6273a87459
Diffstat (limited to 'trunk/pdf-over-gui/src/main/java/at')
| -rw-r--r-- | trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java | 155 | 
1 files changed, 155 insertions, 0 deletions
| diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java new file mode 100644 index 00000000..1637da9e --- /dev/null +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PDFViewerComposite.java @@ -0,0 +1,155 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.gui.composites; + +// Imports +import java.awt.Canvas; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.geom.Rectangle2D; +import java.io.File; +import java.io.IOException; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.awt.SWT_AWT; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.widgets.Composite; +import org.icepdf.core.exceptions.PDFException; +import org.icepdf.core.exceptions.PDFSecurityException; +import org.icepdf.core.pobjects.Document; +import org.icepdf.core.pobjects.Page; +import org.icepdf.core.util.GraphicsRenderingHints; + +/** + * Displays a PDF document + */ +public class PDFViewerComposite extends Composite { + +	/** +	 * PDF document +	 */ +	protected Document document; + +	/** +	 * Currently selected page in the document +	 */ +	protected int page; + +	/** +	 * Get the currently selected page in the document +	 * @return current page +	 */ +	public int getPage() { +		return this.page; +	} + +	/** +	 * Set the visible page in the document +	 * @param page new active page +	 */ +	public void setPage(int page) { +		this.page = page; +		this.canvas.repaint(); +	} + +	/** +	 * AWT Canvas displaying the document +	 */ +	private Canvas canvas; + +	/** +	 * Create the PDF Viewer composite. +	 * @param parent parent Composite +	 * @param style  +	 * @param document +	 * @throws PDFException Error parsing PDF document +	 * @throws PDFSecurityException Error decrypting PDF document (not supported) +	 * @throws IOException I/O Error +	 */ +	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; + +		Frame frame = SWT_AWT.new_Frame(this); +		this.canvas = new Canvas() { +			private static final long serialVersionUID = 1L; + +			@Override +			public void paint(Graphics g) { +				if (g == null || g.getClip() == null) +					return; +				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; +				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); +				} + +				PDFViewerComposite.this.document.paintPage(page, g, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, 0f, zoom); +			} +		}; +		frame.add(this.canvas); +		frame.pack(); +		frame.setVisible(true); + +		this.addKeyListener(new KeyAdapter() { +			@Override +			public void keyPressed(KeyEvent e) { +				int page = getPage(); +				int old_page = page; + +				switch (e.keyCode) +				{ +					case SWT.PAGE_DOWN: +						if (page < (PDFViewerComposite.this.document.getNumberOfPages() - 1)) +							++page; +						break; + +					case SWT.PAGE_UP: +						if (page > 0) +							--page; +						break; + +					case SWT.END: +						page = (PDFViewerComposite.this.document.getNumberOfPages() - 1); +						break; + +					case SWT.HOME: +						page = 0; +						break; +				} + +				if (page != old_page) +					setPage(page); +			} +		}); +	} +} | 
