Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

itextsharp, c#, mvc, position

The c#/code below: how to find x,y position of 'text' for example 'signature'. and after it is found, I hope to drop the png image there.
I just need help how to find the x, y position and I already created the code dropping the png image.

Thanks

  public static string AddSignature(string originalPdf, string outputPdf, string signature, int pdfPositionX, int pdfPositionY)
        {
            string path = @"C:\Users\Documents\Workspaces\";
            string pathSignature = @"C:\Users\Documents\signature\";
            using (Stream inputPdfStream = new FileStream(path + originalPdf, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (Stream inputImageStream = new FileStream(pathSignature + signature, FileMode.Open, FileAccess.Read, FileShare.Read))
            using (Stream outputPdfStream = new FileStream(path + outputPdf, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                var reader = new PdfReader(inputPdfStream);
                var stamper = new PdfStamper(reader, outputPdfStream);
                var pdfContentByte = stamper.GetOverContent(1);
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);

                //begin: need logics to find keyword
                PdfReader pdfReader = new PdfReader(path + outputPdf);
                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                {
                    ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

                    string currentPageText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
                    if (currentPageText.Contains("Signature"))
                    {
                        //how to get x, y postion with text called 'signature'
                    }
                }
                //end: need logics to find keyword
                image.SetAbsolutePosition(pdfPositionX, pdfPositionY);
                pdfContentByte.AddImage(image);
                stamper.Close();
            }
            return "Success";
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ashok
Ashok
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ITsolutionWizard

ASKER

Ashok: I do not get it. Can you help me using my codes and see you can add the codes inside of my codes?
First, please test the above code in a separate (new) project.
Supply your actual pdf.  Check to make sure it displays from the statement
Console.WriteLine(string.Format("Found text {0} at {1}x{2}", p.Text, p.Rect.Left, p.Rect.Bottom));

Thanks,
Ashok