-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nova versão com conversão de PDF para JPG
- Loading branch information
1 parent
124d239
commit 1edcbd0
Showing
6 changed files
with
109 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters