PDFRedactTest.vb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. Imports ComPDFKit.Import
  2. Imports ComPDFKit.PDFAnnotation
  3. Imports ComPDFKit.PDFDocument
  4. Imports ComPDFKit.PDFPage
  5. Imports System.IO
  6. Module PDFRedactTest
  7. Private outputPath As String = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) & "\Output\VB"
  8. Sub Main()
  9. ' Perparation work
  10. Console.WriteLine("Running redact test sample…" & vbCrLf)
  11. SDKLicenseHelper.LicenseVerify()
  12. Dim document As CPDFDocument = CPDFDocument.InitWithFilePath("CommonFivePage.pdf")
  13. Dim str As String = document.PageAtIndex(0).GetTextPage().GetSelectText(New CPoint(300, 240), New CPoint(400, 300), New CPoint(0, 0))
  14. Console.WriteLine("The text need to be redact is: {0}", str)
  15. If Not Directory.Exists(outputPath) Then
  16. Directory.CreateDirectory(outputPath)
  17. End If
  18. ' Redact
  19. If Redact(document) Then
  20. Console.WriteLine("Redact done.")
  21. Else
  22. Console.WriteLine("Redact failed.")
  23. End If
  24. Console.WriteLine("--------------------")
  25. Console.WriteLine("Done!")
  26. Console.WriteLine("--------------------")
  27. Console.ReadLine()
  28. End Sub
  29. ' Redact an area in PDF
  30. Private Function Redact(document As CPDFDocument) As Boolean
  31. Dim page As CPDFPage = document.PageAtIndex(0)
  32. Dim redactAnnot As CPDFRedactAnnotation = TryCast(page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT), CPDFRedactAnnotation)
  33. ' Set redact rect: cover the title
  34. redactAnnot.SetRect(New CRect(300, 300, 400, 240))
  35. ' Set overlay text: REDACTED
  36. redactAnnot.SetOverlayText("REDACTED")
  37. ' Properties of cover text
  38. Dim textAttribute As New CTextAttribute()
  39. textAttribute.FontName = "Helvetica"
  40. textAttribute.FontSize = 12
  41. Dim fontColor As Byte() = {255, 0, 0}
  42. textAttribute.FontColor = fontColor
  43. redactAnnot.SetTextDa(textAttribute)
  44. redactAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT)
  45. ' Properties of cover square
  46. Dim fillColor As Byte() = {255, 0, 0}
  47. redactAnnot.SetFillColor(fillColor)
  48. Dim outlineColor As Byte() = {0, 255, 0}
  49. redactAnnot.SetOutlineColor(outlineColor)
  50. redactAnnot.UpdateAp()
  51. document.ApplyRedaction()
  52. ' Save to pointed path so you can observe the effect.
  53. Dim path As String = outputPath & "\RedactTest.pdf"
  54. If Not document.WriteToFilePath(path) Then
  55. Return False
  56. End If
  57. Console.WriteLine("Browse the changed file in " & path)
  58. Dim newDocument As CPDFDocument = CPDFDocument.InitWithFilePath(path)
  59. ' Validation: try to get the text of the covered area
  60. Dim str As String = newDocument.PageAtIndex(0).GetTextPage().GetSelectText(New CPoint(60, 200), New CPoint(560, 250), New CPoint(0, 0))
  61. Console.WriteLine("Text in the redacted area is: {0}", str)
  62. Return True
  63. End Function
  64. End Module