Free Support Forum - aspose.com

Elements under stamps are still selectable

I’m drawing white boxes to cover certain elements in my pdf like this:

            using (var imageStream = new MemoryStream())
            {
                using (Bitmap bitmap = new Bitmap((int)tWidthPx, (int)tHeightPx))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {

                        using (SolidBrush brush = new SolidBrush(Color.White))
                        {
                            graphics.FillRectangle(brush, new Rectangle(0, 0, bitmap.Width, bitmap.Height));

                            bitmap.Save(imageStream, ImageFormat.Png);
                        }
                    }
                }

                var stamp = new ImageStamp(imageStream)
                {
                    XIndent = tTopLeftX,
                    YIndent = tTopLeftY,
                    Height = tHeight,
                    Width = tiWidth,
                    Background = false,
                    Quality = 1,
                };
 
                pdfDocument.Pages[pageNumber].AddStamp(stamp);
            }

However, when I save this pdf and inspect it with a pdf viewer, I can still select the elements that are under the stamp. I have tried calling pdfDocument.Flatten(); but it doesn’t seem to have any effect. How can I make the elements beneath the stamp completely hidden?

@Kranton

You can redact page region or text using RedactAnnotation offered by the API. If you still face any issue, please share your sample PDF with us along with information to hide/redact the information. We will test the scenario in our environment and address it accordingly.

Thanks, this works! However, later when we try to apply some text using Textbuilder like this:

TextBuilder textBuilder = new TextBuilder(pdfDocument.Pages[1]); //failes here textBuilder.AppendText(textFragment);

Now fails with this:

Message:
System.IndexOutOfRangeException : At most 4 elements (for any collection) can be viewed in evaluation mode.
Stack Trace:

