PDFRedactTest.vb 3.0 KB

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