Skip to content

Commit 08ac529

Browse files
committed
- provide a more elegant and efficient way to load fonts in each page
- bump version & head comment date
1 parent 1615901 commit 08ac529

File tree

4 files changed

+30
-44
lines changed

4 files changed

+30
-44
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.amastigote</groupId>
88
<artifactId>unstamper</artifactId>
9-
<version>0.1.1</version>
10-
<description>text stamp remover for PDF files</description>
9+
<version>0.1.2</version>
10+
<description>Text stamp remover for PDF files.</description>
1111
<name>pdf-unstamper</name>
1212
<url>https://github.com/hwding/pdf-unstamper</url>
1313
<build>

script/install

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/bin/bash
22

33
#AUTH hwding
4-
#DATE AUG/25/2017
4+
#DATE SEP/04/2017
55
#DESC install unstamp as a command
66

77
user_bin=`echo ~`"/bin/"
88
jar_name="pdf-unstamper.jar"
99
exe_name="unstamp"
10-
_version="0.1.1"
10+
_version="0.1.2"
1111
jar_durl="https://github.com/hwding/pdf-unstamper/releases/download/$_version/$jar_name"
1212

1313
function chk_f() {

src/com/amastigote/unstamper/core/Processor.java

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
22
AUTH | hwding
3-
DATE | Aug 27 2017
3+
DATE | Sep 04 2017
44
DESC | text stamp remover for PDF files
55
66
GITH | github.com/hwding
77
*/
88
package com.amastigote.unstamper.core;
99

1010
import com.amastigote.unstamper.log.GeneralLogger;
11-
import org.apache.pdfbox.cos.COSName;
1211
import org.apache.pdfbox.cos.COSString;
1312
import org.apache.pdfbox.pdfparser.PDFStreamParser;
1413
import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
@@ -19,11 +18,9 @@
1918
import java.io.File;
2019
import java.io.IOException;
2120
import java.io.OutputStream;
22-
import java.util.Collections;
2321
import java.util.HashSet;
2422
import java.util.List;
2523
import java.util.Set;
26-
import java.util.stream.Collectors;
2724

2825
public class Processor {
2926
public static void process(File file, String[] strings) {
@@ -35,45 +32,34 @@ public static void process(File file, String[] strings) {
3532
PDDocument pdDocument = PDDocument.load(file);
3633
pdDocument.getPages().forEach(pdPage -> {
3734
try {
38-
/* START: loading font resources for further parsing */
35+
/* START: loading font resources from current page */
3936
PDFStreamParser pdfStreamParser = new PDFStreamParser(pdPage);
4037
pdfStreamParser.parse();
4138

42-
List<Object> objects =
43-
Collections.synchronizedList(pdfStreamParser.getTokens());
39+
List<Object> objects = pdfStreamParser.getTokens();
40+
Set<PDFont> pdFonts = new HashSet<>();
4441

45-
List<Object> cosNames =
46-
objects.parallelStream()
47-
.filter(e -> e instanceof COSName)
48-
.collect(Collectors.toList());
49-
50-
Set<PDFont> pdFonts =
51-
Collections.synchronizedSet(new HashSet<>());
52-
53-
cosNames.parallelStream()
54-
.forEach(e -> {
55-
/* Ignore Any Exception During Parallel Processing */
56-
try {
57-
PDFont pdFont = pdPage.getResources().getFont(((COSName) e));
58-
if (pdFont != null)
59-
pdFonts.add(pdFont);
60-
} catch (Exception ignored) {
61-
}
62-
});
42+
pdPage.getResources().getFontNames().forEach(e -> {
43+
/* Ignore Any Exception During Parallel Processing */
44+
try {
45+
PDFont pdFont = pdPage.getResources().getFont(e);
46+
if (pdFont != null)
47+
pdFonts.add(pdFont);
48+
} catch (Exception ignored) {
49+
}
50+
});
6351
/* END */
64-
objects
65-
.parallelStream()
66-
.forEach(e -> {
67-
if (e instanceof COSString) {
68-
/* Ignore Any Exception During Parallel Processing */
69-
try {
70-
if (TextStampRecognizer.recognize(strings, ((COSString) e).getBytes(), pdFonts))
71-
((COSString) e).setValue(new byte[0]);
72-
} catch (Exception ignored) {
73-
}
74-
}
75-
}
76-
);
52+
53+
objects.parallelStream().forEach(e -> {
54+
if (e instanceof COSString) {
55+
/* Ignore Any Exception During Parallel Processing */
56+
try {
57+
if (TextStampRecognizer.recognize(strings, ((COSString) e).getBytes(), pdFonts))
58+
((COSString) e).setValue(new byte[0]);
59+
} catch (Exception ignored) {
60+
}
61+
}
62+
});
7763

7864
PDStream newContents = new PDStream(pdDocument);
7965
OutputStream out = newContents.createOutputStream();

src/com/amastigote/unstamper/log/GeneralLogger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
AUTH | hwding
3-
DATE | Aug 27 2017
3+
DATE | Sep 04 2017
44
DESC | text stamp remover for PDF files
55
66
GITH | github.com/hwding
@@ -10,7 +10,7 @@
1010
public class GeneralLogger {
1111
public static class Help {
1212
private static final String usage =
13-
"\nPDF-UnStamper ver. 0.1.1 by hwding@GitHub\n" +
13+
"\nPDF-UnStamper ver. 0.1.2 by hwding@GitHub\n" +
1414
"\nUsage: " +
1515
"\n [OPTION] -i [INPUT PDF] -k [KEYWORDS...] (-o [OUTPUT PDF])" +
1616
"\n [OPTION] -I [INPUT DIR] -k [KEYWORDS...] (-O [OUTPUT DIR])\n" +

0 commit comments

Comments
 (0)