mirror of
https://github.com/LucasVbr/LecteurPdfDoubleAffichage.git
synced 2026-05-16 09:05:34 +00:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -23,52 +23,49 @@ import java.io.IOException;
|
||||
* @author Noé VILLENEUVE
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
public class Fenetre extends JFrame {
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
/** Titre de la fenêtre */
|
||||
private final String TITRE = "Lecteur PDF";
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
/** Le menu de la fenêtre */
|
||||
private Menu menu;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*/
|
||||
/** Le fichier courant */
|
||||
private File fichier;
|
||||
|
||||
/** Le document PDF courant */
|
||||
public PDF documentPDF;
|
||||
|
||||
/** Défini la disposition d'affichage des pages du document PDF */
|
||||
private boolean affichageVertical;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* Crée une nouvelle fenêtre contenant un Menu
|
||||
*/
|
||||
public Fenetre() {
|
||||
|
||||
/* Change le style de la fenêtre */
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException
|
||||
| InstantiationException
|
||||
| IllegalAccessException
|
||||
| UnsupportedLookAndFeelException e) {
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Création du menu
|
||||
/* Création du menu */
|
||||
menu = new Menu(this);
|
||||
this.setJMenuBar(menu);
|
||||
|
||||
/* Definis l'affichage Vertical comme affichage par défaut */
|
||||
this.affichageVertical = true;
|
||||
|
||||
/* Initialise l'état par défaut de la fenêtre */
|
||||
setup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fonction utilitaire qui permet de définir l'état par défaut de la fenêtre
|
||||
*/
|
||||
public void setup() {
|
||||
setTitle(TITRE);
|
||||
|
||||
@@ -83,11 +80,42 @@ public class Fenetre extends JFrame {
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* Charge le document PDF courant si possible
|
||||
*
|
||||
* @param fichier
|
||||
* @throws IOException Si l'on arrive pas a charger le PDF
|
||||
*/
|
||||
public void chargerPDF() throws IOException {
|
||||
if (haveDocument()) chargerPDF(fichier, 1.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge le document PDF courant avec un zoom spécifique si possible
|
||||
*
|
||||
* @param zoom La taille du zoom sur les pages
|
||||
* @throws IOException Si l'on arrive pas a charger le PDF
|
||||
*/
|
||||
public void chargerPDF(float zoom) throws IOException {
|
||||
if (haveDocument()) chargerPDF(fichier, zoom);
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge un document PDF
|
||||
*
|
||||
* @param fichier Le fichier PDF à afficher dans la fenêtre
|
||||
* @throws IOException Si l'on arrive pas a charger le PDF
|
||||
*/
|
||||
public void chargerPDF(File fichier) throws IOException {
|
||||
chargerPDF(fichier, 1.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge un document PDF
|
||||
*
|
||||
* @param fichier Le fichier PDF à afficher dans la fenêtre
|
||||
* @param zoom La taille du zoom sur les pages
|
||||
* @throws IOException Si l'on arrive pas a charger le PDF
|
||||
*/
|
||||
public void chargerPDF(File fichier, float zoom) throws IOException {
|
||||
dechargerPDF();
|
||||
|
||||
this.fichier = fichier;
|
||||
@@ -100,50 +128,10 @@ public class Fenetre extends JFrame {
|
||||
scrollPane.getHorizontalScrollBar().setUnitIncrement(16);
|
||||
this.add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
documentPDF = new PDF(fichier, affichageVertical);
|
||||
pdfPanel.add(documentPDF);
|
||||
|
||||
/* Charge les pages */
|
||||
documentPDF.loadPages();
|
||||
|
||||
/* Met à jour la page */
|
||||
this.setTitle(TITRE + " | " + fichier.getName());
|
||||
this.validate();
|
||||
this.pack();
|
||||
this.setLocationRelativeTo(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void rechargerPDF() throws IOException {
|
||||
rechargerPDF(1.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param zoom
|
||||
* @throws IOException
|
||||
*/
|
||||
public void rechargerPDF(float zoom) throws IOException {
|
||||
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);
|
||||
scrollPane.getHorizontalScrollBar().setUnitIncrement(16);
|
||||
this.add(scrollPane);
|
||||
|
||||
documentPDF = new PDF(fichier, affichageVertical);
|
||||
documentPDF.setZoom(zoom);
|
||||
pdfPanel.add(documentPDF);
|
||||
|
||||
/* Charge les pages */
|
||||
documentPDF.loadPages();
|
||||
|
||||
/* Met à jour la page */
|
||||
this.setTitle(TITRE + " | " + fichier.getName());
|
||||
this.validate();
|
||||
@@ -152,7 +140,7 @@ public class Fenetre extends JFrame {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Supprime le PDF de la fenêtre
|
||||
*/
|
||||
public void dechargerPDF() {
|
||||
if (this.getContentPane() != null) {
|
||||
@@ -161,24 +149,22 @@ public class Fenetre extends JFrame {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat qui vérifie si un PDF est affiché dans la fenêtre
|
||||
*
|
||||
* @return true si le prédicat est vérifié, false sinon
|
||||
*/
|
||||
public boolean haveDocument() {
|
||||
return documentPDF != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Définis le mode d'affichage du PDF
|
||||
*
|
||||
* @param affichageVertical true pour un affichage Vertical, false pour
|
||||
* un affichage horizontal
|
||||
*/
|
||||
public void setAffichageVertical(boolean affichageVertical) {
|
||||
this.affichageVertical = affichageVertical;
|
||||
}
|
||||
|
||||
public boolean isAffichageVertical() {
|
||||
return affichageVertical;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO comment main
|
||||
*
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
new Fenetre();
|
||||
}
|
||||
}
|
||||
@@ -1,58 +1,48 @@
|
||||
/*
|
||||
* gestionFenetre, 22/11/2021
|
||||
* IUT Rodez 2021, INFO2
|
||||
* pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package lecteur_pdf.affichage;
|
||||
|
||||
import lecteur_pdf.document.PDF;
|
||||
import lecteur_pdf.menu.Menu;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* classe de gestion des fenêtres
|
||||
*
|
||||
* @author Léo FRANCH
|
||||
* @author Tristan NOGARET
|
||||
* @author Lucàs VABRE
|
||||
* @author Noé VILLENEUVE
|
||||
* @version 1.0
|
||||
*/
|
||||
public class gestionFenetre {
|
||||
|
||||
|
||||
final int NB_MAX_FENETRE = 2 ;
|
||||
private Fenetre[] fenetresOuvertes;
|
||||
File fichier = null ;
|
||||
|
||||
|
||||
public void ouvrirFenetre(){
|
||||
int placeVide;
|
||||
for (placeVide=0; placeVide<2; placeVide++){
|
||||
if (fenetresOuvertes[placeVide]==null){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(placeVide==2) {
|
||||
//todo erreur trop de fenetres
|
||||
}else {
|
||||
Fenetre fenetre = new Fenetre();
|
||||
fenetresOuvertes[placeVide] = fenetre;
|
||||
}
|
||||
}
|
||||
public void fermerFenetre(JFrame fenetre){
|
||||
for (int i = 0; i<2 ; i++){
|
||||
if (fenetre==fenetresOuvertes[i]){
|
||||
fenetre.dispose();
|
||||
fenetresOuvertes[i]= null ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
///*
|
||||
// * gestionFenetre, 22/11/2021
|
||||
// * IUT Rodez 2021, INFO2
|
||||
// * pas de copyright, aucun droits
|
||||
// */
|
||||
//
|
||||
//package lecteur_pdf.affichage;
|
||||
//
|
||||
//import javax.swing.*;
|
||||
//import java.io.File;
|
||||
//
|
||||
///**
|
||||
// * classe de gestion des fenêtres
|
||||
// *
|
||||
// * @author Léo FRANCH
|
||||
// * @author Tristan NOGARET
|
||||
// * @author Lucàs VABRE
|
||||
// * @author Noé VILLENEUVE
|
||||
// * @version 1.0
|
||||
// */
|
||||
//public class gestionFenetre {
|
||||
//
|
||||
// final int NB_MAX_FENETRE = 2 ;
|
||||
// private Fenetre[] fenetresOuvertes;
|
||||
// File fichier = null ;
|
||||
//
|
||||
//
|
||||
// public void ouvrirFenetre(){
|
||||
// int placeVide;
|
||||
// for (placeVide=0; placeVide<2; placeVide++){
|
||||
// if (fenetresOuvertes[placeVide] == null) break;
|
||||
// }
|
||||
// if(placeVide==2) {
|
||||
// //todo erreur trop de fenetres
|
||||
// }else {
|
||||
// Fenetre fenetre = new Fenetre();
|
||||
// fenetresOuvertes[placeVide] = fenetre;
|
||||
// }
|
||||
// }
|
||||
// public void fermerFenetre(JFrame fenetre){
|
||||
// for (int i = 0; i<2 ; i++){
|
||||
// if (fenetre==fenetresOuvertes[i]){
|
||||
// fenetre.dispose();
|
||||
// fenetresOuvertes[i]= null ;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -23,27 +23,19 @@ import java.io.IOException;
|
||||
*/
|
||||
public class PDF extends JPanel {
|
||||
|
||||
/**
|
||||
* Espacement entre chaque page
|
||||
*/
|
||||
public static final int OFFSET_PAGES = 10; // px
|
||||
|
||||
/**
|
||||
* Document PDF chargé
|
||||
*/
|
||||
/** Document chargé */
|
||||
private final PDDocument document;
|
||||
|
||||
/**
|
||||
* Les pages du PDF sous forme de JLabel contenant des images
|
||||
*/
|
||||
/** Les pages chargés */
|
||||
private final Page[] pages;
|
||||
|
||||
/** Le zoom des pages du document */
|
||||
private float zoom;
|
||||
|
||||
/**
|
||||
* Crée un document PDF qui est capable d’être affiché dans une fenêtre
|
||||
* Crée un document PDF affichable dans une fenêtre
|
||||
*
|
||||
* @param fichier Le fichier PDF que l’on veut ouvrir
|
||||
* @param fichier Le fichier que l’on veut ouvrir
|
||||
* @throws IllegalArgumentException si le fichier n’existe pas
|
||||
*/
|
||||
public PDF(File fichier, boolean vertical) throws IOException {
|
||||
@@ -67,18 +59,15 @@ public class PDF extends JPanel {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("PDF: Loaded successfully");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Le nombre de pages chargées
|
||||
* Défini le zoom des pages du document
|
||||
*
|
||||
* @param zoom la nouvelle valeur du zoom (1.0f == 100%)
|
||||
*/
|
||||
public int getNbPages() {
|
||||
return document.getNumberOfPages();
|
||||
}
|
||||
|
||||
public void setZoom(float zoom) {
|
||||
this.zoom = zoom;
|
||||
loadPages();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,12 +30,6 @@ public class Page extends JLabel {
|
||||
/** 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;
|
||||
|
||||
@@ -74,15 +68,13 @@ public class Page extends JLabel {
|
||||
public Page(PDDocument document, int index, float zoom) throws
|
||||
IllegalArgumentException,
|
||||
IOException {
|
||||
if (!isValid(document, index)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (!isValid(document, index)) throw new IllegalArgumentException();
|
||||
|
||||
this.INDEX = index;
|
||||
this.ZOOM = zoom;
|
||||
this.IMAGE_ICON = generateImage(document);
|
||||
this.IMAGE_ICON = generateImage(document); // Génère la page
|
||||
|
||||
/* Render */
|
||||
/* Fait le rendu de la page */
|
||||
this.setIcon(IMAGE_ICON);
|
||||
}
|
||||
|
||||
@@ -109,30 +101,20 @@ public class Page extends JLabel {
|
||||
*/
|
||||
private ImageIcon generateImage(PDDocument document) throws IOException {
|
||||
|
||||
/* Défini la qualité de l’image par rapport au zoom */
|
||||
final int DPI = 120;
|
||||
int imageScale = (ZOOM > 1.0f) ? Image.SCALE_SMOOTH : Image.SCALE_FAST;
|
||||
|
||||
/* Crée une image de la page à afficher */
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(INDEX, DPI);
|
||||
|
||||
this.largeur = (int)(bufferedImage.getWidth() * ZOOM);
|
||||
this.hauteur = (int)(bufferedImage.getHeight() * ZOOM);
|
||||
/* Redimensionne l'image */
|
||||
final int LARGEUR = (int)(bufferedImage.getWidth() * ZOOM);
|
||||
final int HAUTEUR = (int)(bufferedImage.getHeight() * ZOOM);
|
||||
|
||||
return new ImageIcon(bufferedImage.getScaledInstance(largeur, hauteur,
|
||||
/* Retourne l’image à la bonne taille */
|
||||
return new ImageIcon(bufferedImage.getScaledInstance(LARGEUR, HAUTEUR,
|
||||
imageScale));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return La hauteur de la page
|
||||
*/
|
||||
public int getHauteur() {
|
||||
return hauteur;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return La largeur de la page
|
||||
*/
|
||||
public int getLargeur() {
|
||||
return largeur;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,21 +239,21 @@ public class Menu extends JMenuBar {
|
||||
}
|
||||
case "Zoom 150%" -> {
|
||||
try {
|
||||
FENETRE.rechargerPDF(2.0f);
|
||||
FENETRE.chargerPDF(2.0f);
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(FENETRE, messageErrCorrompu);
|
||||
}
|
||||
}
|
||||
case "Zoom 100%" -> {
|
||||
try {
|
||||
FENETRE.rechargerPDF(1.0f);
|
||||
FENETRE.chargerPDF(1.0f);
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(FENETRE, messageErrCorrompu);
|
||||
}
|
||||
}
|
||||
case "Zoom 50%" -> {
|
||||
try {
|
||||
FENETRE.rechargerPDF(0.5f);
|
||||
FENETRE.chargerPDF(0.5f);
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(FENETRE, messageErrCorrompu);
|
||||
}
|
||||
@@ -262,7 +262,7 @@ public class Menu extends JMenuBar {
|
||||
if (FENETRE.haveDocument()) {
|
||||
FENETRE.setAffichageVertical(true);
|
||||
try {
|
||||
FENETRE.rechargerPDF();
|
||||
FENETRE.chargerPDF();
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(FENETRE, messageErrCorrompu);
|
||||
}
|
||||
@@ -272,7 +272,7 @@ public class Menu extends JMenuBar {
|
||||
if (FENETRE.haveDocument()) {
|
||||
FENETRE.setAffichageVertical(false);
|
||||
try {
|
||||
FENETRE.rechargerPDF();
|
||||
FENETRE.chargerPDF();
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(FENETRE, messageErrCorrompu);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package lecteur_pdf.affichage;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class FenetreTest {
|
||||
|
||||
// TODO
|
||||
|
||||
/**
|
||||
* Lance les methodes de tests
|
||||
*
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
new Fenetre();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,14 @@
|
||||
package lecteur_pdf.document;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
|
||||
class PDFTest {
|
||||
|
||||
/**
|
||||
* TODO comment main
|
||||
* Lance les methodes de tests
|
||||
*
|
||||
* @param args
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame();
|
||||
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLocationRelativeTo(null);
|
||||
|
||||
frame.setSize(300,300);
|
||||
frame.setBackground(Color.gray);
|
||||
frame.setVisible(true);
|
||||
|
||||
//PDF doc = new PDF(new File("C:/Users/public/test.pdf"));
|
||||
|
||||
//JScrollPane scrollPane = new JScrollPane(doc);
|
||||
//scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
||||
//scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
|
||||
//frame.add(scrollPane);
|
||||
frame.validate();
|
||||
// TODO
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,32 +3,49 @@ package lecteur_pdf.document;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
class PageTest {
|
||||
|
||||
public static void testPage() {
|
||||
try {
|
||||
PDDocument document = PDDocument.load(
|
||||
new File("Documents_PDF/paysage_35pages.pdf"));
|
||||
JFrame frame = new JFrame();
|
||||
frame.setVisible(true);
|
||||
frame.add(new Page(document, 0, 1.0f));
|
||||
frame.pack();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void testPageZoom() {
|
||||
try {
|
||||
PDDocument document = PDDocument.load(
|
||||
new File("Documents_PDF/paysage_35pages.pdf"));
|
||||
JFrame frame = new JFrame();
|
||||
frame.setVisible(true);
|
||||
frame.add(new Page(document, 0, 2.0f));
|
||||
frame.pack();
|
||||
|
||||
JFrame frame2 = new JFrame();
|
||||
frame2.setVisible(true);
|
||||
frame2.add(new Page(document, 0, 0.5f));
|
||||
frame2.pack();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test du constructeur {@link Page#Page(PDDocument, int)}
|
||||
* Lance les methodes de tests
|
||||
*
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) throws IOException {
|
||||
/* Mise en place du JFrame de test */
|
||||
JFrame frame = new JFrame();
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setSize(300, 300);
|
||||
frame.setBackground(Color.gray);
|
||||
frame.setVisible(true);
|
||||
|
||||
/* Creation de l’objet Page */
|
||||
PDDocument document = PDDocument.load(new File("F:/paysage_35pages.pdf"));
|
||||
Page page = new Page(document, 0);
|
||||
|
||||
/* Ajout de l’objet dans la frame et mise à jour de la frame */
|
||||
// frame.add(page);
|
||||
frame.validate();
|
||||
public static void main(String[] args) {
|
||||
testPage();
|
||||
testPageZoom();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user