1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using System;
- using System.IO;
- using System.Windows;
- namespace PDFRedactTest
- {
- internal class PDFRedactTest
- {
- static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\CS";
- static void Main(string[] args)
- {
- #region Perparation work
- Console.WriteLine("Running redact test sample…\r\n");
- SDKLicenseHelper.LicenseVerify();
- CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
- // Note: replace Point to CPoint
- string str = document.PageAtIndex(0).GetTextPage().GetSelectText(new CPoint(300, 240), new CPoint(400, 300), new CPoint(0, 0));
- Console.WriteLine("The text need to be redact is: {0}", str);
- if (!Directory.Exists(outputPath))
- {
- Directory.CreateDirectory(outputPath);
- }
- #endregion
- #region Redact
- if (Redact(document))
- {
- Console.WriteLine("Redact done.");
- }
- else
- {
- Console.WriteLine("Redact failed.");
- }
- Console.WriteLine("--------------------");
- #endregion
- Console.WriteLine("Done!");
- Console.WriteLine("--------------------");
- Console.ReadLine();
- }
- /// <summary>
- /// Redact an area in PDF
- /// </summary>
- /// <param name="document">Regular document with some text</param>
- /// <returns></returns>
- static private bool Redact(CPDFDocument document)
- {
- CPDFPage page = document.PageAtIndex(0);
- CPDFRedactAnnotation redact = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
- //Set radact rect: cover the title
- redact.SetRect(new CRect(300, 240, 400, 300));
- //Set overlay text: REDACTED
- redact.SetOverlayText("REDACTED");
- //Properties of cover text
- CTextAttribute textAttribute = new CTextAttribute();
- textAttribute.FontName = "Helvetica";
- textAttribute.FontSize = 12;
- byte[] fontColor = { 255, 0, 0 };
- textAttribute.FontColor = fontColor;
- redact.SetTextDa(textAttribute);
- redact.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
- //Properties of cover square
- byte[] fillColor = { 255, 0, 0 };
- redact.SetFillColor(fillColor);
- byte[] outlineColor = { 0, 255, 0 };
- redact.SetOutlineColor(outlineColor);
- redact.UpdateAp();
- document.ApplyRedaction();
- // Save to pointed path so you can observe the effect.
- string path = outputPath + "\\RedactTest.pdf";
- if (!document.WriteToFilePath(path))
- {
- return false;
- }
- Console.WriteLine("Browse the changed file in " + path);
- CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);
- //Validation: try to get the text of the covered area
- // Note: replace Point to CPoint
- string str = newDocument.PageAtIndex(0).GetTextPage().GetSelectText(new CPoint(60, 200), new CPoint(560, 250), new CPoint(0, 0));
- Console.WriteLine("Text in the redacted area is: {0}", str);
- return true;
- }
- }
- }
|