#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zI_zLX8pciVi8vwHToiuwDuWEsOerLI6R6YA1q$XUDKkq(Object #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z_6$xs1x7GrNhR0Gean$o6mfUbY9eJzcVmg==(#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU= #=zgS1Bb4g=, #=qFyidd9jYZ8M4njARx5i7FLfue7QsIvI9iF8VCgC2NRw= #=z$olmFAQ=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zeMlKYC7gnXEd8dgTqn_DKv7P7lz9aZReujk5hIw=()
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zRU3mzOEe7507Z5_KB1mj8TYoCORhWzMGbNveEO8=(Boolean #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zI_zLX8pciVi8vwHToiuwDuWEsOerLI6R6YA1q$XUDKkq(Object #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z6_v4uwGs2kWptsT8dFiUPy2njPLgAb9mAg==()
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z4WQjrAvYQMYibHlpeSv_6w7XFnbctASUnzHnLeA=(Object #=zgS1Bb4g=, UInt32 #=z$olmFAQ=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zRU3mzOEe7507Z5_KB1mj8TYoCORhWzMGbNveEO8=(Boolean #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=ze6ElvPklaMlz2utVRDiIcKGCoYVbw0xLFB9jFh4=()
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z_g7gmV5s3EOSnC1O5adoksQ=(Object[] #=zgS1Bb4g=, Type[] #=z$olmFAQ=, Type[] #=zzjmlbu8=, Object[] #=zIk7QF9s=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zchUqj7_c6tPX9MQYb9Jp0bu7qdvOzeIdhC3i8oI=(Stream #=zgS1Bb4g=, String #=z$olmFAQ=, Object[] #=zzjmlbu8=, Type[] #=zIk7QF9s=, Type[] #=zEzIQq14=, Object[] #=zseUV_3o=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zSeiSCkrMxDeiZZLa9x_rRtkV$LYf1f7GRmUp$EA=(Stream #=zgS1Bb4g=, String #=z$olmFAQ=, Object[] #=zzjmlbu8=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zKMNVEQvzpoHeB7mfal3YZL96IuPB4z24Iw==(Stream #=zgS1Bb4g=, String #=z$olmFAQ=, Object[] #=zzjmlbu8=)
Document.#=zFqlDfF4=(Document #=zkz3ycYA=, Int32 #=zenVUXrk=)
Document.#=zFqlDfF4=(Document #=zkz3ycYA=, #=z4nIKqbE9ZJl05QDVLGAEdLFonq689cFRDg== #=z8jLUQGU=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zI_zLX8pciVi8vwHToiuwDuWEsOerLI6R6YA1q$XUDKkq(Object #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zJexMk5a7IItffnBPq$CNL_PLjyd0te7Bw7bRGGr3D6gf(MethodBase #=zgS1Bb4g=, Boolean #=z$olmFAQ=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zBTmhifxOGcR8d$U9UDFxN9tTuDEo(#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU= #=zgS1Bb4g=, #=qFyidd9jYZ8M4njARx5i7FLfue7QsIvI9iF8VCgC2NRw= #=z$olmFAQ=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zeMlKYC7gnXEd8dgTqn_DKv7P7lz9aZReujk5hIw=()
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zRU3mzOEe7507Z5_KB1mj8TYoCORhWzMGbNveEO8=(Boolean #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zI_zLX8pciVi8vwHToiuwDuWEsOerLI6R6YA1q$XUDKkq(Object #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z6_v4uwGs2kWptsT8dFiUPy2njPLgAb9mAg==()
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z4WQjrAvYQMYibHlpeSv_6w7XFnbctASUnzHnLeA=(Object #=zgS1Bb4g=, UInt32 #=z$olmFAQ=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zRU3mzOEe7507Z5_KB1mj8TYoCORhWzMGbNveEO8=(Boolean #=zgS1Bb4g=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=ze6ElvPklaMlz2utVRDiIcKGCoYVbw0xLFB9jFh4=()
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=z_g7gmV5s3EOSnC1O5adoksQ=(Object[] #=zgS1Bb4g=, Type[] #=z$olmFAQ=, Type[] #=zzjmlbu8=, Object[] #=zIk7QF9s=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zchUqj7_c6tPX9MQYb9Jp0bu7qdvOzeIdhC3i8oI=(Stream #=zgS1Bb4g=, String #=z$olmFAQ=, Object[] #=zzjmlbu8=, Type[] #=zIk7QF9s=, Type[] #=zEzIQq14=, Object[] #=zseUV_3o=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zSeiSCkrMxDeiZZLa9x_rRtkV$LYf1f7GRmUp$EA=(Stream #=zgS1Bb4g=, String #=z$olmFAQ=, Object[] #=zzjmlbu8=)
#=qjrpJuo1_FRjl_sVBWiWObADgzsOzryqiDMqs_BfgySU=.#=zKMNVEQvzpoHeB7mfal3YZL96IuPB4z24Iw==(Stream #=zgS1Bb4g=, String #=z$olmFAQ=, Object[] #=zzjmlbu8=)
XFormCollection.#=zZ9HYZgQ=(String #=znElOPpE=)
XFormCollection.get_Item(String name)
#=zrD4tI$NSsgP_Wp1tD0CJVSFBVf7DPm6FOf09N1$hm57mtnDC_OGjzUg=.#=zbC1Ba9s=()
#=zRilPfW9yjsyyd9M4PS5ryiVoXpi92BP5J5B7_acnRuE6_Xx4OxfLIUcO7UHn.#=z4i2s6L7htg7g(BaseOperatorCollection #=zQncPh4I=, Resources #=zlu_bCaA=, Page #=zaWlAJpI=)
#=zRilPfW9yjsyyd9M4PS5ryiVoXpi92BP5J5B7_acnRuE6_Xx4OxfLIUcO7UHn.#=z4i2s6L7htg7g(BaseOperatorCollection #=zQncPh4I=, Resources #=zlu_bCaA=)
#=zRilPfW9yjsyyd9M4PS5ryiVoXpi92BP5J5B7_acnRuE6_Xx4OxfLIUcO7UHn.#=zLRkRCDs=()
#=zRilPfW9yjsyyd9M4PS5ryiVoXpi92BP5J5B7_acnRuE6_Xx4OxfLIUcO7UHn.ctor(Page #=zaWlAJpI=, TextSearchOptions #=zz0kcByuxOQkm, Resources #=zlu_bCaA=, BaseOperatorCollection #=zukKdbR0=)
#=zRilPfW9yjsyyd9M4PS5ryiVoXpi92BP5J5B7_acnRuE6_Xx4OxfLIUcO7UHn.ctor(Page #=zaWlAJpI=, BaseOperatorCollection #=zukKdbR0=)
TextBuilder.ctor(Page page)

Any idea whats going on here? Creating a Textbuilder with the code in the opening post worked just fine but here it just fails with an cryptic error. I think there might be a bug?

EDIT:

In the sample code you are calling
annotation.Redact()
Directly after adding the annotation to the annotation collection. I commented that out, added my text and then I iterate the Annotations and call Redact before saving. However this caueses some redactoAnnotations to not be filled or come on top of elements that doesnt happen if you call Redact() immediately. I think there might be some kind of bug here?

EDIT:
So I switched from from using Textbuilder entirely and are now using Textstamp instead and this seems to work without any problems. :slight_smile:

@Kranton

This specific exception is thrown due to trial version usage of the API with which you can only process 4 elements of any collection e.g. Pages, TextFragments, Annotations, etc. In order to evaluate the API without any restrictions, please apply for a 30-days free temporary license. This will allow you to have full access to API features. Please let us know in case you still face any issue with valid license.

Aha, I see. My Company has a license, I haven’t gotten it yet though.

So Stamp isn’t part of any of these Collections?

@Kranton

Yes, you are right. Number of added stamps are not limited in trial version.