Spire.Cloud.PDF.SDK提供了接口PdfTextApi及PdfImagesApi用於添加文本和圖片到PDF文檔,添加文本時,可格式化文本樣式,包括文本字體類型、字號、字體樣式、文本顏色、字符間距、行距、首行縮進、文本對齊方式、文本環繞方式等;添加圖片時,可格式化圖片,包括圖片位置、高度、寬度等。本文將經過C#代碼演示如何實現以上內容操做。php
步驟一:dll文件獲取及導入html
方法1. 經過官網本地下載SDK文件包。(須在e-iceblue中國官網在線編輯板塊中註冊帳號並登陸)web
下載後,解壓文件,將Spire.Cloud.Pdf.Sdk.dll文件及其餘三個dll添加引用至VS程序; api
方法2. 在程序中經過Nuget搜索下載,直接導入全部dll。app
導入效果以下如所示:工具
步驟二:App ID及Key獲取。在「個人應用」板塊中建立應用以得到App ID及App Key。字體
步驟三:源文檔上傳。在「文檔管理」板塊,上傳源文檔。這裏能夠建文件夾,將文檔存放在文件夾下。不建文件夾時,源文檔及結果文檔直接保存在根目錄。本文示例中,建了兩個文件夾,分別用於存放源文檔及結果文檔。(雲平臺提供免費1 萬次調用次數和 2G 文檔內存)spa
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); } } }
文本添加效果:code
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); } } }
圖片添加效果:htm
(本文完)