-
C# 添加文本、图片到PDF文档(基于Spire.Cloud.PDF.SDK)
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
Spire.Cloud.PDF.SDK提供了接口PdfTextApi及PdfImagesApi用于添加文本和图片到PDF文档,添加文本时,可格式化文本样式,包括文本字体类型、字号、字体样式、文本颜色、字符间距、行距、首行缩进、文本对齐方式、文本环绕方式等;添加图片时,可格式化图片,包括图片位置、高度、宽度等。本文将通过C#代码演示如何实现以上内容操作。
使用工具:
- Spire.Cloud.PDF.SDK
- Visual Studio
必要步骤:
步骤一:dll文件获取及导入
方法1. 通过官网本地下载SDK文件包。(须在e-iceblue中国官网在线编辑板块中注册账号并登录)
下载后,解压文件,将Spire.Cloud.Pdf.Sdk.dll文件及其他三个dll添加引用至VS程序;
方法2. 在程序中通过Nuget搜索下载,直接导入所有dll。
导入效果如下如所示:
步骤二:App ID及Key获取。在“我的应用”板块中创建应用以获得App ID及App Key。
步骤三:源文档上传。在“文档管理”板块,上传源文档。这里可以建文件夹,将文档存放在文件夹下。不建文件夹时,源文档及结果文档直接保存在根目录。本文示例中,建了两个文件夹,分别用于存放源文档及结果文档。(云平台提供免费1 万次调用次数和 2G 文档内存)
C# 代码示例
1. 添加文本到PDF
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 = "App ID"; static String appKey = "App Key"; 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. 添加图片到PDF
using Spire.Cloud.Pdf.Sdk.Api; using Spire.Cloud.Pdf.Sdk.Client; using System; using System.IO; namespace AddImg_Cloud.PDF { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置账号信息 Configuration PdfConfiguration = new Configuration(appId, appKey); PdfImagesApi pdfImagesApi = new PdfImagesApi(PdfConfiguration); string name = "sample.pdf";//源文档 string outPath = "output/AddImg.pdf";//结果文档路径 int pageNumber = 2;//指定图片所在文档页码 string folder = "input";//源文档所在文件夹 string password = null;//源文档密码 System.IO.Stream file = new FileStream("logo.png", FileMode.Open);//打开图片 //指定图片位置及大小 float x = 50; float y = 320; float width = 200; float height = 200; //调用方法添加图片 pdfImagesApi.AddImage(name, outPath, pageNumber, file, x, y, width, height, folder, password); } } }
图片添加效果:
栏目列表
最新更新
python数据库连接池技术总结
成人网站性能提升 20 倍之经验谈 [Python
python动态捕获异常
python 探测网站目录的GUI程序
python实现中文字符繁体和简体中文转换
Python服务器开发 -- 网络基础
python高性能编程方法一
使用python管理Cisco设备
python抓取google搜索结果
Python 自动备份SVN版本库并复制到远程主机
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
图解TCP数据报结构以及三次握手(非常详
TCP协议的粘包问题(数据的无边界性)
Excel数据导入到Sql server
SQL Server like 字段
SQL Server中的LEFT、RIGHT函数
sql server 安装出现需要sqlncli.msi文件,错误
SQL Server学习内容(一)
SQLServer执行大脚本文件时,提示“无法执
数据库敏捷版本控制之3个数据库策略
将select 转为json
SQL Server 创建索引(index)
GROUP BY中的WITH CUBE、WITH ROLLUP原理测试及