PDFRedactTest.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\\CS";
  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. // Note: replace Point to CPoint
  20. string str = document.PageAtIndex(0).GetTextPage().GetSelectText(new CPoint(300, 240), new CPoint(400, 300), new CPoint(0, 0));
  21. Console.WriteLine("The text need to be redact is: {0}", str);
  22. if (!Directory.Exists(outputPath))
  23. {
  24. Directory.CreateDirectory(outputPath);
  25. }
  26. #endregion
  27. #region Redact
  28. if (Redact(document))
  29. {
  30. Console.WriteLine("Redact done.");
  31. }
  32. else
  33. {
  34. Console.WriteLine("Redact failed.");
  35. }
  36. Console.WriteLine("--------------------");
  37. #endregion
  38. Console.WriteLine("Done!");
  39. Console.WriteLine("--------------------");
  40. Console.ReadLine();
  41. }
  42. /// <summary>
  43. /// Redact an area in PDF
  44. /// </summary>
  45. /// <param name="document">Regular document with some text</param>
  46. /// <returns></returns>
  47. static private bool Redact(CPDFDocument document)
  48. {
  49. CPDFPage page = document.PageAtIndex(0);
  50. CPDFRedactAnnotation redact = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
  51. //Set radact rect: cover the title
  52. redact.SetRect(new CRect(300, 240, 400, 300));
  53. //Set overlay text: REDACTED
  54. redact.SetOverlayText("REDACTED");
  55. //Properties of cover text
  56. CTextAttribute textAttribute = new CTextAttribute();
  57. textAttribute.FontName = "Helvetica";
  58. textAttribute.FontSize = 12;
  59. byte[] fontColor = { 255, 0, 0 };
  60. textAttribute.FontColor = fontColor;
  61. redact.SetTextDa(textAttribute);
  62. redact.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  63. //Properties of cover square
  64. byte[] fillColor = { 255, 0, 0 };
  65. redact.SetFillColor(fillColor);
  66. byte[] outlineColor = { 0, 255, 0 };
  67. redact.SetOutlineColor(outlineColor);
  68. redact.UpdateAp();
  69. document.ApplyRedaction();
  70. // Save to pointed path so you can observe the effect.
  71. string path = outputPath + "\\RedactTest.pdf";
  72. if (!document.WriteToFilePath(path))
  73. {
  74. return false;
  75. }
  76. Console.WriteLine("Browse the changed file in " + path);
  77. CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
  78. //Validation: try to get the text of the covered area
  79. // Note: replace Point to CPoint
  80. string str = newDocument.PageAtIndex(0).GetTextPage().GetSelectText(new CPoint(60, 200), new CPoint(560, 250), new CPoint(0, 0));
  81. Console.WriteLine("Text in the redacted area is: {0}", str);
  82. return true;
  83. }
  84. }
  85. }