Skip to content

Commit

Permalink
command line arguments, remove binaries, upd README and misc
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanpas committed Oct 9, 2018
1 parent 0ab2a06 commit 51f8bd0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 62 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# PDF Merger
Welcome to PDF Merger home page!

This [program](target/pdfmerger.jar?raw=true) merges several pdfs into one and creates handy outline (tree of bookmarks).
This program merges several pdfs into one and creates handy outline (tree of bookmarks).
For example: doc1.pdf with outline:
* Header 1

Expand All @@ -16,10 +16,11 @@ will be merged into merged.pdf with the following outline:
* Header 2
* subheader 2

Usage: `java -jar pdfmerger.jar file1.pdf file2.pdf ... out.pdf`
Building: `mvn compile assembly:single`
Usage: `java -jar pdfmerger.jar file1.pdf file2.pdf ... out.pdf`
If you want to use it your linux system there is sh wrapper at src/pdfmerger

Running without arguments launches Swing GUI:

![GUI](http://i.imgur.com/dMoCWSf.png)

You may also create and install program via `sudo make install` and uninstall via `sudo make uninstall`. For Debian-based distro use `sudo checkinstall -D` for installing the package. Run anywhere right out of console: `pdfmerger [arguments ...]`.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

<groupId>yanpas</groupId>
<artifactId>pdfmerger</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Expand All @@ -30,6 +31,8 @@
<archive>
<manifest>
<mainClass>yanpas.pdfmerger.PdfMerger</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
<descriptorRefs>
Expand Down
82 changes: 58 additions & 24 deletions src/main/java/yanpas/pdfmerger/PdfMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,72 @@
public class PdfMerger {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("CLI usage:\tpdfmerger file1.pdf file2.pdf ... out.pdf");
SwingMerge gui = new SwingMerge();
gui.setVisible(true);
} else {
List<File> infiles = new ArrayList<>();
String outname = args[args.length - 1];
if (new File(outname).exists()) {
System.err.println("The last argument must be output file name");
System.exit(1);
}
for (int i = 0; i < args.length - 1; i++) {
File tmp = new File(args[i]);
if (!tmp.exists()) {
System.err.println("File " + args[i] + " does not exist");
return;
}

List<File> infiles = new ArrayList<>();
boolean progress = false;
String outname = null;
int i = 0;

for (String arg : args) {
switch (arg) {
case "-v":
case "--version":
System.out.println(PdfMerger.class.getPackage().getImplementationVersion());
return;
case "-h":
case "--help":
System.out.println("Usage: pdfmerger file1.pdf file2.pdf ... out.pdf");
System.out.println("Run pdfmerger without arguments to launch GUI");
System.out.println(" -h | --help Print this message and exit");
System.out.println(" -v | --version Print version and exit");
System.out.println(" --progress Log progress to stdout");
return;
case "--progress":
progress = true;
break;
default:
File targetFile = new File(arg);
if (i == args.length - 1) {
if (targetFile.exists()) {
System.err.println("The last argument must be output file name");
System.exit(1);
}
outname = arg;
break;
}
if (!targetFile.exists()) {
System.err.println("File " + arg + " does not exist");
System.exit(1);
}
infiles.add(tmp);
infiles.add(targetFile);
break;
}
i++;
}

try (Merger m = new Merger()) {
for (File file : infiles) {
if (infiles.isEmpty()) {
System.err.println("No input files supplied (last file should be output file)");
System.exit(1);
}

try (Merger m = new Merger()) {
for (File file : infiles) {
if (progress)
System.out.println("Processing " + file.getName());
m.addDocument(file);
}
System.out.println("Saving");
m.save(outname);
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
System.err.println("Stack trace:");
e.printStackTrace();
System.exit(1);
m.addDocument(file);
}
if (progress)
System.out.println("Saving");
m.save(outname);
} catch (IOException e) {
System.err.println("Error: " + e.getLocalizedMessage());
System.err.println("Stack trace:");
e.printStackTrace();
System.exit(1);
}
}
}
58 changes: 25 additions & 33 deletions src/main/java/yanpas/pdfmerger/SwingMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,25 @@ private final void placeAllElements() {
}

private final void addEvents() {
addButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent ae) {
FileDialog fileChooser = new FileDialog(SwingMerge.this);
fileChooser.setMultipleMode(true);
fileChooser.setFilenameFilter((File arg0, String arg1) -> {
if (arg1.length() >= 5 && arg1.substring(arg1.length() - 4, arg1.length()).equals(".pdf"))
return true;
return false;
});
fileChooser.setVisible(true);
File[] farray = fileChooser.getFiles();
for (File f : farray)
if (f.isFile())
flistModel.addElement(f);
else {
JOptionPane.showMessageDialog(SwingMerge.this,
"Selected item:\n" + f.getAbsolutePath() + "\nis not a file", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
}
addButton.addActionListener((ActionEvent ae) -> {
FileDialog fileChooser = new FileDialog(SwingMerge.this);
fileChooser.setMultipleMode(true);
fileChooser.setFilenameFilter((File arg0, String arg1) -> {
if (arg1.length() >= 5 && arg1.substring(arg1.length() - 4, arg1.length()).equals(".pdf"))
return true;
return false;
});
fileChooser.setVisible(true);
File[] farray = fileChooser.getFiles();
for (File f : farray)
if (f.isFile())
flistModel.addElement(f);
else {
JOptionPane.showMessageDialog(SwingMerge.this,
"Selected item:\n" + f.getAbsolutePath() + "\nis not a file", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
});
removeButton.addActionListener((ActionEvent ae) -> {
int[] selected = fstringList.getSelectedIndices();
Expand Down Expand Up @@ -180,16 +176,12 @@ public void actionPerformed(ActionEvent ae) {
farr[i] = flistModel.get(i);
final Worker worker = new Worker(farr, (fileChooser.getDirectory() + outpath));
ActionListener cancelListner = (ActionEvent e) -> worker.cancel(true);
worker.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent event) {
switch (event.getPropertyName()) {
case "progress":
progressBar.setIndeterminate(false);
progressBar.setValue((Integer) event.getNewValue());
break;
}
worker.addPropertyChangeListener((PropertyChangeEvent event) -> {
switch (event.getPropertyName()) {
case "progress":
progressBar.setIndeterminate(false);
progressBar.setValue((Integer) event.getNewValue());
break;
}
});
progressBar.setValue(0);
Expand Down
Binary file removed src/main/resources/commons-logging.jar
Binary file not shown.
Binary file removed src/main/resources/pdfbox.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/pdfmerger.sh → src/pdfmerger
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
#!/bin/sh
exec java -jar /opt/pdfmerger.jar "$@"

0 comments on commit 51f8bd0

Please sign in to comment.