-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- provide a more elegant and efficient way to load fonts in each page
- bump version & head comment date
- Loading branch information
Showing
4 changed files
with
30 additions
and
44 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,14 +1,13 @@ | ||
/* | ||
AUTH | hwding | ||
DATE | Aug 27 2017 | ||
DATE | Sep 04 2017 | ||
DESC | text stamp remover for PDF files | ||
MAIL | [email protected] | ||
GITH | github.com/hwding | ||
*/ | ||
package com.amastigote.unstamper.core; | ||
|
||
import com.amastigote.unstamper.log.GeneralLogger; | ||
import org.apache.pdfbox.cos.COSName; | ||
import org.apache.pdfbox.cos.COSString; | ||
import org.apache.pdfbox.pdfparser.PDFStreamParser; | ||
import org.apache.pdfbox.pdfwriter.ContentStreamWriter; | ||
|
@@ -19,11 +18,9 @@ | |
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; | ||
import java.util.stream.Collectors; | ||
|
||
public class Processor { | ||
public static void process(File file, String[] strings) { | ||
|
@@ -35,45 +32,34 @@ public static void process(File file, String[] strings) { | |
PDDocument pdDocument = PDDocument.load(file); | ||
pdDocument.getPages().forEach(pdPage -> { | ||
try { | ||
/* START: loading font resources for further parsing */ | ||
/* START: loading font resources from current page */ | ||
PDFStreamParser pdfStreamParser = new PDFStreamParser(pdPage); | ||
pdfStreamParser.parse(); | ||
|
||
List<Object> objects = | ||
Collections.synchronizedList(pdfStreamParser.getTokens()); | ||
List<Object> objects = pdfStreamParser.getTokens(); | ||
Set<PDFont> pdFonts = new HashSet<>(); | ||
|
||
List<Object> cosNames = | ||
objects.parallelStream() | ||
.filter(e -> e instanceof COSName) | ||
.collect(Collectors.toList()); | ||
|
||
Set<PDFont> 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) { | ||
} | ||
}); | ||
pdPage.getResources().getFontNames().forEach(e -> { | ||
/* Ignore Any Exception During Parallel Processing */ | ||
try { | ||
PDFont pdFont = pdPage.getResources().getFont(e); | ||
if (pdFont != null) | ||
pdFonts.add(pdFont); | ||
} catch (Exception ignored) { | ||
} | ||
}); | ||
/* END */ | ||
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) { | ||
} | ||
} | ||
} | ||
); | ||
|
||
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(); | ||
|
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,6 +1,6 @@ | ||
/* | ||
AUTH | hwding | ||
DATE | Aug 27 2017 | ||
DATE | Sep 04 2017 | ||
DESC | text stamp remover for PDF files | ||
MAIL | [email protected] | ||
GITH | github.com/hwding | ||
|
@@ -10,7 +10,7 @@ | |
public class GeneralLogger { | ||
public static class Help { | ||
private static final String usage = | ||
"\nPDF-UnStamper ver. 0.1.1 by hwding@GitHub\n" + | ||
"\nPDF-UnStamper ver. 0.1.2 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" + | ||
|