ByteScout PDF Suite - C# - PDF Data Remover Tool - ByteScout

ByteScout PDF Suite – C# – PDF Data Remover Tool

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Suite – C# – PDF Data Remover Tool

PDF data remover tool in C# using ByteScout PDF Suite

Learn to code PDF data remover tool in C#: How-To tutorial

This page explains the steps and algorithm of implementing PDF data remover tool and how to make it work in your application. ByteScout PDF Suite helps with PDF data remover tool in C#. ByteScout PDF Suite is the set that includes 6 SDK products to work with PDF from generating rich PDF reports to extracting data from PDF documents and converting them to HTML. This bundle includes PDF (Generator) SDK, PDF Renderer SDK, PDF Extractor SDK, PDF to HTML SDK, PDF Viewer SDK and PDF Generator SDK for Javascript.

C# code snippet like this for ByteScout PDF Suite works best when you need to quickly implement PDF data remover tool in your C# application. To use PDF data remover tool in your C# project or application just copy & paste the code and then run your app! Enjoy writing a code with ready-to-use sample C# codes to implement PDF data remover tool using ByteScout PDF Suite.

Trial version along with the source code samples for C# can be downloaded from our website

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Form1.Designer.cs
      
namespace Sample_UI_Application { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.pdfViewerControl1 = new Bytescout.PDFViewer.PDFViewerControl(); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.tsbOpen = new System.Windows.Forms.ToolStripButton(); this.btnProceed = new System.Windows.Forms.Button(); this.tbSearchExpression = new System.Windows.Forms.TextBox(); this.btnFind = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.cbRegex = new System.Windows.Forms.CheckBox(); this.label1 = new System.Windows.Forms.Label(); this.cbMaskRemovedText = new System.Windows.Forms.CheckBox(); this.cbMakeUnsearchable = new System.Windows.Forms.CheckBox(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.toolStrip1.SuspendLayout(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // pdfViewerControl1 // this.pdfViewerControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.pdfViewerControl1.BackColor = System.Drawing.SystemColors.ButtonShadow; this.pdfViewerControl1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pdfViewerControl1.CacheVisitedPages = true; this.pdfViewerControl1.Location = new System.Drawing.Point(275, 28); this.pdfViewerControl1.MouseMode = Bytescout.PDFViewer.MouseMode.Selection; this.pdfViewerControl1.Name = "pdfViewerControl1"; this.pdfViewerControl1.RegistrationKey = null; this.pdfViewerControl1.RegistrationName = null; this.pdfViewerControl1.ShowToolbarFind = false; this.pdfViewerControl1.Size = new System.Drawing.Size(866, 662); this.pdfViewerControl1.TabIndex = 0; this.pdfViewerControl1.PreProcessKey += new Bytescout.PDFViewer.PreProcessKeyEventHandler(this.PdfViewerControl1_PreProcessKey); this.pdfViewerControl1.CurrentPageIndexChanged += new System.EventHandler(this.PdfViewerControl1_CurrentPageIndexChanged); this.pdfViewerControl1.SelectionChanged += new Bytescout.PDFViewer.SelectionChangedEventHandler(this.PdfViewerControl1_SelectionChanged); this.pdfViewerControl1.ValidateContextMenu += new Bytescout.PDFViewer.ValidateContextMenuEventHandler(this.PdfViewerControl1_ValidateContextMenu); // // toolStrip1 // this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.tsbOpen}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.Size = new System.Drawing.Size(1153, 25); this.toolStrip1.TabIndex = 1; this.toolStrip1.Text = "toolStrip1"; // // tsbOpen // this.tsbOpen.ImageTransparentColor = System.Drawing.Color.Magenta; this.tsbOpen.Name = "tsbOpen"; this.tsbOpen.Size = new System.Drawing.Size(64, 22); this.tsbOpen.Text = "&Open PDF"; this.tsbOpen.Click += new System.EventHandler(this.tsbOpen_Click); // // btnProceed // this.btnProceed.Location = new System.Drawing.Point(140, 207); this.btnProceed.Margin = new System.Windows.Forms.Padding(2); this.btnProceed.Name = "btnProceed"; this.btnProceed.Size = new System.Drawing.Size(129, 23); this.btnProceed.TabIndex = 4; this.btnProceed.Text = "Perform Removal"; this.btnProceed.UseVisualStyleBackColor = true; this.btnProceed.Click += new System.EventHandler(this.BtnProceed_Click); // // tbSearchExpression // this.tbSearchExpression.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.tbSearchExpression.Location = new System.Drawing.Point(68, 19); this.tbSearchExpression.Name = "tbSearchExpression"; this.tbSearchExpression.Size = new System.Drawing.Size(183, 20); this.tbSearchExpression.TabIndex = 0; // // btnFind // this.btnFind.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnFind.Location = new System.Drawing.Point(176, 69); this.btnFind.Name = "btnFind"; this.btnFind.Size = new System.Drawing.Size(75, 23); this.btnFind.TabIndex = 2; this.btnFind.Text = "Find All"; this.btnFind.UseVisualStyleBackColor = true; this.btnFind.Click += new System.EventHandler(this.BtnFindAll_Click); // // groupBox1 // this.groupBox1.Controls.Add(this.cbRegex); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.tbSearchExpression); this.groupBox1.Controls.Add(this.btnFind); this.groupBox1.Location = new System.Drawing.Point(12, 28); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(257, 98); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Find"; // // cbRegex // this.cbRegex.AutoSize = true; this.cbRegex.Location = new System.Drawing.Point(9, 45); this.cbRegex.Name = "cbRegex"; this.cbRegex.Size = new System.Drawing.Size(144, 17); this.cbRegex.TabIndex = 1; this.cbRegex.Text = "Use Regular Expressions"; this.cbRegex.UseVisualStyleBackColor = true; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(6, 22); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(56, 13); this.label1.TabIndex = 6; this.label1.Text = "Find what:"; // // cbMaskRemovedText // this.cbMaskRemovedText.AutoSize = true; this.cbMaskRemovedText.Location = new System.Drawing.Point(12, 151); this.cbMaskRemovedText.Name = "cbMaskRemovedText"; this.cbMaskRemovedText.Size = new System.Drawing.Size(238, 17); this.cbMaskRemovedText.TabIndex = 2; this.cbMaskRemovedText.Text = "Draw black rectangles over the removed text"; this.toolTip1.SetToolTip(this.cbMaskRemovedText, "Mask removed text fragments with black rectangles to make the output document loo" + "k like \"censored\"."); this.cbMaskRemovedText.UseVisualStyleBackColor = true; // // cbMakeUnsearchable // this.cbMakeUnsearchable.AutoSize = true; this.cbMakeUnsearchable.Location = new System.Drawing.Point(12, 174); this.cbMakeUnsearchable.Name = "cbMakeUnsearchable"; this.cbMakeUnsearchable.Size = new System.Drawing.Size(221, 17); this.cbMakeUnsearchable.TabIndex = 3; this.cbMakeUnsearchable.Text = "Make the output document unsearchable"; this.toolTip1.SetToolTip(this.cbMakeUnsearchable, "Make the output document unsearchable. If checked, all PDF pages will be replaced" + " with rendered images."); this.cbMakeUnsearchable.UseVisualStyleBackColor = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1153, 702); this.Controls.Add(this.cbMakeUnsearchable); this.Controls.Add(this.cbMaskRemovedText); this.Controls.Add(this.groupBox1); this.Controls.Add(this.btnProceed); this.Controls.Add(this.pdfViewerControl1); this.Controls.Add(this.toolStrip1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form1"; this.Text = "PDF Data Remover Tool"; this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private Bytescout.PDFViewer.PDFViewerControl pdfViewerControl1; private System.Windows.Forms.ToolStrip toolStrip1; private System.Windows.Forms.ToolStripButton tsbOpen; private System.Windows.Forms.Button btnProceed; private System.Windows.Forms.TextBox tbSearchExpression; private System.Windows.Forms.Button btnFind; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.CheckBox cbRegex; private System.Windows.Forms.Label label1; private System.Windows.Forms.CheckBox cbMaskRemovedText; private System.Windows.Forms.CheckBox cbMakeUnsearchable; private System.Windows.Forms.ToolTip toolTip1; } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Form1.cs
      
using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Windows.Forms; using Bytescout.PDFExtractor; using Bytescout.PDFViewer; namespace Sample_UI_Application { public partial class Form1 : Form { // Keep selected rectangles for all document pages Dictionary<int, RectangleF[]> _foundTextRectangles = new Dictionary<int, RectangleF[]>(); public Form1() { InitializeComponent(); // Tune PDF Viewer control pdfViewerControl1.MouseMode = MouseMode.Selection; pdfViewerControl1.MultiSelectMode = true; pdfViewerControl1.AllowResizeSelectionRectangles = true; pdfViewerControl1.ShowResizeHandlesForActiveSelectionOnly = true; pdfViewerControl1.ClearSelectionOnClick = false; // Load document into PDF viewer pdfViewerControl1.InputFile = "sample.pdf"; } private void tsbOpen_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Title = @"Open PDF Document"; openFileDialog.Filter = @"PDF Files (*.pdf)|*.pdf|All Files|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { this.Text = openFileDialog.FileName; Cursor = Cursors.WaitCursor; try { // Open file in PDF Viewer control pdfViewerControl1.InputFile = openFileDialog.FileName; } catch (Exception exception) { MessageBox.Show(exception.Message); } finally { Cursor = Cursors.Default; } } } } private void PdfViewerControl1_SelectionChanged(object sender, SelectionChange selectionChange, int selectionIndex) { // Store selection changes if (selectionChange != SelectionChange.Cleared) { _foundTextRectangles[pdfViewerControl1.CurrentPageIndex] = pdfViewerControl1.SelectionInPoints; } } private void PdfViewerControl1_CurrentPageIndexChanged(object sender, EventArgs e) { // Show stored selection on page switching if (_foundTextRectangles.ContainsKey(pdfViewerControl1.CurrentPageIndex)) { pdfViewerControl1.SelectionInPoints = _foundTextRectangles[pdfViewerControl1.CurrentPageIndex]; } } private void PdfViewerControl1_ValidateContextMenu(object source, ContextMenuStrip menu) { // Add context menu item to delete active selection menu.Items.Insert(0, new ToolStripMenuItem("Delete active selection", null, (sender, args) => { int activeSelectionIndex = pdfViewerControl1.ActiveSelectionIndex; if (activeSelectionIndex != -1) pdfViewerControl1.RemoveSelectionAt(activeSelectionIndex); }) { Enabled = pdfViewerControl1.ActiveSelectionIndex != -1 }); menu.Items.Insert(1, new ToolStripSeparator()); } private void PdfViewerControl1_PreProcessKey(object source, Keys keyData, ref bool handled) { // `Delete` key will delete active selection if (keyData == Keys.Delete) { int activeSelectionIndex = pdfViewerControl1.ActiveSelectionIndex; if (activeSelectionIndex != -1) { pdfViewerControl1.RemoveSelectionAt(activeSelectionIndex); handled = true; } } } private void BtnFindAll_Click(object sender, EventArgs e) { if (tbSearchExpression.Text.Length > 1) { // Prepare TextExtractor using (TextExtractor textExtractor = new TextExtractor("demo", "demo")) { // Load document into TextExtractor textExtractor.LoadDocumentFromFile(pdfViewerControl1.InputFile); // Set options from UI textExtractor.RegexSearch = cbRegex.Checked; textExtractor.WordMatchingMode = WordMatchingMode.None; // Search for text in all pages and store rectangles of found pieces for (int pageIndex = 0; pageIndex < textExtractor.GetPageCount(); pageIndex++) { ISearchResult[] searchResults = textExtractor.FindAll(pageIndex, tbSearchExpression.Text, caseSensitive: true); if (searchResults.Length > 0) { _foundTextRectangles[pageIndex] = searchResults.Select(searchResult => searchResult.Bounds).ToArray(); } } } // Select fount rectangles in PDF Viewer if (_foundTextRectangles.ContainsKey(pdfViewerControl1.CurrentPageIndex)) pdfViewerControl1.SelectionInPoints = _foundTextRectangles[pdfViewerControl1.CurrentPageIndex]; } else { MessageBox.Show(@"Try larger search string"); } } private void BtnProceed_Click(object sender, EventArgs e) { var outputFile = "output.pdf"; MemoryStream tempStream = new MemoryStream(); // Create `Bytescout.PDFExtractor.Remover2` instance using (Remover2 remover = new Remover2("demo", "demo")) { // Load document into remover remover.LoadDocumentFromFile(pdfViewerControl1.InputFile); // Set options from UI remover.MaskRemovedText = cbMaskRemovedText.Checked; remover.MakePDFUnsearchable = cbMakeUnsearchable.Checked; // Add fragments to remove foreach (KeyValuePair<int, RectangleF[]> keyValuePair in _foundTextRectangles) remover.AddTextToRemove(keyValuePair.Key, keyValuePair.Value); // Perform removal and save result document to file remover.PerformRemoval(outputFile); } // Open output PDF file in default associated application Process.Start(outputFile); } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Program.cs
      
using System; using System.Collections.Generic; using System.Windows.Forms; namespace Sample_UI_Application { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit ByteScout PDF Suite Home Page

Explore ByteScout PDF Suite Documentation

Explore Samples

Sign Up for ByteScout PDF Suite Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Tutorials:

prev
next