Skip to content

Commit 78631f6

Browse files
committed
Source and destination parameter support
1 parent f45307c commit 78631f6

File tree

4 files changed

+86
-24
lines changed

4 files changed

+86
-24
lines changed
0 Bytes
Binary file not shown.
708 Bytes
Binary file not shown.

dist/PDFFlattener.jar

425 Bytes
Binary file not shown.

src/ca/saultstemarie/pdfflattener/FlattenPDF.java

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,32 @@
2424

2525
public class FlattenPDF {
2626

27-
public static int IMAGE_DPI = 200;
27+
/**
28+
* Whether or not debug output is printed to the console.
29+
*/
2830
public static boolean SHOW_DEBUG = true;
31+
32+
/**
33+
* The suggested prefix to add to output files.
34+
*/
35+
public static String DESTINATION_FILENAME_PREFIX = "flat--";
36+
37+
/**
38+
* The DPI that should be used when generating images.
39+
* Higher DPI increases the memory requirements and output file sizes, but also produces sharper images.
40+
*/
41+
public static int IMAGE_DPI = 200;
42+
2943

3044
public static void main(String[] args) {
3145

3246
// Settings
3347

3448
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
49+
50+
/*
51+
* Set up JFileChooser
52+
*/
3553

3654
try {
3755
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@@ -40,50 +58,94 @@ public static void main(String[] args) {
4058
// ignore and use default
4159
}
4260

61+
JFileChooser fileChooser = new JFileChooser();
62+
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));
63+
fileChooser.setAcceptAllFileFilterUsed(false);
64+
4365
/*
4466
* Get the source file
4567
*/
4668

47-
JFileChooser fileChooser = new JFileChooser();
48-
49-
fileChooser.setDialogTitle("Select the Source PDF File to Flatten");
50-
fileChooser.setApproveButtonText("Set Source PDF");
51-
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));
52-
fileChooser.setAcceptAllFileFilterUsed(false);
69+
File sourceFile = null;
5370

54-
int returnValue = fileChooser.showOpenDialog(null);
71+
boolean sourceFileFromArgument = false;
5572

56-
if (returnValue != JFileChooser.APPROVE_OPTION) {
57-
JOptionPane.showMessageDialog(null, "No Source PDF file selected.", "Error", JOptionPane.ERROR_MESSAGE);
58-
System.exit(1);
73+
if (args.length >= 1) {
74+
sourceFile = new File(args[0]);
75+
76+
if (!sourceFile.exists() || !sourceFile.isFile() || !sourceFile.canRead() || !sourceFile.getName().toLowerCase().endsWith(".pdf")) {
77+
JOptionPane.showMessageDialog(null, "Invalid source file.\n" +
78+
args[0], "Error", JOptionPane.ERROR_MESSAGE);
79+
System.exit(1);
80+
}
81+
else {
82+
sourceFileFromArgument = true;
83+
}
5984
}
6085

61-
File sourceFile = fileChooser.getSelectedFile();
86+
if (!sourceFileFromArgument) {
87+
88+
fileChooser.setDialogTitle("Select the Source PDF File to Flatten");
89+
fileChooser.setApproveButtonText("Set Source PDF");
90+
91+
int returnValue = fileChooser.showOpenDialog(null);
92+
93+
if (returnValue != JFileChooser.APPROVE_OPTION) {
94+
JOptionPane.showMessageDialog(null, "No Source PDF file selected.", "Error", JOptionPane.ERROR_MESSAGE);
95+
System.exit(1);
96+
}
97+
98+
sourceFile = fileChooser.getSelectedFile();
99+
}
62100

63101
/*
64102
* Get the destination file
65103
*/
66-
67-
File destinationFile = new File(sourceFile.getAbsolutePath() + File.separator + "FLAT-" + sourceFile.getName());
68-
69-
fileChooser.setDialogTitle("Select the Destination PDF File");
70-
fileChooser.setApproveButtonText("Set Destination PDF");
71-
fileChooser.setSelectedFile(destinationFile);
72104

73-
returnValue = fileChooser.showSaveDialog(null);
105+
File destinationFile = null;
106+
boolean destinationFileFromArgument = false;
74107

75-
if (returnValue != JFileChooser.APPROVE_OPTION) {
76-
JOptionPane.showMessageDialog(null, "No Destination PDF file selected.", "Error", JOptionPane.ERROR_MESSAGE);
77-
System.exit(1);
108+
if (args.length >= 2) {
109+
destinationFile = new File(args[1]);
110+
111+
if (!sourceFile.getName().toLowerCase().endsWith(".pdf")) {
112+
JOptionPane.showMessageDialog(null, "Invalid destination file.\n" +
113+
args[0], "Error", JOptionPane.ERROR_MESSAGE);
114+
System.exit(1);
115+
}
116+
else {
117+
destinationFileFromArgument = true;
118+
}
78119
}
79120

80-
destinationFile = fileChooser.getSelectedFile();
121+
if (!destinationFileFromArgument) {
122+
123+
destinationFile = new File(sourceFile.getAbsolutePath() + File.separator + DESTINATION_FILENAME_PREFIX + sourceFile.getName());
124+
125+
fileChooser.setDialogTitle("Select the Destination PDF File");
126+
fileChooser.setApproveButtonText("Set Destination PDF");
127+
fileChooser.setSelectedFile(destinationFile);
128+
129+
int returnValue = fileChooser.showSaveDialog(null);
130+
131+
if (returnValue != JFileChooser.APPROVE_OPTION) {
132+
JOptionPane.showMessageDialog(null, "No Destination PDF file selected.", "Error", JOptionPane.ERROR_MESSAGE);
133+
System.exit(1);
134+
}
135+
136+
destinationFile = fileChooser.getSelectedFile();
137+
}
81138

82139
if (sourceFile.getPath().equals(destinationFile.getPath()) && sourceFile.getName().equals(destinationFile.getName())) {
83140
JOptionPane.showMessageDialog(null, "You cannot select the same PDF file as both the source and the destination.", "Error", JOptionPane.ERROR_MESSAGE);
84141
System.exit(1);
85142
}
86143

144+
if (!destinationFile.getName().toLowerCase().endsWith(".pdf")) {
145+
destinationFile = new File(destinationFile.getAbsolutePath() + ".pdf");
146+
}
147+
148+
87149
/*
88150
* Do the flattening
89151
*/
@@ -96,7 +158,7 @@ public static void main(String[] args) {
96158
"Ready to Start",
97159
JOptionPane.INFORMATION_MESSAGE);
98160

99-
returnValue = flattenPDF (sourceFile, destinationFile);
161+
int returnValue = flattenPDF (sourceFile, destinationFile);
100162

101163
if (returnValue == 0) {
102164
JOptionPane.showMessageDialog(null, "The PDF file was flattened successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);

0 commit comments

Comments
 (0)