本文介绍如何使用Spire.PDF for .NET为PDF文档中的文字块加上行号。
原文档:

C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Texts;
using System.Drawing;
namespace AddLineNumber
{
class Program
{
static void Main(string[] args)
{
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//加载PDF文档
doc.LoadFromFile(@""C:\Users\Administrator\Desktop\input.pdf"");
//获取第一页
PdfPageBase page = doc.Pages[0];
//查找第一行中的指定文字
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
PdfTextFragment topLine = finder.Find(""成都冰蓝科技"")[0];
//获取行高
float lineHeight = topLine.Bounds[0].Height;
//获取一个y坐标,用于写入编号的起始y坐标
float y = topLine.Bounds[0].Location.Y - 2;
//获取第二行中的指定文字
PdfTextFinder secondfinder = new PdfTextFinder(page);
secondfinder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
PdfTextFragment secondLine = secondfinder.Find(""Office控件产品"")[0];
//计算行间距
float lineSpacing = secondLine.Bounds[0].Top - topLine.Bounds[0].Bottom;
//查找最后一行中的指定文字
PdfTextFinder bottomfinder = new PdfTextFinder(page);
bottomfinder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;
PdfTextFragment bottomLine = bottomfinder.Find(""开发并向他们"")[0];
//获取获取最后一行的底部y坐标,亦即添加行号区域的高度
float height = bottomLine.Bounds[0].Bottom;
//创建一个字体,大小和PDF中文字大小一致
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(new Font(""黑体"", 12f), true);
int i = 1;
while (y < height)
{
//绘制行号到每一行的前面
page.Canvas.DrawString(i.ToString(), trueTypeFont, PdfBrushes.Black, new PointF(15, y));
y += lineHeight + lineSpacing;
i++;
}
//保存文档
doc.SaveToFile(""result.pdf"");
}
}
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Texts
Imports System.Drawing
Namespace AddLineNumber
Class Program
Shared Sub Main(ByVal args() As String)
'创建PdfDocument对象 Dim doc As PdfDocument = New PdfDocument() '加载PDF文档
doc.LoadFromFile(""C:\Users\Administrator\Desktop\input.pdf"")
'获取第一页 Dim page As PdfPageBase = doc.Pages(0) '查找第一行中的指定文字
Dim finder As PdfTextFinder = New PdfTextFinder(page)
finder.Options.Parameter = TextFindParameter.WholeWord
Dim topLine As PdfTextFragment = finder.Find(""成都冰蓝科技"")(0)
'获取行高 Dim lineHeight As Single = topLine.Bounds(0).Height '获取一个y坐标,用于写入编号的起始y坐标
Dim y As Single = topLine.Bounds(0).Location.Y - 2
'获取第二行中的指定文字 Dim secondfinder As PdfTextFinder = New PdfTextFinder(page) secondfinder.Options.Parameter = TextFindParameter.WholeWord Dim secondLine As PdfTextFragment = secondfinder.Find(""Office控件产品"")(0) '计算行间距
Dim lineSpacing As Single = secondLine.Bounds(0).Top - topLine.Bounds(0).Bottom
'查找最后一行中的指定文字 Dim bottomfinder As PdfTextFinder = New PdfTextFinder(page) bottomfinder.Options.Parameter = TextFindParameter.WholeWord Dim bottomLine As PdfTextFragment = bottomfinder.Find(""开发并向他们"")(0) '获取获取最后一行的底部y坐标,亦即添加行号区域的高度
Dim height As Single = bottomLine.Bounds(0).Bottom
'创建一个字体,大小和PDF中文字大小一致 Dim trueTypeFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(""黑体"",12.0F),True) Dim i As Integer = 1 While y < height '绘制行号到每一行的前面
page.Canvas.DrawString(i.ToString(), trueTypeFont, PdfBrushes.Black, New PointF(15, y))
y += lineHeight + lineSpacing
i += 1
End While
'保存文档 doc.SaveToFile(""result.pdf"") End Sub End Class End Namespace结果文档:








