public int CreatePDF(Stream stream) { var doc = new GcPdfDocument(); using (var fs = new FileStream(Path.Combine("Resources", "PDFs", "SlidePages.pdf"), FileMode.Open, FileAccess.Read)) { // Load the PDF containing redact annotations (areas marked for redaction): doc.Load(fs); var rc = new RectangleF(16, 16, 280, 300); var redact = new RedactAnnotation() { Rect = rc, Page = doc.Pages[0], OverlayText = "This content has been redacted.", OverlayFillColor = Color.PaleGoldenrod }; // Apply the redact: doc.Redact(redact); // doc.Pages[0].Graphics.DrawRectangle(rc, Color.Red); // Done: doc.Save(stream); return(doc.Pages.Count); } }
public int CreatePDF(Stream stream) { var doc = new GcPdfDocument(); using (var fs = new FileStream(Path.Combine("Resources", "PDFs", "TimeSheet.pdf"), FileMode.Open, FileAccess.Read)) { doc.Load(fs); // Note: Acrobat does not allow to apply redactions in a digitally signed // document, so first we find and remove any existing signatures: removeSignatureFields(doc.AcroForm.Fields); // Loop through pages, removing anything that looks like a short date: foreach (var page in doc.Pages) { var tmap = page.GetTextMap(); foreach (ITextLine tline in tmap) { if (Regex.Match(tline.Text.Trim(), @"\d+[/-]\w+[/-]\d").Success) { var redact = new RedactAnnotation() { Rect = tline.GetCoords().ToRect(), MarkBorderColor = Color.Red, MarkFillColor = Color.Yellow, Page = page }; // If we hadn't already set redact.Page = page, we could do this: // page.Annotations.Add(redact); } } } // Done: doc.Save(stream); return(doc.Pages.Count); // This code is from the RemoveSignatureFields sample: void removeSignatureFields(FieldCollection fields) { for (int i = fields.Count - 1; i >= 0; --i) { removeSignatureFields(fields[i].Children); if (fields[i] is SignatureField) { fields.RemoveAt(i); } } } } }