0

How can I export the “stamp annotation from image” to an image file?

I have a pdf which contains stamp annotions (in fact it's an image), I want to export all these kinds of images to file and get the x/y position.

I am new to pdf. Any idea or code will be appreciated.

[--- Edited on 2019/08/08 ---]

    private void btnExtractAnnotaion_Click(object sender, EventArgs e)
    {
        PdfReader reader = new PdfReader(this.txtPdf.Text);
        PdfDictionary pageDict = reader.GetPageN(reader.NumberOfPages);
        PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);
        PdfObject annot = null;
        Console.WriteLine("Annotation count:{0}", annotArray.Size);
        for (int i = 0; i < annotArray.Size; i++)
        {
            annot = annotArray.GetDirectObject(i);
            Console.WriteLine(annot.ToString());
            //curAnnot = annotArray.GetAsDict(i);
            //Console.WriteLine(curAnnot.ToString());
            bool btmp = annot.IsDictionary();
            if (btmp)
            {
                PdfDictionary pdfDic = ((PdfDictionary)annot);
                PdfName stamp = pdfDic.GetAsName(PdfName.SUBTYPE);
                if (stamp.Equals(PdfName.STAMP))
                {
                    //PdfObject img = pdfDic.GetDirectObject(PdfName.RECT);
                    // How Can I get the image(png, jpg...) of Stamp?
                }
            }
Swell Yu
  • 50
  • 5
  • It would be awesome if you could provide a [mcve] – jazb Aug 02 '19 at 07:45
  • **A** Please share the example file on a service that does not require registration, and that also does not drown you in adds. Public shares on Google drive or Dropbox are good options. **B** Have you looked at [this answer](https://stackoverflow.com/a/51113117/1729265)? It shows how to copy an image from a source annotation to some target annotation. you actually only need to use the part extracting the image from the source... – mkl Aug 08 '19 at 14:06

2 Answers2

0

iText7 is exactly the thing you want to use for that. ;)

https://www.nuget.org/packages/itext7/

https://itextpdf.com/en/products/itext-7

This is how you could get access to the stamps, depending on the stamping technique used:

using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
    var annotations = doc.GetPage(1).GetAnnotations();
}

on other occasions this could do the trick:

using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
    var res = doc.GetFirstPage().GetResources();
}

unless you provide a demo-pdf, that all the help i can give you

FastJack
  • 866
  • 1
  • 10
  • 11
0

@mkl @FastJack Thanks for your time! By this answer ImageRenderListener

It solved my problem. The following are my codes:

PdfReader reader = new PdfReader(this.txtPdf.Text);
PdfDictionary pageDict = reader.GetPageN(reader.NumberOfPages);
PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);

//Console.WriteLine("Annotation count:{0}", annotArray.Size);
for (int i = 0; i < annotArray.Size; i++)
{
    PdfObject annot = annotArray.GetDirectObject(i);
    //Console.WriteLine(annot.ToString());
    bool btmp = annot.IsDictionary();
    if (btmp)
    {
        PdfDictionary pdfDic = ((PdfDictionary)annot);
        PdfName stamp = pdfDic.GetAsName(PdfName.SUBTYPE);
        if (stamp.Equals(PdfName.STAMP))
        {
            // rects are laid out [llx, lly, urx, ury]
            float x, y, width, height;
            PdfArray rect = pdfDic.GetAsArray(PdfName.RECT);
            x = rect.GetAsNumber(0).FloatValue;
            y = rect.GetAsNumber(1).FloatValue;
            width = rect.GetAsNumber(2).FloatValue - x;
            height = rect.GetAsNumber(3).FloatValue - y;

            PdfDictionary appearancesDic = pdfDic.GetAsDict(PdfName.AP);
            PdfStream normalAppearance = appearancesDic.GetAsStream(PdfName.N);
            PdfDictionary resourcesDic = ormalAppearance.GetAsDict(PdfName.RESOURCES);

            ImageRenderListener listener = new ImageRenderListener();
            PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
            processor.ProcessContent(
                ContentByteUtils.GetContentBytesFromContentObject(normalAppearance),                 resourcesDic);

            System.Drawing.Image drawingImage = listener.Images.First();
            //Image image = Image.GetInstance(drawingImage, drawingImage.RawFormat);
        }
    }
}
Swell Yu
  • 50
  • 5