FindAndRedact.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text.RegularExpressions
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Pdf.Annotations
  10. Imports GrapeCity.Documents.Pdf.AcroForms
  11.  
  12. '' This sample shows how to use the text map to find specific content
  13. '' in a PDF And mark it for redaction.
  14. '' The PDF used in this sample was created by TimeSheet.
  15. Public Class FindAndRedact
  16. Public Function CreatePDF(ByVal stream As Stream) As Integer
  17. Dim doc = New GcPdfDocument()
  18. Using fs As New FileStream(Path.Combine("Resources", "PDFs", "TimeSheet.pdf"), FileMode.Open, FileAccess.Read)
  19. doc.Load(fs)
  20.  
  21. '' Note: Acrobat does not allow applying redactions in a digitally signed
  22. '' document, so first we find and remove any existing signatures:
  23. RemoveSignatureFields(doc.AcroForm.Fields)
  24.  
  25. '' Loop through pages, removing anything that looks Like a short date
  26. For Each page In doc.Pages
  27. Dim tmap = page.GetTextMap()
  28. For Each tline In tmap
  29. If (Regex.Match(tline.Text.Trim(), "\d+[/-]\w+[/-]\d").Success) Then
  30. Dim redact = New RedactAnnotation() With
  31. {
  32. .Rect = tline.GetCoords().ToRect(),
  33. .Color = Color.Red,
  34. .Page = page,
  35. .MarkBorderColor = Color.Red,
  36. .MarkFillColor = Color.Yellow
  37. }
  38. '' If we hadn't already set redact.Page = page, we could do this:
  39. '' page.Annotations.Add(redact)
  40. End If
  41. Next
  42. Next
  43. '' Done
  44. doc.Save(stream)
  45. Return doc.Pages.Count
  46. End Using
  47. End Function
  48.  
  49. '' This code Is from the RemoveSignatureFields sample:
  50. Sub RemoveSignatureFields(ByVal fields As FieldCollection)
  51. For i = fields.Count - 1 To 0 Step -1
  52. RemoveSignatureFields(fields(i).Children)
  53. If TypeOf fields(i) Is SignatureField Then
  54. fields.RemoveAt(i)
  55. End If
  56. Next
  57. End Sub
  58. End Class
  59.