diff --git a/pom.xml b/pom.xml index 2d0db81..20fabe2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.amastigote unstamper - 0.1.0 + 0.1.1 text stamp remover for PDF files pdf-unstamper https://github.com/hwding/pdf-unstamper diff --git a/script/install b/script/install index 9bd9036..881e9fc 100755 --- a/script/install +++ b/script/install @@ -7,7 +7,7 @@ user_bin=`echo ~`"/bin/" jar_name="pdf-unstamper.jar" exe_name="unstamp" -_version="0.1.0" +_version="0.1.1" jar_durl="https://github.com/hwding/pdf-unstamper/releases/download/$_version/$jar_name" function chk_f() { diff --git a/src/com/amastigote/unstamper/Main.java b/src/com/amastigote/unstamper/Main.java index 0b19f21..45a2739 100644 --- a/src/com/amastigote/unstamper/Main.java +++ b/src/com/amastigote/unstamper/Main.java @@ -16,6 +16,12 @@ public class Main { + static { + /* Disable Logging in Apache PDFBox */ + System.setProperty("org.apache.commons.logging.Log", + "org.apache.commons.logging.impl.NoOpLog"); + } + public static void main(String[] args) { CommandLine commandLine = null; try { diff --git a/src/com/amastigote/unstamper/core/Processor.java b/src/com/amastigote/unstamper/core/Processor.java index 4c03321..ac89c8e 100644 --- a/src/com/amastigote/unstamper/core/Processor.java +++ b/src/com/amastigote/unstamper/core/Processor.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -37,33 +38,51 @@ public static void process(File file, String[] strings) { /* START: loading font resources for further parsing */ PDFStreamParser pdfStreamParser = new PDFStreamParser(pdPage); pdfStreamParser.parse(); - List objects = pdfStreamParser.getTokens(); - List cosNames = objects.parallelStream() - .filter(e -> e instanceof COSName) - .collect(Collectors.toList()); - Set pdFonts = new HashSet<>(); - cosNames.forEach(e -> { - try { - PDFont pdFont = pdPage.getResources().getFont(((COSName) e)); - if (pdFont != null) - pdFonts.add(pdFont); - } catch (IOException ignored) { - } - }); + + List objects = + Collections.synchronizedList(pdfStreamParser.getTokens()); + + List cosNames = + objects.parallelStream() + .filter(e -> e instanceof COSName) + .collect(Collectors.toList()); + + Set pdFonts = + Collections.synchronizedSet(new HashSet<>()); + + cosNames.parallelStream() + .forEach(e -> { + /* Ignore Any Exception During Parallel Processing */ + try { + PDFont pdFont = pdPage.getResources().getFont(((COSName) e)); + if (pdFont != null) + pdFonts.add(pdFont); + } catch (Exception ignored) { + } + }); /* END */ - for (Object o : objects) { - if (o instanceof COSString) { - if (TextStampRecognizer.recognize(strings, ((COSString) o).getBytes(), pdFonts)) - ((COSString) o).setValue(new byte[0]); - } - } + objects + .parallelStream() + .forEach(e -> { + if (e instanceof COSString) { + /* Ignore Any Exception During Parallel Processing */ + try { + if (TextStampRecognizer.recognize(strings, ((COSString) e).getBytes(), pdFonts)) + ((COSString) e).setValue(new byte[0]); + } catch (Exception ignored) { + } + } + } + ); + PDStream newContents = new PDStream(pdDocument); OutputStream out = newContents.createOutputStream(); ContentStreamWriter writer = new ContentStreamWriter(out); writer.writeTokens(objects); out.close(); + pdPage.setContents(newContents); - } catch (IOException e) { + } catch (Exception e) { GeneralLogger.Processor.errorProcess(file.getName()); } }); diff --git a/src/com/amastigote/unstamper/log/GeneralLogger.java b/src/com/amastigote/unstamper/log/GeneralLogger.java index 3802a05..c85f8ca 100644 --- a/src/com/amastigote/unstamper/log/GeneralLogger.java +++ b/src/com/amastigote/unstamper/log/GeneralLogger.java @@ -10,7 +10,7 @@ public class GeneralLogger { public static class Help { private static final String usage = - "\nPDF-UnStamper ver. 0.1.0 by hwding@GitHub\n" + + "\nPDF-UnStamper ver. 0.1.1 by hwding@GitHub\n" + "\nUsage: " + "\n [OPTION] -i [INPUT PDF] -k [KEYWORDS...] (-o [OUTPUT PDF])" + "\n [OPTION] -I [INPUT DIR] -k [KEYWORDS...] (-O [OUTPUT DIR])\n" +