1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PDFRedactTest
- {
- internal class PDFRedactTest
- {
- static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Redact";
- static void Main(string[] args)
- {
- Console.WriteLine("Running redact test sample…\r\n");
- SDKLicenseHelper.LicenseVerify();
- CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
- string str = document.PageAtIndex(0).GetTextPage().GetSelectText(new Point(300, 240), new Point(400, 300), new Point(0, 0));
- Console.WriteLine("The text need to be redact is: {0}", str);
- if (!Directory.Exists(outputPath))
- {
- Directory.CreateDirectory(outputPath);
- }
- #region Redact
- if (Redact(document))
- {
- Console.WriteLine("Redact done.");
- }
- else
- {
- Console.WriteLine("Redact failed.");
- }
- #endregion
- Console.WriteLine("--------------------");
- Console.WriteLine("Done!");
- Console.WriteLine("--------------------");
- Console.ReadLine();
- }
- static private bool Redact(CPDFDocument document)
- {
- //PageIndex: 0
- CPDFPage page = document.PageAtIndex(0);
- //Initial redaction
- 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);
- //Fill color
- 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);
- string str = newDocument.PageAtIndex(0).GetTextPage().GetSelectText(new Point(60, 200), new Point(560, 250), new Point(0, 0));
- Console.WriteLine("Text in the redacted area is: {0}", str);
- return true;
- }
- }
- }
|