C# 添加文本到PDF(基于Spire.Cloud.PDF)

2025-10-06 01:52:30

1、步骤一:dll文件获取及导入

       方法1. 通过官网本地下载SDK文件包。(须在e-iceblue中国官网在线编辑板块中账号注册并登录)

下载后,解压文件,将Spire.Cloud.Pdf.Sdk.dll文件及其他三个dll添加引用至VS程序;

       方法2. 在程序中通过Nuget搜索下载,直接导入所有dll。

导入效果如下如所示:

C# 添加文本到PDF(基于Spire.Cloud.PDF)

C# 添加文本到PDF(基于Spire.Cloud.PDF)

2、步骤二:App ID及Key获取。

在“我的应用”板块中创建应用以获得App ID及App Key。

C# 添加文本到PDF(基于Spire.Cloud.PDF)

3、步骤三:源文档上传。

在“文档管理”板块,上传源文档。这里可以建文件夹,将文档存放在文件夹下。不建文件夹时,源文档及结果文档直接保存在根目录。本文示例中,建了两个文件夹,分别用于存放源文档及结果文档。(云平台提供免费1 万次调用次数和 2G 文档内存)

C# 添加文本到PDF(基于Spire.Cloud.PDF)

1、using System;

using Spire.Cloud.Pdf.Sdk.Client;

using Spire.Cloud.Pdf.Sdk.Api;

using Spire.Cloud.Pdf.Sdk.Model;

namespace AddText_Cloud.PDF

{

    class Program

    {

        static String appId = "e64a28638d9546292a6fcce77b2680b3";

        static String appKey = "h1bhiARNuzIDGGa9Vo6gMUOvit9UbN3N";

        static void Main(string[] args)

        {

            //配置账号信息

            Configuration PdfConfiguration = new Configuration(appId, appKey);

            PdfTextApi PdfTextApi = new PdfTextApi(PdfConfiguration);

            string name = "sample.pdf";//源文档

            string outPath = "output/AddText.pdf";//结果文档路径

            int pageNumber = 2;//指定文本内容所在页数

            string folder = "input";//源文档所在文件夹

            Spire.Cloud.Pdf.Sdk.Model.Text text = new Spire.Cloud.Pdf.Sdk.Model.Text("This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.",

                new Font(Font.FontTypeEnum.TrueType, "Arial", 13, Font.FontStyleEnum.Regular),

                new RectangleF(50, 320, 500, 200));//实例化文本信息(文本内容、字体类型、字号、字体样式、文本位置)

            text.BackgroundColor = new Color(255, 244, 164, 96);//设置文本背景色

            text.ForegroundColor = new Color(255, 135, 206, 235);//设置文本前景色

            text.CharSpacing = 5;//字符间距

            text.FirstLineIndent = 100;//首行缩进

            text.LineSpacing = 15;//行距

            text.HorizontalAlignment = Spire.Cloud.Pdf.Sdk.Model.Text.HorizontalAlignmentEnum.Left;//文本水平对齐方式    

            text.VerticalAlignment = Spire.Cloud.Pdf.Sdk.Model.Text.VerticalAlignmentEnum.Middle;//文本垂直对齐方式  

            text.WordSpacing = 12;//单词间距

            text.WordWrap = Spire.Cloud.Pdf.Sdk.Model.Text.WordWrapEnum.Character;//文本环绕方式

            //调用方法添加文本

            PdfTextApi.AddText(name, outPath, pageNumber, text, folder, null);

        }

    }

}

2、文本添加效果:

C# 添加文本到PDF(基于Spire.Cloud.PDF)

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