Spire.Cloud.SDK for .NET提供了介面PdfTextApi及PdfImagesApi用于添加文本和圖片到PDF檔案,添加文本時,可格式化文本樣式,包括文本字體型別、字號、字體樣式、文本顏色、字符間距、行距、首行縮進、文本對齊方式、文本環繞方式等;添加圖片時,可格式化圖片,包括圖片位置、高度、寬度等,本文將通過C#代碼演示如何實作以上內容操作,
使用工具:
- Spire.Cloud.SDK for .NET
- Visual Studio
必要步驟:
步驟一:dll檔案獲取及匯入,在VS程式中通過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 { //配置AppID和AppKey static string appId = "App ID"; static string appKey = "App Key"; static string basePath = "https://api.e-iceblue.cn"; static Configuration Configuration = new Configuration(appId, appKey, basePath); static PdfTextApi textApi = new PdfTextApi(Configuration); static void Main(string[] args) { string name = "sample.pdf";//源檔案 string destFilePath = "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;//文本環繞方式 //呼叫方法添加文本 textApi.AddText(name, destFilePath, 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 { //配置AppID和AppKey static string appId = "App ID"; static string appKey = "App Key"; static string basePath = "https://api.e-iceblue.cn"; static Configuration Configuration = new Configuration(appId, appKey, basePath); static PdfImagesApi imageApi = new PdfImagesApi(Configuration); static void Main(string[] args) { string name = "sample.pdf";//源檔案 string destFilePath = "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; //呼叫方法添加圖片 imageApi.AddImage(name, destFilePath, pageNumber, file, x, y, width, height, folder, password); } } }
圖片添加效果:

(本文完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/83009.html
標籤:C#
