>>Spire.PDF更新至最新版v7.1,欢迎下载体验
在C#,VB.NET中将行 添加到PDF
下面是输入文档的屏幕截图。

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:360MoveDataUsersAdministratorDesktopsample.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.PdfImports Spire.Pdf.General.FindImports Spire.Pdf.GraphicsImports System.DrawingNamespace 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:360MoveDataUsersAdministratorDesktopsample.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 ClassEnd Namespace
是E-iceblue官方友好合作伙伴,如果您对spire.PDF感兴趣,可以联系在线客服了解具体授权价格和使用机制。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!