轉帖|使用教程|編輯:李顯亮|2020-05-26 10:26:12.710|閱讀 199 次
概述:Spire.Cloud.PDF.SDK提供了接口PdfTextApi及PdfImagesApi用于添加文本和圖片到PDF文檔,本文就將問你講解如何實現這一功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
你在尋找一款既能在線編輯office文件,又能實現與web應用程序對接的軟件嗎?好巧,冰藍公司最新推出Spire.Cloud,搭載了基于云端的Office在線編輯器和WEB API開發接口,既能安全穩定地實現WEB網頁端在線查看、編輯Office文檔;又能在服務器端通過代碼調用接口簡單高效地實現讀寫Office文檔內容。
Spire.Cloud.PDF.SDK提供了接口PdfTextApi及PdfImagesApi用于添加文本和圖片到PDF文檔,添加文本時,可格式化文本樣式,包括文本字體類型、字號、字體樣式、文本顏色、字符間距、行距、首行縮進、文本對齊方式、文本環繞方式等;添加圖片時,可格式化圖片,包括圖片位置、高度、寬度等。
Spire.Cloud提供了四種語言的SDK(包括.NET、Java、python、PHP),你可以點擊下載Spire.Cloud Web SDK。
本文將通過C#代碼演示如何實現以上內容操作。
步驟1:dll文件獲取及引用。下載獲取Spire.Cloud.PDF.SDK package,并將Spire.Cloud.PDF.Sdk.dll及其依賴項的dll添加引用至程序(如下圖)
步驟2:ID及Key獲取。在冰藍云網頁注冊賬號并登陸,在“我的應用”板塊創建應用程序,獲得 App ID 及 App Key。
步驟3:在“文檔管理”上傳源文檔。這里可以建文件夾,將文檔存放在文件夾下。不建文件夾時,源文檔及結果文檔直接保存在根目錄。 本文示例中,建了兩個文件夾,分別用于存放源文檔及結果文檔。(云平臺提供免費1 萬次調用次數和 2G 文檔內存)
示例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); } } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn