Skip to content

Commit bb46ae5

Browse files
committed
Calculate overlapping according to the overlap ratio glyph by glyph
DEVSIX-8773
1 parent 669505b commit bb46ae5

16 files changed

+42
-4
lines changed

src/main/java/com/itextpdf/pdfcleanup/PdfCleanUpFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,24 @@ private boolean checkIfIntersectionOccurs(Paths paths, Point[] rect1, boolean is
226226
FilterResult<PdfArray> filterText(TextRenderInfo text) {
227227
PdfTextArray textArray = new PdfTextArray();
228228

229-
if (isTextNotToBeCleaned(text)) {
229+
// Overlap ratio should not be taken into account when we check the whole text not to be cleaned up
230+
if (properties.getOverlapRatio() == null && isTextNotToBeCleaned(text)) {
230231
return new FilterResult<>(false, new PdfArray(text.getPdfString()));
231232
}
232233

234+
boolean isModified = false;
233235
for (TextRenderInfo ri : text.getCharacterRenderInfos()) {
234236
if (isTextNotToBeCleaned(ri)) {
235237
textArray.add(ri.getPdfString());
236238
} else {
239+
isModified = true;
237240
textArray.add(new PdfNumber(FontProgram.convertGlyphSpaceToTextSpace(-ri.getUnscaledWidth()) /
238241
(text.getFontSize() * text.getHorizontalScaling() / FontProgram.HORIZONTAL_SCALING_FACTOR)
239242
));
240243
}
241244
}
242245

243-
return new FilterResult<PdfArray>(true, textArray);
246+
return new FilterResult<PdfArray>(isModified, textArray);
244247
}
245248

246249
/**

src/test/java/com/itextpdf/pdfcleanup/OverlapRatioTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.pdfcleanup;
2424

25+
import com.itextpdf.io.util.DecimalFormatUtil;
2526
import com.itextpdf.kernel.colors.ColorConstants;
2627
import com.itextpdf.kernel.pdf.PdfDocument;
2728
import com.itextpdf.kernel.pdf.PdfReader;
2829
import com.itextpdf.kernel.pdf.PdfWriter;
2930
import com.itextpdf.kernel.geom.Rectangle;
31+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
3032
import com.itextpdf.kernel.utils.CompareTool;
3133
import com.itextpdf.test.ExtendedITextTest;
34+
3235
import org.junit.jupiter.api.Assertions;
3336
import org.junit.jupiter.api.Test;
3437
import org.junit.jupiter.api.BeforeAll;
@@ -127,11 +130,10 @@ public void extractionWithSettingOverlapRatio1() throws IOException, Interrupted
127130

128131
PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(targetFile));
129132

130-
131133
CleanUpProperties properties = new CleanUpProperties();
132134
properties.setOverlapRatio(1d);
133135

134-
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>(); // convertCleanupLocations();
136+
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
135137
cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(20, 690, 263.75f, 40), ColorConstants.YELLOW));
136138
PdfCleaner.cleanUp(pdfDoc, cleanUpLocations, properties);
137139
pdfDoc.close();
@@ -141,6 +143,39 @@ public void extractionWithSettingOverlapRatio1() throws IOException, Interrupted
141143
Assertions.assertNull(errorMessage);
142144
}
143145

146+
@Test
147+
public void differentTextRenderInfo() throws IOException, InterruptedException {
148+
final String inputFile = inputPath + "differentTextRenderInfo.pdf";
149+
final Double[] ratioArray = new Double[] {0d, 0.001, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1d};
150+
final Rectangle r = new Rectangle(110, 700, 400, 130);
151+
152+
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
153+
PdfCleanUpLocation location = new PdfCleanUpLocation(1, r);
154+
cleanUpLocations.add(location);
155+
CleanUpProperties cleanUpProperties = new CleanUpProperties();
156+
157+
for (Double ratio : ratioArray) {
158+
final String targetFile = outputPath + "differentTextRenderInfo_" +
159+
DecimalFormatUtil.formatNumber(ratio.doubleValue(), "#.000#") + "_redact.pdf";
160+
final String cmpFile = inputPath + "cmp_differentTextRenderInfo_" +
161+
DecimalFormatUtil.formatNumber(ratio.doubleValue(), "#.000#") + "_redact.pdf";
162+
163+
try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(targetFile))) {
164+
if (ratio == 0d) {
165+
cleanUpProperties.setOverlapRatio(null);
166+
} else {
167+
cleanUpProperties.setOverlapRatio(ratio);
168+
}
169+
PdfCleaner.cleanUp(pdfDoc, cleanUpLocations, cleanUpProperties);
170+
171+
// Draw a rectangle to visualize the cleanup
172+
PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc.getPage(1));
173+
pdfCanvas.setStrokeColor(ColorConstants.RED).rectangle(r).stroke();
174+
}
175+
176+
Assertions.assertNull(new CompareTool().compareByContent(targetFile, cmpFile, outputPath, "diff_"));
177+
}
178+
}
144179

145180
private static List<PdfCleanUpLocation> convertCleanupLocations() {
146181
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();

0 commit comments

Comments
 (0)