iText7 remove stamp

253 views Asked by At

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

1

There are 1 answers

0
mkl On BEST ANSWER

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)