|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2017 iText Group NV |
| 4 | +Authors: iText Software. |
| 5 | +
|
| 6 | +This program is free software; you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU Affero General Public License version 3 |
| 8 | +as published by the Free Software Foundation with the addition of the |
| 9 | +following permission added to Section 15 as permitted in Section 7(a): |
| 10 | +FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | +ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | +OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | +This program is distributed in the hope that it will be useful, but |
| 15 | +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | +or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | +See the GNU Affero General Public License for more details. |
| 18 | +You should have received a copy of the GNU Affero General Public License |
| 19 | +along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | +Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | +http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | +The interactive user interfaces in modified source and object code versions |
| 25 | +of this program must display Appropriate Legal Notices, as required under |
| 26 | +Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | +In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | +a covered work must retain the producer line in every PDF that is created |
| 30 | +or manipulated using iText. |
| 31 | +
|
| 32 | +You can be released from the requirements of the license by purchasing |
| 33 | +a commercial license. Buying such a license is mandatory as soon as you |
| 34 | +develop commercial activities involving the iText software without |
| 35 | +disclosing the source code of your own applications. |
| 36 | +These activities include: offering paid services to customers as an ASP, |
| 37 | +serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | +source product. |
| 39 | +
|
| 40 | +For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | +*/ |
| 43 | +/* |
| 44 | +* To change this license header, choose License Headers in Project Properties. |
| 45 | +* To change this template file, choose Tools | Templates |
| 46 | +* and open the template in the editor. |
| 47 | +*/ |
| 48 | +using System; |
| 49 | +using System.Collections.Generic; |
| 50 | +using iText.Kernel.Colors; |
| 51 | +using iText.Kernel.Geom; |
| 52 | +using iText.Kernel.Pdf; |
| 53 | +using iText.Kernel.Pdf.Canvas.Parser.Data; |
| 54 | +using iText.Kernel.Pdf.Canvas.Parser.Listener; |
| 55 | +using iText.Kernel.Utils; |
| 56 | +using iText.PdfCleanup.Autosweep; |
| 57 | +using iText.Test; |
| 58 | + |
| 59 | +namespace iText.PdfCleanup { |
| 60 | + /// <author>Joris Schellekens</author> |
| 61 | + public class BigDocumentAutoCleanUpTest { |
| 62 | + private static readonly String inputPath = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 63 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfcleanup/BigDocumentAutoCleanUpTest/"; |
| 64 | + |
| 65 | + private static readonly String outputPath = NUnit.Framework.TestContext.CurrentContext.TestDirectory + "/test/itext/pdfcleanup/BigDocumentAutoCleanUpTest/"; |
| 66 | + |
| 67 | + [NUnit.Framework.OneTimeSetUp] |
| 68 | + public static void Before() { |
| 69 | + ITextTest.CreateOrClearDestinationFolder(outputPath); |
| 70 | + } |
| 71 | + |
| 72 | + /// <exception cref="System.IO.IOException"/> |
| 73 | + /// <exception cref="System.Exception"/> |
| 74 | + [NUnit.Framework.Test] |
| 75 | + public virtual void RedactLipsum() { |
| 76 | + String input = inputPath + "Lipsum.pdf"; |
| 77 | + String output = outputPath + "redactLipsum.pdf"; |
| 78 | + String cmp = inputPath + "cmp_redactLipsum.pdf"; |
| 79 | + CompositeCleanupStrategy strategy = new CompositeCleanupStrategy(); |
| 80 | + strategy.Add(new RegexBasedCleanupStrategy("(D|d)olor").SetRedactionColor(Color.GREEN)); |
| 81 | + PdfWriter writer = new PdfWriter(output); |
| 82 | + writer.SetCompressionLevel(0); |
| 83 | + PdfDocument pdf = new PdfDocument(new PdfReader(input), writer); |
| 84 | + // sweep |
| 85 | + PdfAutoSweep autoSweep = new PdfAutoSweep(strategy); |
| 86 | + autoSweep.CleanUp(pdf); |
| 87 | + pdf.Close(); |
| 88 | + // compare |
| 89 | + CompareByContent(cmp, output, outputPath, "diff_redactLipsum_"); |
| 90 | + } |
| 91 | + |
| 92 | + /// <exception cref="System.IO.IOException"/> |
| 93 | + /// <exception cref="System.Exception"/> |
| 94 | + [NUnit.Framework.Test] |
| 95 | + public virtual void RedactTonySoprano() { |
| 96 | + String input = inputPath + "TheSopranos.pdf"; |
| 97 | + String output = outputPath + "redactTonySoprano.pdf"; |
| 98 | + String cmp = inputPath + "cmp_redactTonySoprano.pdf"; |
| 99 | + CompositeCleanupStrategy strategy = new CompositeCleanupStrategy(); |
| 100 | + strategy.Add(new RegexBasedCleanupStrategy("Tony( |_)Soprano")); |
| 101 | + strategy.Add(new RegexBasedCleanupStrategy("Soprano")); |
| 102 | + strategy.Add(new RegexBasedCleanupStrategy("Sopranos")); |
| 103 | + PdfWriter writer = new PdfWriter(output); |
| 104 | + writer.SetCompressionLevel(0); |
| 105 | + PdfDocument pdf = new PdfDocument(new PdfReader(input), writer); |
| 106 | + // sweep |
| 107 | + PdfAutoSweep autoSweep = new PdfAutoSweep(strategy); |
| 108 | + autoSweep.CleanUp(pdf); |
| 109 | + pdf.Close(); |
| 110 | + // compare |
| 111 | + CompareByContent(cmp, output, outputPath, "diff_redactTonySoprano_"); |
| 112 | + } |
| 113 | + |
| 114 | + /// <exception cref="System.IO.IOException"/> |
| 115 | + /// <exception cref="System.Exception"/> |
| 116 | + [NUnit.Framework.Test] |
| 117 | + public virtual void RedactIPhoneUserManualMatchColor() { |
| 118 | + String input = inputPath + "iphone_user_guide_untagged.pdf"; |
| 119 | + String output = outputPath + "redactIPhoneUserManualMatchColor.pdf"; |
| 120 | + String cmp = inputPath + "cmp_redactIPhoneUserManualMatchColor.pdf"; |
| 121 | + CompositeCleanupStrategy strategy = new CompositeCleanupStrategy(); |
| 122 | + strategy.Add(new CustomLocationExtractionStrategy("(iphone)|(iPhone)")); |
| 123 | + PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(output)); |
| 124 | + // sweep |
| 125 | + PdfAutoSweep autoSweep = new PdfAutoSweep(strategy); |
| 126 | + autoSweep.CleanUp(pdf); |
| 127 | + pdf.Close(); |
| 128 | + // compare |
| 129 | + CompareByContent(cmp, output, outputPath, "diff_redactIPhoneUserManualMatchColor_"); |
| 130 | + } |
| 131 | + |
| 132 | + /// <exception cref="System.IO.IOException"/> |
| 133 | + /// <exception cref="System.Exception"/> |
| 134 | + [NUnit.Framework.Test] |
| 135 | + public virtual void RedactIPhoneUserManual() { |
| 136 | + String input = inputPath + "iphone_user_guide_untagged.pdf"; |
| 137 | + String output = outputPath + "redactIPhoneUserManual.pdf"; |
| 138 | + String cmp = inputPath + "cmp_redactIPhoneUserManual.pdf"; |
| 139 | + CompositeCleanupStrategy strategy = new CompositeCleanupStrategy(); |
| 140 | + strategy.Add(new RegexBasedCleanupStrategy("(iphone)|(iPhone)")); |
| 141 | + PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(output)); |
| 142 | + // sweep |
| 143 | + PdfAutoSweep autoSweep = new PdfAutoSweep(strategy); |
| 144 | + autoSweep.CleanUp(pdf); |
| 145 | + pdf.Close(); |
| 146 | + // compare |
| 147 | + CompareByContent(cmp, output, outputPath, "diff_redactIPhoneUserManual_"); |
| 148 | + } |
| 149 | + |
| 150 | + /// <exception cref="System.IO.IOException"/> |
| 151 | + /// <exception cref="System.Exception"/> |
| 152 | + private void CompareByContent(String cmp, String output, String targetDir, String diffPrefix) { |
| 153 | + CompareTool cmpTool = new CompareTool(); |
| 154 | + String errorMessage = cmpTool.CompareVisually(output, cmp, targetDir, diffPrefix + "_"); |
| 155 | + if (errorMessage != null) { |
| 156 | + NUnit.Framework.Assert.Fail(errorMessage); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + internal class CCharacterRenderInfo : CharacterRenderInfo { |
| 162 | + private Color strokeColor; |
| 163 | + |
| 164 | + private Color fillColor; |
| 165 | + |
| 166 | + public CCharacterRenderInfo(TextRenderInfo tri) |
| 167 | + : base(tri) { |
| 168 | + /* |
| 169 | + * color matching text redaction |
| 170 | + */ |
| 171 | + this.strokeColor = tri.GetStrokeColor(); |
| 172 | + this.fillColor = tri.GetFillColor(); |
| 173 | + } |
| 174 | + |
| 175 | + public virtual Color GetStrokeColor() { |
| 176 | + return strokeColor; |
| 177 | + } |
| 178 | + |
| 179 | + public virtual Color GetFillColor() { |
| 180 | + return fillColor; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + internal class CustomLocationExtractionStrategy : RegexBasedLocationExtractionStrategy, ICleanupStrategy { |
| 185 | + private String regex; |
| 186 | + |
| 187 | + private IDictionary<Rectangle, Color> colorByRectangle = new Dictionary<Rectangle, Color>(); |
| 188 | + |
| 189 | + public CustomLocationExtractionStrategy(String regex) |
| 190 | + : base(regex) { |
| 191 | + this.regex = regex; |
| 192 | + } |
| 193 | + |
| 194 | + protected override IList<CharacterRenderInfo> ToCRI(TextRenderInfo tri) { |
| 195 | + IList<CharacterRenderInfo> cris = new List<CharacterRenderInfo>(); |
| 196 | + foreach (TextRenderInfo subTri in tri.GetCharacterRenderInfos()) { |
| 197 | + cris.Add(new CCharacterRenderInfo(subTri)); |
| 198 | + } |
| 199 | + return cris; |
| 200 | + } |
| 201 | + |
| 202 | + protected override IList<Rectangle> ToRectangles(IList<CharacterRenderInfo> cris) { |
| 203 | + Color col = ((CCharacterRenderInfo)cris[0]).GetFillColor(); |
| 204 | + IList<Rectangle> rects = new List<Rectangle>(base.ToRectangles(cris)); |
| 205 | + foreach (Rectangle rect in rects) { |
| 206 | + colorByRectangle.Put(rect, col); |
| 207 | + } |
| 208 | + return rects; |
| 209 | + } |
| 210 | + |
| 211 | + public virtual Color GetRedactionColor(IPdfTextLocation rect) { |
| 212 | + return colorByRectangle.ContainsKey(rect.GetRectangle()) ? colorByRectangle.Get(rect.GetRectangle()) : Color |
| 213 | + .BLACK; |
| 214 | + } |
| 215 | + |
| 216 | + public virtual ICleanupStrategy Reset() { |
| 217 | + return new iText.PdfCleanup.CustomLocationExtractionStrategy(regex); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments