I am needing to insert text into a PDF at specific coordinate locations. The original coordinates are derived from FindText. I need to add text in a postion just above the found text. (Y postion).
Coordinate example: {X=316.24, Y=487.72}
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputFile);
PdfPageBase page = doc.Pages[0];
// Searches text by ignoring case
PdfTextFindCollection collection = page.FindText(searchText, TextFindParameter.IgnoreCase);
// Creates a brush
PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
// Defines a font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 6f, FontStyle.Regular),true);
foreach (PdfTextFind find in collection.Finds)
{
List < RectangleF > list = find.TextBounds;
foreach (RectangleF rec in list)
{
// Get X position
float xPosition = rec.X;
//Get Y position
float yPosition = rec.Y;
// Draws the text as defined font and color
page.Canvas.DrawString(insertText, font, brush, xPosition,yPosition);
}
}
//Save the document
doc.SaveToFile(resultFile);string insertText = "NEW TEXT";
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputFile);
PdfTextFind[] results1 = null;
PdfTextFind[] results2 = null;
foreach (PdfPageBase page in doc.Pages)
{
int rotation = (int)page.Rotation;
if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
{
rotation += (int)PdfPageRotateAngle.RotateAngle270;
page.Rotation = (PdfPageRotateAngle)rotation;
}
if (page.Rotation == PdfPageRotateAngle.RotateAngle180)
{
rotation += (int)PdfPageRotateAngle.RotateAngle180;
page.Rotation = (PdfPageRotateAngle)rotation;
}
if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
{
rotation += (int)PdfPageRotateAngle.RotateAngle90;
page.Rotation = (PdfPageRotateAngle)rotation;
}
results1 = page.FindText("Cardholder Signature").Finds;
foreach (PdfTextFind text1 in results1)
{
PointF chs1 = text1.Position;
Console.WriteLine("Location of Cardholder Signature: " + chs1);
//Console.ReadLine();
float xPosition1 = text1.Position.X;
float yPosition1 = text1.Position.Y;
PdfBrush brush = new PdfSolidBrush(Color.Red);
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular), true);
Console.WriteLine("Location of Supervisor/Manager Signature (X): " + xPosition1);
Console.WriteLine("Location of Supervisor/Manager Signature (Y): " + yPosition1);
page.Canvas.DrawString(insertText, font, brush, xPosition1, yPosition1);
}
}