简体   繁体   中英

iText7 remove stamp

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?

在此处输入图像描述

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);

Ie as a stamp annotation with stamp name StampRML .

Thus, to remove it again, simply remove all annotations with that stamp name, eg 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM