Multiple signatures on PDF

Hi,

I am using ASPOSE.PDF to replace text with an image into PDF. I have a situation where I need to insert different images on one PDF page. The code below is only inserting one image on a given page. Can you see what’s wrong here??

       using (FileStream outputStream = new FileStream(@outputFileName, FileMode.Create))
        using (FileStream signatureImageStream = new FileStream(@signatureFile, FileMode.Open))
        using (Document pdfDocument = new Document(inputFileName))
        {
            foreach (Page page in pdfDocument.Pages)
            {
                // Create TextAbsorber object to find all instances of the input search phrase
                TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchText);

                //Console.WriteLine("!!!Create TextAbsorber object to find all instances of the input search phrase!!!!");

                page.Accept(textFragmentAbsorber);

                //Console.WriteLine("!!!Accept the absorber for all the pages!!!!");

                // Get the extracted text fragments
                TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;


                if (textFragmentCollection.Count == 0)
                {
                    page.Dispose();
                    continue;
                }

                int textFragmentCount = 0;
                foreach (TextFragment textFragment in textFragmentCollection)
                {
                    textFragment.Text = String.Empty;
                    textFragmentCount += 1;
                    //Console.WriteLine("Text Fragment Count: {0}", textFragmentCount);

                    // Set coordinates
                    int lowerLeftX = (int)textFragment.Position.XIndent;
                    int lowerLeftY = (int)textFragment.Position.YIndent;
                    int upperRightX = (int)textFragment.Position.XIndent + 100;
                    int upperRightY = (int)textFragment.Position.YIndent + 25;

                    Rectangle rectangle = new Rectangle(
                        lowerLeftX,
                        lowerLeftY,
                        upperRightX,
                        upperRightY
                    );

                    Matrix matrix = new Matrix(new double[] {
                        rectangle.URX - rectangle.LLX,
                        0,
                        0,
                        rectangle.URY - rectangle.LLY,
                        rectangle.LLX, rectangle.LLY
                    });

                    page.Resources.Images.Add(signatureImageStream);

                    page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));

                    XImage ximage = page.Resources.Images[page.Resources.Images.Count];

                    page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));

                    page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
                }
            }

            pdfDocument.Save(outputStream);

Thank you

I think I found the answer to my problem. This is the code I am using now and it seems to work well.

        Rectangle imageRectangle = new Rectangle(0, 0, 30, 15);
        using (Document document = new Document(inputFileName))
        {
            using (var imageStream = File.Open(signatureFile, FileMode.Open))
            {
                XImage image = null;
                foreach (Page page in document.Pages)
                {
                    // Create TextAbsorber object to find all instances of the input search phrase
                    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchText);

                    //Console.WriteLine("!!!Create TextAbsorber object to find all instances of the input search phrase!!!!");

                    page.Accept(textFragmentAbsorber);

                    //Console.WriteLine("!!!Accept the absorber for all the pages!!!!");

                    // Get the extracted text fragments
                    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

                    foreach(TextFragment textFragment in textFragmentCollection)
                    {
                        // Set coordinates
                        int lowerLeftX = (int)textFragment.Position.XIndent;
                        int lowerLeftY = (int)textFragment.Position.YIndent;
                        int upperRightX = (int)textFragment.Position.XIndent + 100;
                        int upperRightY = (int)textFragment.Position.YIndent + 25;

                        Rectangle rectangle = new Rectangle(
                            lowerLeftX,
                            lowerLeftY,
                            upperRightX,
                            upperRightY
                        );

                        Matrix matrix = new Matrix(new double[] {
                        rectangle.URX - rectangle.LLX,
                        0,
                        0,
                        rectangle.URY - rectangle.LLY,
                        rectangle.LLX, rectangle.LLY
                    });

                        WatermarkAnnotation annotation = new WatermarkAnnotation(page, page.Rect);
                        XForm form = annotation.Appearance["N"];
                        form.BBox = page.Rect;

                        string name;
                        if (image == null)
                        {
                            name = form.Resources.Images.Add(imageStream);
                            image = form.Resources.Images[name];
                        }
                        else
                        {
                            name = form.Resources.Images.Add(image);
                        }

                        form.Contents.Add(new Aspose.Pdf.Operators.GSave());
                        form.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
                        form.Contents.Add(new Aspose.Pdf.Operators.Do(name));
                        form.Contents.Add(new Aspose.Pdf.Operators.GRestore());

                        page.Annotations.Add(annotation, false);
                    }

                    image = null;
                }
            }

            document.Save(outputFileName);
        }

@harishupad

Thank you for sharing the solution.

It will certainly help other community members with same requirements. Please feel free to contact us if you need any further assistance.