Search code examples
c#imageitextpdf-annotations

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


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?
                }
            }

Solution

  • @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);
            }
        }
    }