Skip to content

Commit

Permalink
Nova versão com conversão de PDF para JPG
Browse files Browse the repository at this point in the history
  • Loading branch information
SavioAndres committed May 1, 2021
1 parent 124d239 commit 1edcbd0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 32 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@
<artifactId>tess4j</artifactId>
<version>4.5.4</version>
</dependency>

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.23</version>
</dependency>
</dependencies>
</project>
10 changes: 6 additions & 4 deletions src/application/ConvertScreen.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="274.0" prefWidth="423.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ConvertScreenController">
<children>
<Button layoutX="108.0" layoutY="30.0" mnemonicParsing="false" onAction="#openFile" prefHeight="25.0" prefWidth="218.0" text="Carregar PDF" />
<ComboBox layoutX="61.0" layoutY="97.0" prefHeight="25.0" prefWidth="302.0" />
<ComboBox layoutX="61.0" layoutY="143.0" prefHeight="25.0" prefWidth="302.0" />
<Button layoutX="171.0" layoutY="216.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="155.0" text="Converter para JPG" />
<Button layoutX="108.0" layoutY="216.0" mnemonicParsing="false" onAction="#back" text="Voltar" />
<ComboBox fx:id="cb_typeDoc" layoutX="61.0" layoutY="88.0" onAction="#typeDoc" prefHeight="25.0" prefWidth="302.0" />
<ComboBox fx:id="cb_subTypeDoc" layoutX="61.0" layoutY="133.0" prefHeight="25.0" prefWidth="302.0" />
<Button fx:id="btn_convert" layoutX="171.0" layoutY="200.0" mnemonicParsing="false" onAction="#saveFile" prefHeight="25.0" prefWidth="155.0" text="Converter para JPG" />
<Button layoutX="108.0" layoutY="200.0" mnemonicParsing="false" onAction="#back" text="Voltar" />
<Label fx:id="lb_feedback" layoutX="183.0" layoutY="240.0" />
</children>
</Pane>
108 changes: 89 additions & 19 deletions src/application/ConvertScreenController.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,119 @@
package application;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ResourceBundle;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.stage.FileChooser;
import main.Main;
import organize_files.DocumentType;

public class ConvertScreenController implements Initializable {

private File directory;
private File fileInput;

@FXML
private ComboBox<String> cb_typeDoc;
@FXML
private ComboBox<String> cb_subTypeDoc;
@FXML
private Button btn_convert;
@FXML
private Label lb_feedback;

@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub

cb_typeDoc.getItems().setAll(DocumentType.types());
cb_typeDoc.setDisable(true);
cb_subTypeDoc.setDisable(true);
btn_convert.setDisable(true);
}

@FXML
private void openFile(ActionEvent event) {
DirectoryChooser chooser = new DirectoryChooser();
File directory = chooser.showDialog(Main.stage);
this.directory = directory;
System.out.println(this.directory);
lb_feedback.setText("");
btn_convert.setDisable(true);
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("*.pdf", "*.pdf"));
fileInput = chooser.showOpenDialog(Main.stage);
if (fileInput != null)
cb_typeDoc.setDisable(false);
}

@FXML
private void back(ActionEvent event) throws IOException {
Pane root = FXMLLoader.load(ConvertScreenController.class.getResource("MainScreen.fxml"));
Scene scene = new Scene(root);
private void typeDoc(ActionEvent event) {
if (!cb_typeDoc.getValue().isEmpty())
btn_convert.setDisable(false);

Main.stage.setTitle("Organizador de documentos digitalizados");
Main.stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/icon.png")));
Main.stage.setScene(scene);
cb_subTypeDoc.setValue("");
DocumentType.subTypes(cb_typeDoc.getValue());
cb_subTypeDoc.setDisable(DocumentType.subTypeDisable);

if (!DocumentType.subTypeDisable) {
cb_subTypeDoc.setValue(DocumentType.valueType);
cb_subTypeDoc.getItems().setAll(DocumentType.itens);
}
}

@FXML
private void back(ActionEvent event) {
Main.stage.setScene(Main.scene);
Main.stage.show();
}

private String getNameType() {
return cb_subTypeDoc.getValue().isEmpty() ? cb_typeDoc.getValue() : cb_typeDoc.getValue() + " " + cb_subTypeDoc.getValue();
}

@FXML
private void saveFile(ActionEvent event) throws IOException {
FileChooser chooser = new FileChooser();
chooser.setInitialFileName(getNameType());
chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("*.jpg", "*.jpg"));
File fileOutput = chooser.showSaveDialog(Main.stage);

if (fileOutput != null)
convert(fileInput, fileOutput);
}


private void convert(File pdfFilename, File fileOutput) throws IOException {
lb_feedback.setText("Convertendo...");
PDDocument document = PDDocument.load(pdfFilename);
PDFRenderer pdfRenderer = new PDFRenderer(document);
PDPageTree pages = document.getPages();

for (int i = 0; i < pages.getCount(); i++) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 300, ImageType.GRAY);
ImageIOUtil.writeImage(bim, createFileName(fileOutput, i), 300);
}
document.close();
cb_typeDoc.setDisable(true);
cb_subTypeDoc.setDisable(true);
btn_convert.setDisable(true);
lb_feedback.setText("Convertido");
}

private String createFileName(File fileOutput, int index) {
String file = fileOutput.getAbsolutePath();
file = file.substring(0, file.lastIndexOf('.'));

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

return file + " " + timestamp.getTime() + index + ".jpg";
}

}
5 changes: 4 additions & 1 deletion src/application/MainScreen.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@
</Menu>
<Menu mnemonicParsing="false" text="Converter">
<items>
<MenuItem mnemonicParsing="false" onAction="#convertPDFtoJPG" text="PDF para JPG" />
<MenuItem mnemonicParsing="false" onAction="#convertPDFtoJPG" text="PDF para JPG">
<accelerator>
<KeyCodeCombination alt="UP" code="P" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator></MenuItem>
</items>
</Menu>
<Menu mnemonicParsing="false" text="Ajuda">
Expand Down
6 changes: 1 addition & 5 deletions src/application/MainScreenController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.ScrollEvent;
Expand Down Expand Up @@ -427,11 +426,8 @@ private void zoomOut(ActionEvent event) {

@FXML
private void convertPDFtoJPG(ActionEvent event) throws IOException {
Pane root = FXMLLoader.load(ConvertScreenController.class.getResource("ConvertScreen.fxml"));
Pane root = FXMLLoader.load(getClass().getResource("/application/ConvertScreen.fxml")); // /src/application/ConvertScreen.fxml
Scene scene = new Scene(root);

Main.stage.setTitle("Organizador de documentos digitalizados");
Main.stage.getIcons().add(new Image(getClass().getResourceAsStream("/images/icon.png")));
Main.stage.setScene(scene);
Main.stage.show();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main;

import application.MainScreenController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
Expand All @@ -11,14 +10,15 @@
public class Main extends Application {

public static Stage stage;
public static Scene scene;

@Override
public void start(Stage primaryStage) {
try {
stage = primaryStage;

Pane root = FXMLLoader.load(MainScreenController.class.getResource("MainScreen.fxml"));
Scene scene = new Scene(root);
Pane root = FXMLLoader.load(getClass().getResource("/application/MainScreen.fxml")); // /src/application/MainScreen.fxml
scene = new Scene(root);

primaryStage.setTitle("Organizador de documentos digitalizados");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/icon.png")));
Expand Down

0 comments on commit 1edcbd0

Please sign in to comment.