iTextSharp remove text from static PDF document C#

The following code makes a white image over the text i want to hide from the user, it then makes the user not able to copy or paste into the pdf so they cannot select the hidden text and copy the value.

//Path to where you want the file to output
string outputFilePath = "C:\\test.pdf";
//Path to where the pdf you want to modify is
string inputFilePath = "C:\\input.pdf";
	using (Stream inputPdfStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
	using (Stream outputPdfStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
	using (Stream outputPdfStream2 = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
		//Opens the unmodified PDF for reading
		var reader = new PdfReader(inputPdfStream);
		//Creates a stamper to put an image on the original pdf
		var stamper = new PdfStamper(reader, outputPdfStream) { FormFlattening = true, FreeTextFlattening = true };
		//Creates an image that is the size i need to hide the text i'm interested in removing
		iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Bitmap(120, 20), BaseColor.WHITE);
		//Sets the position that the image needs to be placed (ie the location of the text to be removed)
		image.SetAbsolutePosition(245, 462);
		//Adds the image to the output pdf
		stamper.GetOverContent(1).AddImage(image, true);
		//Creates the first copy of the outputted pdf

		//Opens our outputted file for reading
		var reader2 = new PdfReader(outputPdfStream2);

		//Encrypts the outputted PDF to make it not allow Copy or Pasting
		//Creates the outputted final file
catch (Exception ex)

If you have any questions like why you have to do it this ridiculous way let me know.

12 thoughts on “iTextSharp remove text from static PDF document C#

    1. I found this using GIMP, and print screen. you’ll also need to change the line above that that makes the white image to the width you needs for the text you are covering up šŸ™‚

    1. absolutely true! you’d need to page through each of the pdf pages and apply the same method. a


      loop should solve this.

  1. Hello Thanks for the nice piece of code, my question is how do we remove back this image drawn later if we need to ?



    1. you’d need to get the array of images and cycle through it…then delete the image that matches the one you added.

      1. Thanks for the reply. Came across same answer from many but no one have shared any snippets..every one is sharing only the image adding part would be great if someone can share the snippet for the adding and then deleting the added images.. thanks again,


  2. How do i add the white image, if the my pdf have 3 pages. I want to add the image on the 3 page of my PDF. With your code i can only add image on the very first page of my PDF.

    1. You are correct, you basically have to lock copy + paste to make that not possible (lock in the PDF settings). There isn’t any easy way to delete text (actually delete) from a PDF…you could probably hack something together but it wouldn’t be something simple (or likely repeatable).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s