PDFRedactTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using System;
  6. using System.IO;
  7. using System.Windows;
  8. namespace PDFRedactTest
  9. {
  10. internal class PDFRedactTest
  11. {
  12. static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Redact";
  13. static void Main(string[] args)
  14. {
  15. #region Perparation work
  16. Console.WriteLine("Running redact test sample…\r\n");
  17. SDKLicenseHelper.LicenseVerify();
  18. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  19. string str = document.PageAtIndex(0).GetTextPage().GetSelectText(new Point(300, 240), new Point(400, 300), new Point(0, 0));
  20. Console.WriteLine("The text need to be redact is: {0}", str);
  21. if (!Directory.Exists(outputPath))
  22. {
  23. Directory.CreateDirectory(outputPath);
  24. }
  25. #endregion
  26. #region Redact
  27. if (Redact(document))
  28. {
  29. Console.WriteLine("Redact done.");
  30. }
  31. else
  32. {
  33. Console.WriteLine("Redact failed.");
  34. }
  35. Console.WriteLine("--------------------");
  36. #endregion
  37. Console.WriteLine("Done!");
  38. Console.WriteLine("--------------------");
  39. Console.ReadLine();
  40. }
  41. /// <summary>
  42. /// Redact an area in PDF
  43. /// </summary>
  44. /// <param name="document">Regular document with some text</param>
  45. /// <returns></returns>
  46. static private bool Redact(CPDFDocument document)
  47. {
  48. CPDFPage page = document.PageAtIndex(0);
  49. CPDFRedactAnnotation redact = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
  50. //Set radact rect: cover the title
  51. redact.SetRect(new CRect(300, 240, 400, 300));
  52. //Set overlay text: REDACTED
  53. redact.SetOverlayText("REDACTED");
  54. //Properties of cover text
  55. CTextAttribute textAttribute = new CTextAttribute();
  56. textAttribute.FontName = "Helvetica";
  57. textAttribute.FontSize = 12;
  58. byte[] fontColor = { 255, 0, 0 };
  59. textAttribute.FontColor = fontColor;
  60. redact.SetTextDa(textAttribute);
  61. redact.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  62. //Properties of cover square
  63. byte[] fillColor = { 255, 0, 0 };
  64. redact.SetFillColor(fillColor);
  65. byte[] outlineColor = { 0, 255, 0 };
  66. redact.SetOutlineColor(outlineColor);
  67. redact.UpdateAp();
  68. document.ApplyRedaction();
  69. // Save to pointed path so you can observe the effect.
  70. string path = outputPath + "\\RedactTest.pdf";
  71. if (!document.WriteToFilePath(path))
  72. {
  73. return false;
  74. }
  75. Console.WriteLine("Browse the changed file in " + path);
  76. CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
  77. //Validation: try to get the text of the covered area
  78. string str = newDocument.PageAtIndex(0).GetTextPage().GetSelectText(new Point(60, 200), new Point(560, 250), new Point(0, 0));
  79. Console.WriteLine("Text in the redacted area is: {0}", str);
  80. return true;
  81. }
  82. }
  83. }