Skip to content

Commit

Permalink
chore: java: replace ghost4j with pdfbox
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Sep 3, 2022
1 parent 6cfcc09 commit 2277119
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cie-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
implementation 'com.jgoodies:jgoodies-forms:1.9.0'
implementation 'net.java.dev.jna:jna:5.12.1'
implementation 'net.java.dev.jna:jna-platform:5.12.1'
implementation 'org.ghost4j:ghost4j:1.0.1'
implementation 'org.apache.pdfbox:pdfbox:2.0.26'
implementation 'org.javassist:javassist:3.29.1-GA'
implementation 'org.slf4j:slf4j-api:2.0.0'
}
Expand Down
80 changes: 36 additions & 44 deletions cie-java/src/it/ipzs/cieid/Firma/PdfPreview.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package it.ipzs.cieid.Firma;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.ghost4j.document.DocumentException;
import org.ghost4j.document.PDFDocument;
import org.ghost4j.renderer.RendererException;
import org.ghost4j.renderer.SimpleRenderer;

public class PdfPreview {
private final JPanel prPanel;
private final String filePath;
private final String signImagePath;
private int pdfPageIndex;
private int pdfNumPages;
private List<Image> images;
private List<BufferedImage> renderedPages = new ArrayList<BufferedImage>();
private final JLabel imgLabel;
private final ImageIcon imgIcon;
private final MoveablePicture signImage;
Expand All @@ -41,71 +37,67 @@ public PdfPreview(JPanel panelPdfPreview, String pdfFilePath, String signImagePa
imgPanel.add(signImage);
imgPanel.add(imgLabel);

File file = new File(filePath);

try {
PDFDocument document = new PDFDocument();
document.load(new File(filePath));
pdfNumPages = document.getPageCount();
PDDocument document = PDDocument.load(file);
pdfNumPages = document.getNumberOfPages();
System.out.println("Pdf page: " + pdfNumPages);
SimpleRenderer renderer = new SimpleRenderer();
PDFRenderer renderer = new PDFRenderer(document);

renderer.setResolution(100);
prPanel.removeAll();
images = renderer.render(document);

showPreview();
for (pdfPageIndex = 0; pdfPageIndex < pdfNumPages; pdfPageIndex++) {
BufferedImage renderedPage = renderer.renderImage(pdfPageIndex);
renderedPages.add(renderedPage);
}

pdfPageIndex = 0;
showPreview();
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("PDF File not found");
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
System.out.println("Document Exception");
e.printStackTrace();
} catch (RendererException e) {
// TODO Auto-generated catch block
System.out.println("Renderer Exception");
e.printStackTrace();
}
}

private void showPreview() {
Image tmpImg = images.get(pdfPageIndex);
Image tmpImg = renderedPages.get(pdfPageIndex);

int width = prPanel.getWidth();
int height = prPanel.getHeight();

int tmpImgWidth = tmpImg.getWidth(null);
int tmpImgHeight = tmpImg.getHeight(null);

int imgHeigth = height;
int imgHeight = height;
int imgWidth = width;

if (tmpImgWidth > tmpImgHeight) {
imgHeigth = (width * tmpImgHeight) / tmpImgWidth;
imgHeight = (width * tmpImgHeight) / tmpImgWidth;

if (imgHeigth > height) {
if (imgHeight > height) {
imgWidth = (height * tmpImgWidth) / tmpImgHeight;
imgHeigth = (imgWidth * tmpImgHeight) / tmpImgWidth;
imgHeight = (imgWidth * tmpImgHeight) / tmpImgWidth;
}
} else {
imgWidth = (height * tmpImgWidth) / tmpImgHeight;

if (imgWidth > width) {
imgHeigth = (width * imgHeigth) / tmpImgWidth;
imgWidth = (imgHeigth * tmpImgWidth) / imgHeigth;
imgHeight = (width * imgHeight) / tmpImgWidth;
imgWidth = (imgHeight * tmpImgWidth) / imgHeight;
}
}

imgIcon.setImage(tmpImg.getScaledInstance(imgWidth, imgHeigth, Image.SCALE_AREA_AVERAGING));
imgIcon.setImage(tmpImg.getScaledInstance(imgWidth, imgHeight, Image.SCALE_AREA_AVERAGING));
imgLabel.setIcon(imgIcon);
imgLabel.setHorizontalAlignment(JLabel.CENTER);
imgLabel.setVerticalAlignment(JLabel.CENTER);
imgLabel.revalidate();
imgLabel.repaint();

// imgPanel.removeAll();
imgPanel.setMaximumSize(new Dimension(imgWidth, imgHeigth));
imgPanel.setMaximumSize(new Dimension(imgWidth, imgHeight));
imgPanel.updateUI();

prPanel.removeAll();
Expand Down Expand Up @@ -133,20 +125,20 @@ public int getSelectedPage() {
return pdfPageIndex;
}

public float[] signImageInfos() {
float[] infos = new float[4];
public float[] signImageInfo() {
float[] info = new float[4];

float x = ((float) signImage.getX() / (float) imgPanel.getWidth());
float y =
((float) (signImage.getY() + signImage.getHeight()) / (float) imgPanel.getHeight());
float w = ((float) signImage.getWidth() / (float) imgPanel.getWidth());
float h = ((float) signImage.getHeight() / (float) imgPanel.getHeight());

infos[0] = x;
infos[1] = y;
infos[2] = w;
infos[3] = h;
info[0] = x;
info[1] = y;
info[2] = w;
info[3] = h;

return infos;
return info;
}
}
2 changes: 1 addition & 1 deletion cie-java/src/it/ipzs/cieid/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3461,7 +3461,7 @@ public void invoke(int retValue) {
final int ret;
if (signOperation == SignOp.PADES) {
if (cbGraphicSig.isSelected()) {
float infos[] = preview.signImageInfos();
float infos[] = preview.signImageInfo();
String signImagePath =
getSignImagePath(
selectedCie.getCard().getSerialNumber());
Expand Down

0 comments on commit 2277119

Please sign in to comment.