PDF管理控件Spire.PDF使用教程:在C#,VB.NET中将行号添加到PDF

Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。本文演示了如何使用Spire.PDF for .NET在PDF页面中的文本块之前添加行号。

Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API拥有丰富的功能,如安全设置(包括数字签名)、PDF文本/附件/图片提取、PDF文件合并/拆分、元数据更新、章节和段落优化、图形/图像描绘和插入、表格创建和处理、数据导入等等。

>>Spire.PDF更新至最新版v7.1,欢迎下载体验

在C#,VB.NET中将行号添加到PDF

本文演示了如何使用Spire.PDF for .NET在PDF页面中的文本块之前添加行号。

下面是输入文档的屏幕截图。

PDF管理控件Spire.PDF使用教程:在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进行处理,非常感谢!

上一篇 2021年1月9日
下一篇 2021年1月9日

相关推荐

发表回复

登录后才能评论