Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Mar 03, 2022 4:39 pm

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}

edmondok
 
Posts: 2
Joined: Thu Jun 17, 2021 1:56 pm

Fri Mar 04, 2022 3:46 am

Hello,

Thanks for your inquiry.
Please refer to the following code to achieve your needs. If there is any question, just feel free to contact us.
Code: Select all
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);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Mon Mar 07, 2022 3:42 pm

I can insert text at the location now, but the text is vertical rather than horizontal. Can you help with getting it to write out horizontally?

Here is my code:

string insertText = "NEW TEXT";
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"c:\temp\test.pdf");

PdfTextFind[] results1 = null;
foreach (PdfPageBase page in doc.Pages)
{
results1 = page.FindText("Supervisor/Manager").Finds;
foreach (PdfTextFind text in results1)
{
float xPosition = text.Position.X;
float yPosition = text.Position.Y;
PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 6f, FontStyle.Regular), true);
Console.WriteLine("Location of Supervisor/Manager Signature (X): " + xPosition);
Console.WriteLine("Location of Supervisor/Manager Signature (Y): " + yPosition);
//Console.ReadLine();
page.Canvas.DrawString(insertText, font, brush, xPosition, yPosition);
doc.SaveToFile(@"C:\Temp\OutputTest.pdf");

Image


Would a stamp work better?

Thank you for your help.

edmondok
 
Posts: 2
Joined: Thu Jun 17, 2021 1:56 pm

Tue Mar 08, 2022 2:27 am

Hello,

Thanks for your feedback.
To help us investigate your issue quickly and efficiently, please provide your input PDF file. You could attach it here or send it to us via email ([email protected]). Thanks in advance.
In addition, I can't view the picture you provided, please send it to us again.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Wed Mar 09, 2022 3:04 am

Hello,

Thanks for sharing the information via email.
After investigation, I found that the page of the input file you provided was rotated 90 degrees causing the added text to be vertical. Please use the following code to turn the PDF page back to the source location.
Code: Select all
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);
    }
}

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF

cron