Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 9, 2024 13:55
Redact Micrsoft Word, Excel Spreadsheets, PowerPoint Presentations and PDF Documents by finding and replacing the desired content via your .NET applications.
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "test.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchTerm);
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
doc.Pages.Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection){
Page page = textFragment.Page;
Aspose.Pdf.Rectangle annotationRectangle = textFragment.Rectangle;
Aspose.Pdf.Annotations.RedactionAnnotation annot = new Aspose.Pdf.Annotations.RedactionAnnotation(page, annotationRectangle);
annot.FillColor = Aspose.Pdf.Color.Black;
doc.Pages[textFragment.Page.Number].Annotations.Add(annot, true);
annot.Redact();
}
doc.Save(dataDir + "output.pdf");
Workbook wb = new Workbook("e:\test2\Input.xlsx");
wb.Replace("\bKIM\b", "^^^^^^^^", new ReplaceOptions() { RegexKey = true });
wb.Save("e:\test2\output.xlsx");
Workbook wb = new Workbook("e:\test2\Input.xlsx");
Worksheet sheet = wb.Worksheets[0];
FindOptions opts = new FindOptions();
opts.LookInType = LookInType.Values;
opts.LookAtType = LookAtType.Contains;
opts.RegexKey = true;
Aspose.Cells.Cell cell = null;
do
{
cell = sheet.Cells.Find("\bKIM\b", cell, opts);
if (cell != null)
{
string celltext = cell.Value.ToString();
celltext = celltext.Replace("KIM", "^^^^^^^^^^");
cell.PutValue(celltext);
}
}
while (cell != null);
wb.Save("e:\test2\out1.xlsx");
using (Presentation pres = new Presentation("pres.pptx"))
{
const string toRedact = "bKIM";
string stub = new string(' ', toRedact.Length);
foreach (ISlide slide in pres.Slides){
ITextFrame[] textFrames = SlideUtil.GetAllTextBoxes(slide);
foreach (ITextFrame textFrame in textFrames){
textFrame.Text = textFrame.Text.Replace(toRedact, stub);
textFrame.HighlightText(stub, Color.Black);
}
}
pres.Save("pres-edited.pptx", SaveFormat.Pptx);
}
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("sad mad bad");
Assert.AreEqual("sad mad bad", doc.GetText().Trim());
doc.Range.Replace(new Regex("[s|m]ad"), "bad");
doc.Save("Range.ReplaceWithRegex.docx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment