summaryrefslogtreecommitdiff
path: root/pdf-over-gui/src/main
diff options
context:
space:
mode:
authorJakob Heher <jakob.heher@iaik.tugraz.at>2022-07-15 15:30:11 +0200
committerJakob Heher <jakob.heher@iaik.tugraz.at>2022-07-15 15:30:11 +0200
commit3011a7561a30005efc929e9ad4c681a7653e8ba7 (patch)
tree309bbbda6c89a0e9f0353372d86a3725a23a641e /pdf-over-gui/src/main
parent145dc4e64898572353de3e92bb3d365ccdf8f02f (diff)
downloadpdf-over-3011a7561a30005efc929e9ad4c681a7653e8ba7.tar.gz
pdf-over-3011a7561a30005efc929e9ad4c681a7653e8ba7.tar.bz2
pdf-over-3011a7561a30005efc929e9ad4c681a7653e8ba7.zip
kill pdfview, cf. #104
Diffstat (limited to 'pdf-over-gui/src/main')
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java10
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java114
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java31
3 files changed, 84 insertions, 71 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java
index 88577f7f..dc512d19 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/PositioningComposite.java
@@ -27,6 +27,7 @@ import java.awt.event.KeyListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
+import org.apache.pdfbox.pdmodel.PDDocument;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.SelectionAdapter;
@@ -47,8 +48,6 @@ import at.asit.pdfover.commons.Messages;
import at.asit.pdfover.gui.workflow.states.State;
import at.asit.pdfover.signator.SignaturePosition;
-import com.sun.pdfview.PDFFile;
-
/**
* Composite which allows to position the signature on a preview of the document
*/
@@ -56,8 +55,7 @@ public class PositioningComposite extends StateComposite {
/**
* SLF4J Logger instance
**/
- static final Logger log = LoggerFactory
- .getLogger(PositioningComposite.class);
+ static final Logger log = LoggerFactory.getLogger(PositioningComposite.class);
SignaturePanel viewer = null;
Frame frame = null;
@@ -155,7 +153,7 @@ public class PositioningComposite extends StateComposite {
* @param document
* document to display
*/
- public void displayDocument(final PDFFile document) {
+ public void displayDocument(final PDDocument document) {
if (this.viewer == null) {
EventQueue.invokeLater(() -> {
this.viewer = new SignaturePanel(document);
@@ -173,7 +171,7 @@ public class PositioningComposite extends StateComposite {
if (document != null)
{
- this.numPages = document.getNumPages();
+ this.numPages = document.getNumberOfPages();
this.scrollbar.setValues(1, 1, this.numPages + 1, 1, 1, 1);
showPage(this.numPages);
}
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java
index ec2b1a65..3a325684 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/SignaturePanel.java
@@ -29,18 +29,21 @@ import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
+import java.io.IOException;
import javax.swing.JPanel;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;
+import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import at.asit.pdfover.commons.Constants;
import at.asit.pdfover.commons.Messages;
-import com.sun.pdfview.PDFFile;
-import com.sun.pdfview.PDFPage;
-
/**
*
*/
@@ -55,13 +58,14 @@ public class SignaturePanel extends JPanel {
private static final long serialVersionUID = 1L;
/** The PDF file being displayed */
- private PDFFile pdf = null;
+ private PDDocument pdf = null;
+ private PDFRenderer renderer = null;
/** The image of the rendered PDF page being displayed */
Image currentImage = null;
/** The current PDFPage that was rendered into currentImage */
- private PDFPage currentPage = null;
+ private PDPage currentPage = null;
/** The current transform from screen to page space */
- AffineTransform currentXform = null;
+ AffineTransform currentTransform = null;
/** The horizontal offset of the image from the left edge of the panel */
int offX = 0;
/** The vertical offset of the image from the top of the panel */
@@ -95,7 +99,7 @@ public class SignaturePanel extends JPanel {
/** Color of the signature placeholder border */
private Color sigPlaceholderBorderColor = Color.BLUE;
/** Current page */
- int page = 0;
+ int currentPageNo = 0;
/** Number of pages in the document */
int numPages = 0;
/** Cursor types */
@@ -113,7 +117,7 @@ public class SignaturePanel extends JPanel {
* Create a new PagePanel.
* @param pdf the PDFFile to display
*/
- public SignaturePanel(PDFFile pdf) {
+ public SignaturePanel(PDDocument pdf) {
super(new BorderLayout());
setDocument(pdf);
setPreferredSize(new Dimension(Constants.DEFAULT_MAINWINDOW_WIDTH, Constants.DEFAULT_MAINWINDOW_HEIGHT - Constants.MAINBAR_HEIGHT));
@@ -126,19 +130,22 @@ public class SignaturePanel extends JPanel {
* Set a new document to be displayed
* @param pdf the PDFFile to be displayed
*/
- public void setDocument(PDFFile pdf) {
+ public void setDocument(PDDocument pdf) {
this.pdf = pdf;
this.sigPagePos = null;
if (pdf != null)
{
- this.numPages = pdf.getNumPages();
+ this.renderer = new PDFRenderer(pdf);
+ this.numPages = pdf.getNumberOfPages();
showPage(this.numPages);
}
else
{
- this.page = 0;
+ this.renderer = null;
+ this.currentPageNo = 0;
this.numPages = 0;
- showPage(null);
+ renderPageToImage();
+ repaint();
}
}
@@ -169,8 +176,9 @@ public class SignaturePanel extends JPanel {
* @param page the number of the page to display
*/
public void showPage(int page) {
- this.page = page;
- showPage(this.pdf.getPage(page));
+ this.currentPageNo = page;
+ renderPageToImage();
+ repaint();
}
/**
@@ -227,29 +235,30 @@ public class SignaturePanel extends JPanel {
*
* @param page the PDFPage to draw.
*/
- private synchronized void showPage(PDFPage page) {
- // stop drawing the previous page
- if (this.currentPage != null && this.prevSize != null) {
- this.currentPage.stop(this.prevSize.width, this.prevSize.height, null);
+ private synchronized void renderPageToImage() {
+ if (this.pdf == null)
+ {
+ this.currentImage = null;
+ this.currentTransform = null;
+ return;
}
boolean newPage = false;
// set up the new page
- if (this.page > this.numPages)
+ if (this.currentPageNo > this.numPages)
{
// New last page - use old last page as template
- this.currentPage = this.pdf.getPage(this.numPages);
+ this.currentPage = this.pdf.getPage(this.numPages-1);
newPage = true;
}
else
- this.currentPage = page;
+ this.currentPage = this.pdf.getPage(this.currentPageNo-1);
if (this.currentPage == null) {
// no page
this.currentImage = null;
- this.currentXform = null;
- repaint();
+ this.currentTransform = null;
} else {
// start drawing
Dimension sz = getSize();
@@ -258,8 +267,14 @@ public class SignaturePanel extends JPanel {
return;
}
- Dimension pageSize = this.currentPage.getUnstretchedSize(sz.width, sz.height,
- null);
+ PDRectangle clip = this.currentPage.getBBox();
+ double clipRatio = clip.getHeight() / clip.getWidth();
+ double myRatio = (double)sz.height / (double)sz.width;
+ Dimension pageSize = (Dimension)sz.clone();
+ if (myRatio > clipRatio)
+ pageSize.height = (int)(pageSize.width * clipRatio + 0.5);
+ else
+ pageSize.width = (int)(pageSize.height / clipRatio + 0.5);
// get the new image
if (newPage)
@@ -270,25 +285,36 @@ public class SignaturePanel extends JPanel {
g.fillRect(0, 0, pageSize.width, pageSize.height);
}
else
- this.currentImage = this.currentPage.getImage(pageSize.width, pageSize.height,
- null, this);
+ {
+ int whichPage = Math.min(this.currentPageNo, this.numPages);
+ float scale = 1;
+ if (pageSize.width == sz.width)
+ scale = ((float) sz.width) / clip.getWidth();
+ else if (pageSize.height == sz.height)
+ scale = ((float) sz.height) / clip.getHeight();
+
+ try {
+ this.currentImage = renderer.renderImage(whichPage-1, scale);
+ } catch (IOException e) {
+ log.error(String.format("Failed to render image for page %d of %d", whichPage, this.numPages), e);
+ }
+ }
// calculate the transform from page to screen space
- this.currentXform = new AffineTransform(1, 0, 0, -1, 0, pageSize.height);
- Rectangle2D clip = this.currentPage.getBBox();
+ this.currentTransform = new AffineTransform(1, 0, 0, -1, 0, pageSize.height);
double scaleX = pageSize.width / clip.getWidth();
double scaleY = pageSize.height / clip.getHeight();
- this.currentXform.scale(scaleX, scaleY);
- this.currentXform.translate(-clip.getMinX(), -clip.getMinY());
+ this.currentTransform.scale(scaleX, scaleY);
+ this.currentTransform.translate(-clip.getLowerLeftX(), -clip.getLowerLeftY());
if (this.sigPagePos != null)
- this.sigScreenPos = this.currentXform.transform(this.sigPagePos, this.sigScreenPos);
- this.sigScreenWidth = (int) Math.round(this.sigPageWidth * this.currentXform.getScaleX());
- this.sigScreenHeight = (int) Math.round(this.sigPageHeight * this.currentXform.getScaleX());
+ this.sigScreenPos = this.currentTransform.transform(this.sigPagePos, this.sigScreenPos);
+ this.sigScreenWidth = (int) Math.round(this.sigPageWidth * this.currentTransform.getScaleX());
+ this.sigScreenHeight = (int) Math.round(this.sigPageHeight * this.currentTransform.getScaleX());
// invert the transform (screen to page space)
try {
- this.currentXform = this.currentXform.createInverse();
+ this.currentTransform = this.currentTransform.createInverse();
} catch (NoninvertibleTransformException nte) {
log.error("Error inverting page transform!", nte);
}
@@ -298,14 +324,12 @@ public class SignaturePanel extends JPanel {
this.sigScreenPos = new Point2D.Double(
clamp((int) (pageSize.getWidth() / 2), 0, this.currentImage.getWidth(null) - this.sigScreenWidth),
clamp((int) ((pageSize.getHeight() / 4) * 3), 0, this.currentImage.getHeight(null) - this.sigScreenHeight));
- this.sigPagePos = this.currentXform.transform(this.sigScreenPos, this.sigPagePos);
+ this.sigPagePos = this.currentTransform.transform(this.sigScreenPos, this.sigPagePos);
}
else
updateSigPos((int) this.sigScreenPos.getX(), (int) this.sigScreenPos.getY());
this.prevSize = pageSize;
-
- repaint();
}
}
@@ -319,10 +343,10 @@ public class SignaturePanel extends JPanel {
g.fillRect(0, 0, getWidth(), getHeight());
if (this.currentImage == null) {
g.setColor(Color.black);
- g.drawString(Messages.getString("error.SignaturePanel.NoPage"), getWidth() / 2 - 30,
- getHeight() / 2);
+ g.drawString(Messages.getString("error.SignaturePanel.NoPage"), getWidth() / 2 - 30, getHeight() / 2);
if (this.currentPage != null) {
- showPage(this.currentPage);
+ renderPageToImage();
+ repaint();
}
} else {
// draw the image
@@ -395,9 +419,9 @@ public class SignaturePanel extends JPanel {
} else {
// the image is bogus. try again, or give up.
- if (this.currentPage != null) {
- showPage(this.currentPage);
- }
+ if (this.currentPage != null)
+ renderPageToImage();
+
g.setColor(Color.black);
g.drawString(Messages.getString("error.SignaturePanel.NoRender"), getWidth() / 2 - 30,
getHeight() / 2);
@@ -536,7 +560,7 @@ public class SignaturePanel extends JPanel {
sigx = clamp(sigx, 0, this.currentImage.getWidth(null) - this.sigScreenWidth);
sigy = clamp(sigy, 0, this.currentImage.getHeight(null) - this.sigScreenHeight);
this.sigScreenPos = new Point2D.Double(sigx, sigy);
- this.sigPagePos = this.currentXform.transform(this.sigScreenPos, this.sigPagePos);
+ this.sigPagePos = this.currentTransform.transform(this.sigScreenPos, this.sigPagePos);
repaint();
}
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
index b09728c9..aff0fe61 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/states/PositioningState.java
@@ -22,6 +22,8 @@ import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.eclipse.swt.SWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,9 +43,6 @@ import at.asit.pdfover.signator.Emblem;
import at.asit.pdfover.signator.SignatureParameter;
import at.asit.pdfover.signator.SignaturePosition;
-import com.sun.pdfview.PDFFile;
-import com.sun.pdfview.decrypt.UnsupportedEncryptionException;
-import com.sun.pdfview.decrypt.PDFAuthenticationFailureException;
/**
* Decides where to position the signature block
*/
@@ -68,14 +67,14 @@ public class PositioningState extends State {
private File loadedDocumentPath = null;
- private PDFFile document = null;
+ private PDDocument document = null;
private void closePDFDocument() {
if (this.document != null)
{
+ try { this.document.close(); } catch (IOException e) { log.warn("Failed to close PDF", e); }
this.document = null;
- System.gc(); /* try to get Java to close the mapped file... */
}
this.loadedDocumentPath = null;
}
@@ -83,34 +82,26 @@ public class PositioningState extends State {
private void openPDFDocument() throws IOException {
closePDFDocument();
File documentPath = getStateMachine().status.document;
- PDFFile pdf = null;
- RandomAccessFile rafile = new RandomAccessFile(documentPath, "r");
- FileChannel chan = rafile.getChannel();
- ByteBuffer buf = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
- chan.close();
- rafile.close();
+ PDDocument pdf = null;
try
{
- pdf = new PDFFile(buf);
- if (pdf.getNumPages() > 0)
- pdf.getPage(1);
+ pdf = PDDocument.load(documentPath);
+ if (pdf.getNumberOfPages() > 0)
+ pdf.getPage(0);
else
throw new IOException();
}
- catch (PDFAuthenticationFailureException e) {
+ catch (InvalidPasswordException e) {
throw new IOException(Messages.getString("error.PDFPwdProtected"), e);
}
catch (IOException e) {
- if (e.getCause() instanceof UnsupportedEncryptionException)
- throw new IOException(Messages.getString("error.PDFProtected"));
- else
- throw new IOException(Messages.getString("error.MayNotBeAPDF"), e);
+ throw new IOException(Messages.getString("error.MayNotBeAPDF"), e);
}
this.document = pdf;
this.loadedDocumentPath = documentPath;
}
- private PositioningComposite getPositioningComposite(PDFFile document) {
+ private PositioningComposite getPositioningComposite(PDDocument document) {
StateMachine stateMachine = getStateMachine();
if (this.positionComposite == null) {
this.positionComposite =