@neerajpokh
Please first try the code for what you asked then talk about your integration issue please.
However, please find the updated code for your use case(Insert text and image + Get X and Y coordinates for given text).
Code:
using (var reader = new PdfReader("C:\\Users\\Documents\\Loading.pdf"))
{
using (var stamper = new PdfStamper(reader, new FileStream("C:\\Deenu\\RPA.pdf", FileMode.Create)))
{
var count = reader.NumberOfPages;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfContentByte content = stamper.GetOverContent(i);
iTextSharp.text.pdf.PdfDocument doc = new iTextSharp.text.pdf.PdfDocument();
if (i == 1)
{
//add text
content.BeginText();
iTextSharp.text.Rectangle pagesize = reader.GetPageSize(1);
float x = pagesize.Left + 200;
float y = pagesize.Top - 40;
content.SetFontAndSize(BaseFont.CreateFont(), 12);
content.ShowTextAligned(0, "Contents", x, y, 0);
content.EndText();
}
// Add image
if (count == i)
{
iTextSharp.text.Rectangle pagesize = reader.GetPageSize(i);
//float x = pagesize.Left + 100;
//float y = pagesize.Top - 680;
var Str_Cordinates_X = "";
var Str_Cordinates_Y = "";
var textfound = false;
float x = 0;
float y = 0;
string text = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i);
// Split the extracted text into lines
string[] lines = text.Split('\n');
foreach (string line in lines)
{
// Check if the search text is found in the current line
if (line.Contains("Loading Reference: 12345 "))
{
// Output page number and coordinates of the found text
var StrPageNum = Convert.ToString(i);
Str_Cordinates_X = Convert.ToString(reader.GetPageSize(i).Width / 2);
Str_Cordinates_Y = Convert.ToString(reader.GetPageSize(i).Top - reader.GetPageSize(i).Bottom);
textfound = true;
}
if (textfound == true)
{
break;
}
//break;
}
//Str_Cordinates_X= float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
x = float.Parse(Str_Cordinates_X, CultureInfo.InvariantCulture.NumberFormat);
y = float.Parse(Str_Cordinates_Y, CultureInfo.InvariantCulture.NumberFormat);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:\\Users\\Downloads\\Download BAckup\\Article1\\6.png");
//image.SetAbsolutePosition(100, 100); // Set position
//object result=stamper.fin
//content.ShowTextKerned("on the date first above written:");
image.SetAbsolutePosition(x, pagesize.Top - 680);
content.AddImage(image);
}
}
}
}
Hope you are using System.Globalization dll as well for your above code.
Thanks,
Deenuji Loganathan
🤖 Automation Evangelist
Deenuji - Follow me on LinkedIn
-------------------------------------------------------------------------------------------------------------
If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🌟