1

First, I add a stamp to pdf files used iText7, about drawing's rev date... I get it. Second, if I update the drawings, need to update the stamp information, the simplest, to delete the stamp and add it again. But, I can't get the stamps in pdf.

PdfArray stamps = page.GetPdfObject().GetAsArray(PdfName.Stamp);

I find this way to get stamps, but the stamp is null. What should I do?

enter image description here

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
SHEN
  • 13
  • 3
  • 1
    How exactly did you add those stamps? – mkl Jun 02 '21 at 04:37
  • PdfStampAnnotation stampAnno = new PdfStampAnnotation(stampRect) .SetStampName(new PdfName("StampRML")); PdfFormXObject stampObj = tempDoc.GetFirstPage().CopyAsFormXObject(pdfDoc); stampAnno.SetNormalAppearance(stampObj.GetPdfObject()); stampAnno.SetFlags(PdfAnnotation.PRINT); page.AddAnnotation(stampAnno); – SHEN Jun 02 '21 at 07:56
  • Does my proposal work for you? – mkl Jun 04 '21 at 13:35
  • Yes, I have succeeded, Thanks – SHEN Jun 07 '21 at 01:03
  • Great! In that case it would be nice to mark my answer as accepted (by clicking the check mark at its upper left, see [here](https://meta.stackexchange.com/a/5235/460854)). – mkl Jun 07 '21 at 04:38
  • 1
    ok,it‘s my first to used it – SHEN Jun 07 '21 at 07:01

1 Answers1

0

According to a comment you added the stamps in question like this:

PdfStampAnnotation stampAnno = new PdfStampAnnotation(stampRect).SetStampName(new PdfName("StampRML"));
PdfFormXObject stampObj = tempDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);
stampAnno.SetNormalAppearance(stampObj.GetPdfObject());
stampAnno.SetFlags(PdfAnnotation.PRINT);
page.AddAnnotation(stampAnno);

I.e. as a stamp annotation with stamp name StampRML.

Thus, to remove it again, simply remove all annotations with that stamp name, e.g. like this:

using (PdfReader pdfReader = new PdfReader(SOURCE_WITH_STAMP))
using (PdfWriter pdfWriter = new PdfWriter(RESULT_WITHOUT_STAMP))
using (PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter))
{
    for (int pageNr = 1; pageNr <= pdfDocument.GetNumberOfPages(); pageNr++)
    {
        PdfPage page = pdfDocument.GetPage(pageNr);
        IList<PdfAnnotation> annotations = page.GetAnnotations();
        for (int i = annotations.Count - 1; i >= 0; i--)
        {
            PdfAnnotation annotation = annotations[i];
            if (annotation is PdfStampAnnotation stamp)
            {
                if ("/StampRML" == stamp.GetStampName()?.ToString())
                {
                    page.RemoveAnnotation(stamp);
                }
            }
        }
    }
}

(RemoveStampAnnotation test testRemoveStampByShen)

mkl
  • 90,588
  • 15
  • 125
  • 265