mirror of
https://github.com/LucasVbr/LecteurPdfDoubleAffichage.git
synced 2026-05-14 01:21:50 +00:00
Ajout de la possibilité de zoom avec rechargerPDF()
This commit is contained in:
@@ -28,7 +28,7 @@ public class Fenetre extends JFrame {
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
private String titre;
|
||||
private final String TITRE = "Lecteur PDF";
|
||||
|
||||
/**
|
||||
* TODO
|
||||
@@ -38,16 +38,28 @@ public class Fenetre extends JFrame {
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
private PDF fichierPDF;
|
||||
private File fichier;
|
||||
|
||||
private PDF documentPDF;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
public Fenetre() {
|
||||
|
||||
/* Change le style de la fenêtre */
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException | InstantiationException
|
||||
| IllegalAccessException | UnsupportedLookAndFeelException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Création du menu
|
||||
menu = new Menu(this);
|
||||
|
||||
this.setTitle(TITRE);
|
||||
|
||||
// Ajout de la barre de menu au frame
|
||||
|
||||
this.setJMenuBar(menu);
|
||||
@@ -63,32 +75,65 @@ public class Fenetre extends JFrame {
|
||||
*
|
||||
* @param fichier
|
||||
*/
|
||||
public void chargerPdf(File fichier) {
|
||||
public void chargerPDF(File fichier) {
|
||||
dechargerPDF();
|
||||
|
||||
/* Décharge le précédent PDF avant de charger le prochain */
|
||||
dechargerPdf();
|
||||
|
||||
PDF fichierPDF = new PDF(fichier);
|
||||
this.fichier = fichier;
|
||||
|
||||
/* Crée le panel qui contient le document PDF */
|
||||
JPanel mainPanel = new JPanel();
|
||||
mainPanel.add(fichierPDF);
|
||||
|
||||
/* Crée l’élément scrollable qui contiendra le pannel */
|
||||
JScrollPane scrollPane = new JScrollPane(mainPanel);
|
||||
JPanel pdfPanel = new JPanel();
|
||||
/* Crée l’élément scrollable */
|
||||
JScrollPane scrollPane = new JScrollPane(pdfPanel);
|
||||
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
|
||||
|
||||
/* Ajoute l'élément scrollable a la fenêtre et le centre */
|
||||
this.add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
/* Génère les pages */
|
||||
fichierPDF.loadPages(this, 1);
|
||||
documentPDF = new PDF(fichier);
|
||||
pdfPanel.add(documentPDF);
|
||||
|
||||
/* Ajoute le scrollPane et le centre dans la page */
|
||||
|
||||
/* Charge les pages */
|
||||
documentPDF.loadPages();
|
||||
|
||||
/* Met à jour la page */
|
||||
this.setTitle(TITRE + " | " + fichier.getName());
|
||||
this.validate();
|
||||
this.pack();
|
||||
this.setLocationRelativeTo(null);
|
||||
}
|
||||
|
||||
public void dechargerPdf() {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void rechargerPDF() {
|
||||
dechargerPDF();
|
||||
|
||||
/* Crée le panel qui contient le document PDF */
|
||||
JPanel pdfPanel = new JPanel();
|
||||
/* Crée l’élément scrollable */
|
||||
JScrollPane scrollPane = new JScrollPane(pdfPanel);
|
||||
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
|
||||
this.add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
documentPDF = new PDF(fichier);
|
||||
pdfPanel.add(documentPDF);
|
||||
|
||||
/* Ajoute le scrollPane et le centre dans la page */
|
||||
|
||||
/* Charge les pages */
|
||||
documentPDF.loadPages();
|
||||
|
||||
/* Met à jour la page */
|
||||
this.setTitle(TITRE + " | " + fichier.getName());
|
||||
this.validate();
|
||||
this.pack();
|
||||
this.setLocationRelativeTo(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void dechargerPDF() {
|
||||
if (this.getContentPane() != null) {
|
||||
this.getContentPane().removeAll();
|
||||
}
|
||||
|
||||
@@ -6,15 +6,12 @@
|
||||
|
||||
package lecteur_pdf.document;
|
||||
|
||||
import lecteur_pdf.affichage.Fenetre;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Définis virtuellement un fichier PDF
|
||||
@@ -36,7 +33,12 @@ public class PDF extends JPanel {
|
||||
*/
|
||||
private final PDDocument document;
|
||||
|
||||
private List pages;
|
||||
/**
|
||||
* Les pages du PDF sous forme de JLabel contenant des images
|
||||
*/
|
||||
private final Page[] pages;
|
||||
|
||||
private float zoom;
|
||||
|
||||
/**
|
||||
* Crée un document PDF qui est capable d’être affiché dans une fenêtre
|
||||
@@ -47,7 +49,8 @@ public class PDF extends JPanel {
|
||||
public PDF(File fichier) {
|
||||
try {
|
||||
this.document = PDDocument.load(fichier);
|
||||
pages = this.document.getDocumentCatalog().getAllPages();
|
||||
this.pages = new Page[document.getNumberOfPages()];
|
||||
this.zoom = 1.0f;
|
||||
|
||||
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
||||
this.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
@@ -59,32 +62,15 @@ public class PDF extends JPanel {
|
||||
/**
|
||||
* Charge toutes les pages du document PDF et les stocke dans le tableau
|
||||
*/
|
||||
public void loadPages(Fenetre fenetre, int zoom) {
|
||||
public void loadPages() {
|
||||
|
||||
|
||||
for (Object p : pages) {
|
||||
if (p instanceof PDPage pdPage) {
|
||||
try {
|
||||
this.add(new Page(pdPage, zoom));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
fenetre.validate();
|
||||
|
||||
/* Méthode asynchrone (Ne marche pas bien) */
|
||||
// new Thread(() -> {
|
||||
// try {
|
||||
// this.add(new Page(pdPage, zoom));
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// fenetre.validate();
|
||||
// try {
|
||||
// Thread.sleep(50);
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }).start();
|
||||
for (int i = 0; i < pages.length; i++) {
|
||||
try {
|
||||
pages[i] = new Page(document, i, zoom);
|
||||
this.add(pages[i]);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,4 +83,8 @@ public class PDF extends JPanel {
|
||||
public int getNbPages() {
|
||||
return document.getNumberOfPages();
|
||||
}
|
||||
|
||||
public void setZoom(float zoom) {
|
||||
this.zoom = zoom;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
package lecteur_pdf.document;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
@@ -23,42 +24,102 @@ import java.io.IOException;
|
||||
*/
|
||||
public class Page extends JLabel {
|
||||
|
||||
/** Indice de la page dans le document */
|
||||
private final int INDEX;
|
||||
|
||||
/** Zoom de la page (1.0f == 100 %, 0.5 == 50%, ...) */
|
||||
private final float ZOOM;
|
||||
|
||||
/** Hauteur de la page */
|
||||
private int hauteur;
|
||||
|
||||
/** Largeur de la page */
|
||||
private int largeur;
|
||||
|
||||
/** Image de la page générée */
|
||||
private final ImageIcon IMAGE_ICON;
|
||||
|
||||
/**
|
||||
* Crée une page virtuellement pour l’afficher avec java swing
|
||||
*
|
||||
* @param pageImage
|
||||
* @param scale
|
||||
* @param document Document pdf
|
||||
* @param index indice de la page
|
||||
* @throws IllegalArgumentException Si les arguments ne sont pas valides
|
||||
* @throws IOException Si la page n’as pas pu être lue
|
||||
*/
|
||||
public Page(PDPage pageImage, int scale) throws
|
||||
public Page(PDDocument document, int index) throws
|
||||
IllegalArgumentException,
|
||||
IOException {
|
||||
if (pageImage == null) throw new IllegalArgumentException();
|
||||
if (!isValid(document, index)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.setIcon(generateImage(pageImage.convertToImage(), scale));
|
||||
this.INDEX = index;
|
||||
this.ZOOM = 1.0f;
|
||||
this.IMAGE_ICON = generateImage(document, ZOOM);
|
||||
|
||||
/* Render */
|
||||
this.setIcon(IMAGE_ICON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée une page virtuellement pour l’afficher avec java swing
|
||||
*
|
||||
* @param document Document pdf
|
||||
* @param index indice de la page
|
||||
* @param zoom Le zoom de la page
|
||||
* @throws IllegalArgumentException Si les arguments ne sont pas valides
|
||||
* @throws IOException Si la page n’as pas pu être lue
|
||||
*/
|
||||
public Page(PDDocument document, int index, float zoom) throws
|
||||
IllegalArgumentException,
|
||||
IOException {
|
||||
if (!isValid(document, index)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.INDEX = index;
|
||||
this.ZOOM = zoom;
|
||||
this.IMAGE_ICON = generateImage(document, ZOOM);
|
||||
|
||||
/* Render */
|
||||
this.setIcon(IMAGE_ICON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat qui vérifie si une page est valide
|
||||
* Le document ne doit pas être null et l’index doit être compris entre 0
|
||||
* et le nombre de pages du pdf.
|
||||
*
|
||||
* @param document Document pdf
|
||||
* @param index indice de la page
|
||||
* @return true si le prédicat est vérifié, false sinon
|
||||
*/
|
||||
private boolean isValid(PDDocument document, int index) {
|
||||
return document != null && 0 <= index
|
||||
&& index < document.getNumberOfPages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère une image de la page
|
||||
*
|
||||
* @param img
|
||||
* @param scale
|
||||
* @param document Document PDF
|
||||
* @return JLabel contenant la page sous forme d’image
|
||||
* @throws IOException En cas d’erreur de lecture
|
||||
*/
|
||||
private ImageIcon generateImage(BufferedImage img, int scale) throws IOException {
|
||||
private ImageIcon generateImage(PDDocument document, float scale) throws IOException {
|
||||
|
||||
this.largeur = img.getWidth() / scale;
|
||||
this.hauteur = img.getHeight() / scale;
|
||||
final int DPI = 120;
|
||||
int imageScale = (scale > 1.0f) ? Image.SCALE_SMOOTH : Image.SCALE_FAST;
|
||||
|
||||
return new ImageIcon(img.getScaledInstance(largeur, hauteur,
|
||||
Image.SCALE_SMOOTH));
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(INDEX, DPI);
|
||||
|
||||
this.largeur = (int)(bufferedImage.getWidth() * scale);
|
||||
this.hauteur = (int)(bufferedImage.getHeight() * scale);
|
||||
|
||||
return new ImageIcon(bufferedImage.getScaledInstance(largeur, hauteur,
|
||||
imageScale));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -102,10 +102,10 @@ public class Menu extends JMenuBar {
|
||||
switch (choice) {
|
||||
case "Ouvrir" -> {
|
||||
File fichier = SelectionnerFichier.ouvrirFichier();
|
||||
fenetre.chargerPdf(fichier);
|
||||
fenetre.chargerPDF(fichier);
|
||||
}
|
||||
case "Fermer" -> System.exit(0);
|
||||
// TODO à changer pour que ça quitte vraiment
|
||||
case "Fermer" -> System.exit(
|
||||
0); // TODO à changer pour que ça quitte vraiment
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user