|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using iText.Kernel.Colors; |
| 4 | +using iText.Kernel.Geom; |
| 5 | +using iText.Kernel.Pdf; |
| 6 | +using iText.Kernel.Utils; |
| 7 | +using iText.Test; |
| 8 | + |
| 9 | +namespace iText.PdfCleanup { |
| 10 | + [NUnit.Framework.Category("IntegrationTest")] |
| 11 | + public class OverlapRatioTest : ExtendedITextTest { |
| 12 | + private static readonly String inputPath = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 13 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfcleanup/OverlapRatioTest/"; |
| 14 | + |
| 15 | + private static readonly String outputPath = NUnit.Framework.TestContext.CurrentContext.TestDirectory + "/test/itext/pdfcleanup/OverlapRatioTest/"; |
| 16 | + |
| 17 | + [NUnit.Framework.OneTimeSetUp] |
| 18 | + public static void Before() { |
| 19 | + CreateOrClearDestinationFolder(outputPath); |
| 20 | + } |
| 21 | + |
| 22 | + private static readonly double[][] coordinates = new double[][] { |
| 23 | + //Areas with small line spacing |
| 24 | + new double[] { 1, 149, 700, 63.75, 10.75 }, new double[] { 1, 149, 640, 63.75, 10.75 }, new double[] { 1, |
| 25 | + 149, 520, 163.75, 50.75 }, |
| 26 | + //Areas with big line spacing |
| 27 | + new double[] { 1, 149, 374, 63.75, 10.75 }, new double[] { 1, 149, 310, 63.75, 10.75 }, new double[] { 1, |
| 28 | + 149, 120, 163.75, 50.75 } }; |
| 29 | + |
| 30 | + [NUnit.Framework.Test] |
| 31 | + public virtual void ExtractionWithoutSettingOverlapRatio() { |
| 32 | + String inputFile = inputPath + "redact_aspect_ratio_simple.pdf"; |
| 33 | + String targetFile = outputPath + "wo_redact_aspect_ratio_simple_redact.pdf"; |
| 34 | + String cmpFile = inputPath + "cmp_wo_redact_aspect_ratio_simple.pdf"; |
| 35 | + PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(targetFile)); |
| 36 | + CleanUpProperties properties = new CleanUpProperties(); |
| 37 | + PdfCleaner.CleanUp(pdfDoc, ConvertCleanupLocations(), properties); |
| 38 | + pdfDoc.Close(); |
| 39 | + CompareTool cmpTool = new CompareTool(); |
| 40 | + String errorMessage = cmpTool.CompareByContent(targetFile, cmpFile, outputPath, "diff_"); |
| 41 | + NUnit.Framework.Assert.IsNull(errorMessage); |
| 42 | + } |
| 43 | + |
| 44 | + [NUnit.Framework.Test] |
| 45 | + public virtual void ExtractionWithSettingOverlapRatio() { |
| 46 | + String inputFile = inputPath + "redact_aspect_ratio_simple.pdf"; |
| 47 | + String targetFile = outputPath + "redact_aspect_ratio_simple_redact.pdf"; |
| 48 | + String cmpFile = inputPath + "cmp_redact_aspect_ratio_simple.pdf"; |
| 49 | + PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(targetFile)); |
| 50 | + CleanUpProperties properties = new CleanUpProperties(); |
| 51 | + properties.SetOverlapRatio(0.35); |
| 52 | + PdfCleaner.CleanUp(pdfDoc, ConvertCleanupLocations(), properties); |
| 53 | + pdfDoc.Close(); |
| 54 | + CompareTool cmpTool = new CompareTool(); |
| 55 | + String errorMessage = cmpTool.CompareByContent(targetFile, cmpFile, outputPath, "diff_"); |
| 56 | + NUnit.Framework.Assert.IsNull(errorMessage); |
| 57 | + } |
| 58 | + |
| 59 | + [NUnit.Framework.Test] |
| 60 | + public virtual void ExtractionWithSettingOverlapRatioCloseTo0() { |
| 61 | + //In this test we expect it to behave as normal that everything that gets touched by the redaction \ |
| 62 | + //area should be redacted. |
| 63 | + String inputFile = inputPath + "redact_aspect_ratio_simple.pdf"; |
| 64 | + String targetFile = outputPath + "redact_aspect_ratio_0_simple_redact.pdf"; |
| 65 | + String cmpFile = inputPath + "cmp_redact_aspect_ratio_0_simple.pdf"; |
| 66 | + PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(targetFile)); |
| 67 | + CleanUpProperties properties = new CleanUpProperties(); |
| 68 | + properties.SetOverlapRatio(0.0001); |
| 69 | + PdfCleaner.CleanUp(pdfDoc, ConvertCleanupLocations(), properties); |
| 70 | + pdfDoc.Close(); |
| 71 | + CompareTool cmpTool = new CompareTool(); |
| 72 | + String errorMessage = cmpTool.CompareByContent(targetFile, cmpFile, outputPath, "diff_"); |
| 73 | + NUnit.Framework.Assert.IsNull(errorMessage); |
| 74 | + } |
| 75 | + |
| 76 | + [NUnit.Framework.Test] |
| 77 | + public virtual void ExtractionWithSettingOverlapRatio1() { |
| 78 | + //In this sample we expect nothing to be redacted because of none of the items actually overlaps all of it. |
| 79 | + String inputFile = inputPath + "redact_aspect_ratio_simple.pdf"; |
| 80 | + String targetFile = outputPath + "redact_aspect_ratio_1_simple_redact.pdf"; |
| 81 | + String cmpFile = inputPath + "cmp_redact_aspect_ratio_1_simple.pdf"; |
| 82 | + PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputFile), new PdfWriter(targetFile)); |
| 83 | + CleanUpProperties properties = new CleanUpProperties(); |
| 84 | + properties.SetOverlapRatio(1d); |
| 85 | + IList<iText.PdfCleanup.PdfCleanUpLocation> cleanUpLocations = new List<iText.PdfCleanup.PdfCleanUpLocation |
| 86 | + >(); |
| 87 | + // convertCleanupLocations(); |
| 88 | + cleanUpLocations.Add(new iText.PdfCleanup.PdfCleanUpLocation(1, new Rectangle(20, 690, 263.75f, 40), ColorConstants |
| 89 | + .YELLOW)); |
| 90 | + PdfCleaner.CleanUp(pdfDoc, cleanUpLocations, properties); |
| 91 | + pdfDoc.Close(); |
| 92 | + CompareTool cmpTool = new CompareTool(); |
| 93 | + String errorMessage = cmpTool.CompareByContent(targetFile, cmpFile, outputPath, "diff_"); |
| 94 | + NUnit.Framework.Assert.IsNull(errorMessage); |
| 95 | + } |
| 96 | + |
| 97 | + private static IList<iText.PdfCleanup.PdfCleanUpLocation> ConvertCleanupLocations() { |
| 98 | + IList<iText.PdfCleanup.PdfCleanUpLocation> cleanUpLocations = new List<iText.PdfCleanup.PdfCleanUpLocation |
| 99 | + >(); |
| 100 | + foreach (double[] coord in coordinates) { |
| 101 | + int pageNumber = (int)coord[0]; |
| 102 | + double x = coord[1]; |
| 103 | + double y = coord[2]; |
| 104 | + double width = coord[3]; |
| 105 | + double height = coord[4]; |
| 106 | + iText.PdfCleanup.PdfCleanUpLocation location = new iText.PdfCleanup.PdfCleanUpLocation(pageNumber, new Rectangle |
| 107 | + ((float)x, (float)y, (float)width, (float)height), ColorConstants.BLACK); |
| 108 | + cleanUpLocations.Add(location); |
| 109 | + } |
| 110 | + return cleanUpLocations; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments