冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

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

原文档:

C#/VB.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

结果文档:

C#/VB.NET 为 PDF 添加行号

Spire.Doc 9.1已发布。该版本增强了转换Word到PDF的功能,修复了查找文本、加载和保存文档时出现的问题。详情请阅读以下内容。

问题修复:


获取Spire.Doc 9.1,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html

Spire.Presentation for .NET 6.1版本支持嵌入Zip对象到PPT幻灯片。下面通过C#及VB.NET代码展示如何来实现Zip对象添加。在PPT中嵌入Excel对象可参考这篇文章

C#
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Drawing;
using System.IO;

namespace InsertZip
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个PPT文档,并获取第一张幻灯片(创建的PPT默认包含一张空白幻灯片)
            Presentation ppt = new Presentation();
            ISlide slide = ppt.Slides[0];

            //读取zip文件到byte数组
            byte[] data = File.ReadAllBytes("sample.zip");

            //在幻灯片中绘制形状,并添加zip文件对象
            Rectangle rec = new Rectangle(120, 120, 100, 100);             
            IOleObject ole = slide.Shapes.AppendOleObject("sample.zip", data, rec);
            ole.ProgId = "Package";

            //设置对象封面图片
            Image image = Image.FromFile("logo.png");
            IImageData oleImage = ppt.Images.Append(image);
            ole.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;

            //保存文档
            ppt.SaveToFile("InsertZip.pptx", Spire.Presentation.FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("InsertZip.pptx");
        }
    }
}
VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing
Imports System.IO

Namespace InsertZip
	Class Program
		Private Shared Sub Main(args As String())
			'创建一个PPT文档,并获取第一张幻灯片(创建的PPT默认包含一张空白幻灯片)
			Dim ppt As New Presentation()
			Dim slide As ISlide = ppt.Slides(0)

			'读取zip文件到byte数组
			Dim data As Byte() = File.ReadAllBytes("sample.zip")

			'在幻灯片中绘制形状,并添加zip文件对象
			Dim rec As New Rectangle(120, 120, 100, 100)
			Dim ole As IOleObject = slide.Shapes.AppendOleObject("sample.zip", data, rec)
			ole.ProgId = "Package"

			'设置对象封面图片
			Dim image__1 As Image = Image.FromFile("logo.png")
			Dim oleImage As IImageData = ppt.Images.Append(image__1)
			ole.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage

			'保存文档
			ppt.SaveToFile("InsertZip.pptx", Spire.Presentation.FileFormat.Pptx2013)
			System.Diagnostics.Process.Start("InsertZip.pptx")
		End Sub
	End Class
End Namespace

嵌入效果:

C#/VB.NET 在 PPT 中嵌入 Zip 对象

Spire.PDF for Java 4.1.2现已发布。该版本主要修复了一些在PDF转HTML/图片和插入图片到PDF Grid单元格时出现的问题。详情如下。

问题修复:


获取Spire.PDF for Java 4.1.2,请点击:

https://www.e-iceblue.cn/Downloads/Spire-PDF-JAVA.html

Spire.PDF 7.1 已正式发布。该版本调整了pdftable.Style.DefaultStyle和pdftable.Style.AlternateStyle的属性注释,并修复了在压缩、打印、合并以及对PDF文档签名时出现的一些问题。详情如下。

改进:

问题修复:


获取Spire.PDF 7.1,请点击:

https://www.e-iceblue.cn/Downloads/Spire-PDF-NET.html

Spire.Barcode for Java 4.1.2现已发布。该版本成功修复了扫描QR code二维码的数据不正确的问题以及在Linux系统下生成的Code128条形码数据显示不完全的问题。详情请查阅以下内容。

问题修复:


获取Spire.Barcode for Java 4.1.2请点击:

https://www.e-iceblue.cn/Downloads/Spire-Barcode-JAVA.html

Spire.Email 4.1已发布。本次调整主要将ImapClient.MarkAsDeleted(int sequqnceNo)方法的参数类型由String改为int。详情请阅读以下内容。

调整变化:


获取Spire.Email 4.1,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Email-NET.html

Spire.Barcode 5.1已发布。该版本主要增强了扫描功能。详情请阅读以下内容。

问题修复:


获取Spire.Barcode 5.1请点击:

https://www.e-iceblue.cn/Downloads/Spire-Barcode-NET.html

Spire.PDFViewer 6.1已发布。该版本支持了显示页面标签的功能并修复了一些显示问题。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.PDFViewer 6.1,请点击:

https://www.e-iceblue.cn/Downloads/Spire-PDFViewer-NET.html

Spire.Presentation 6.1已发布。该本支持嵌入zip对象到PPTX文件;支持加载和打印包含数学公式的PPT/PPTX文档以及转换到其他文档格式。此外,该版本还修复了复制组合图形时出现的问题。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.Presentation 6.1请点击:

https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html