This article demonstrates how to add line numbers before chunks of text in a PDF page by using Spire.PDF for .NET.
Below is a screenshot of the input document.
C#
using Spire.Pdf; using Spire.Pdf.General.Find; using Spire.Pdf.Graphics; using System.Drawing; namespace AddLineNumber { class Program { static void Main(string[] args) { //Create a PdfDocument of instance PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile(@"G:\360MoveData\Users\Administrator\Desktop\sample.pdf"); //Get the first page PdfPageBase page = doc.Pages[0]; //Find the spcific text in the fisrt line PdfTextFind topLine = page.FindText("C# (pronounced See Sharp)", TextFindParameter.None).Finds[0]; //Get the line height float lineHeight = topLine.Bounds.Height; //Get the Y coordinate of the selected text float y = topLine.Bounds.Y; //Find the spcific text in the second line PdfTextFind secondLine = page.FindText("language. C#", TextFindParameter.None).Finds[0]; //Calculate the line spacing float lineSpacing = secondLine.Bounds.Top - topLine.Bounds.Bottom; //Find the specific text in the last line PdfTextFind bottomLine = page.FindText("allocation of objects", TextFindParameter.None).Finds[0]; //Get the height of the chunks float height = bottomLine.Bounds.Bottom; //Create a font PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 11f); int i = 1; while (y < height) { //Draw line number before a specific line of text page.Canvas.DrawString(i.ToString(), font, PdfBrushes.Black, new PointF(15, y)); y += lineHeight + lineSpacing; i++; } //Save the document doc.SaveToFile("result.pdf"); } } }
VB.NET
Imports Spire.Pdf Imports Spire.Pdf.General.Find Imports Spire.Pdf.Graphics Imports System.Drawing Namespace AddLineNumber Class Program Shared Sub Main(ByVal args() As String) 'Create a PdfDocument of instance Dim doc As PdfDocument = New PdfDocument() 'Load a PDF document doc.LoadFromFile("G:\360MoveData\Users\Administrator\Desktop\sample.pdf") 'Get the first page Dim page As PdfPageBase = doc.Pages(0) 'Find the spcific text in the fisrt line Dim topLine As PdfTextFind = page.FindText("C# (pronounced See Sharp)",TextFindParameter.None).Finds(0) 'Get the line height Dim lineHeight As single = topLine.Bounds.Height 'Get the Y coordinate of the selected text Dim y As single = topLine.Bounds.Y 'Find the spcific text in the second line Dim secondLine As PdfTextFind = page.FindText("language. C#",TextFindParameter.None).Finds(0) 'Calculate the line spacing Dim lineSpacing As single = secondLine.Bounds.Top - topLine.Bounds.Bottom 'Find the specific text in the last line Dim bottomLine As PdfTextFind = page.FindText("allocation of objects",TextFindParameter.None).Finds(0) 'Get the height of the chunks Dim height As single = bottomLine.Bounds.Bottom 'Create a font Dim font As PdfFont = New PdfFont(PdfFontFamily.TimesRoman,11f) Dim i As Integer = 1 While y < height 'Draw line number before a specific line of text page.Canvas.DrawString(i.ToString(),font,PdfBrushes.Black,New PointF(15,y)) y += lineHeight + lineSpacing i = i + 1 End While 'Save the document doc.SaveToFile("result.pdf") End Sub End Class End Namespace
Output