RED-7686: Specific hidden text in specific file is not removed
* extend invisible Element removal to Form XObjects BBoxes
This commit is contained in:
parent
612bb5a63a
commit
b5f8c37bcd
@ -8,6 +8,7 @@ import java.awt.geom.Rectangle2D;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -46,6 +47,7 @@ public class InvisibleElementRemovalService {
|
|||||||
* handled cases:
|
* handled cases:
|
||||||
* -Text which is transparent or is set to not render
|
* -Text which is transparent or is set to not render
|
||||||
* -Elements outside of clipping path
|
* -Elements outside of clipping path
|
||||||
|
* -Elements outside of Form XObjects
|
||||||
* -Elements that have been painted over by visible and filled Paths
|
* -Elements that have been painted over by visible and filled Paths
|
||||||
* -Elements with the same color as background
|
* -Elements with the same color as background
|
||||||
* unhandled cases:
|
* unhandled cases:
|
||||||
@ -235,6 +237,8 @@ public class InvisibleElementRemovalService {
|
|||||||
// writer needs to be newly initialized when entering a new content stream
|
// writer needs to be newly initialized when entering a new content stream
|
||||||
// see ElementEditTest in PDFTron (https://www.pdftron.com/documentation/samples/android/java/ElementEditTest)
|
// see ElementEditTest in PDFTron (https://www.pdftron.com/documentation/samples/android/java/ElementEditTest)
|
||||||
ElementWriter formWriter = new ElementWriter();
|
ElementWriter formWriter = new ElementWriter();
|
||||||
|
context.clippingPathStack().enterNewGState();
|
||||||
|
context.clippingPathStack().intersectClippingPath(new GeneralPath(Converter.toRectangle2D(formElement.getBBox())));
|
||||||
context.reader().formBegin();
|
context.reader().formBegin();
|
||||||
formWriter.begin(formObj);
|
formWriter.begin(formObj);
|
||||||
|
|
||||||
@ -245,6 +249,7 @@ public class InvisibleElementRemovalService {
|
|||||||
formWriter.end();
|
formWriter.end();
|
||||||
formWriter.destroy();
|
formWriter.destroy();
|
||||||
context.reader().end();
|
context.reader().end();
|
||||||
|
context.clippingPathStack().leaveGState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -53,6 +53,34 @@ class InvisibleElementRemovalServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SneakyThrows
|
||||||
|
void removeInvisibleTextClippedByFormObjects() {
|
||||||
|
|
||||||
|
String fileName = "files/invisibleTextInNestedFormObjects.pdf";
|
||||||
|
String resultFileName = OsUtils.createTmpFileName(fileName, "INVISIBLE_REMOVAL");
|
||||||
|
String deltaResultFileName = OsUtils.createTmpFileName(fileName, "INVISIBLE_REMOVAL_DELTA");
|
||||||
|
try (var in = this.getClass().getClassLoader().getResourceAsStream(fileName); var out = new FileOutputStream(resultFileName)) {
|
||||||
|
invisibleElementRemovalService.removeInvisibleElements(in, out, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (var in = this.getClass().getClassLoader().getResourceAsStream(fileName); var out = new FileOutputStream(deltaResultFileName)) {
|
||||||
|
invisibleElementRemovalService.removeInvisibleElements(in, out, true);
|
||||||
|
}
|
||||||
|
try (var in = new FileInputStream(resultFileName)) {
|
||||||
|
String text = extractAllTextFromDocument(in);
|
||||||
|
assertThat(text).isBlank();
|
||||||
|
}
|
||||||
|
try (var in = new FileInputStream(deltaResultFileName)) {
|
||||||
|
String[] text = extractAllTextFromDocument(in).split("\n");
|
||||||
|
assertThat(text).contains(":Bold S-enantiomer form if two codes are supplied",
|
||||||
|
"Red : Only observed in laboratory soil studies",
|
||||||
|
"Green : Observed in both laboratory soil studies and lysimeter leachate",
|
||||||
|
"Blue : Only observed in lysimeter leachate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
void removeInvisibleElementsWithColoredBackground() {
|
void removeInvisibleElementsWithColoredBackground() {
|
||||||
|
|||||||
BIN
src/test/resources/files/invisibleTextInNestedFormObjects.pdf
Normal file
BIN
src/test/resources/files/invisibleTextInNestedFormObjects.pdf
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user