PDFRedactTest.cs 3.3 KB

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