可移植文檔格式 (PDF) 是 Adobe 制定的獨立規(guī)范的固定版式文檔。它封裝了完整的描述,包括文本字體、圖形和顯示它所需的其他信息。
Spire.PDF for .NET?是一款獨立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發(fā)人員可以新建一個 PDF 文檔或者對現(xiàn)有的 PDF 文檔進(jìn)行處理,且無需安裝 Adobe Acrobat。
E-iceblue?功能類庫Spire 系列文檔處理組件均由中國本土團(tuán)隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載? ?Spire.PDF for java下載
動態(tài)創(chuàng)建PDF并將其發(fā)送到客戶端瀏覽器
要動態(tài)生成 PDF 文件然后將其發(fā)送到客戶端瀏覽器,您可以使用Spire.PDF for .NET來完成此任務(wù)。此外,Spire.PDF還支持加載現(xiàn)有的PDF文件并將其發(fā)送到客戶端瀏覽器。在這篇技術(shù)文章中,我們將結(jié)合這兩個功能來完整描述 Spire.PDF 的工作原理。下面是兩個任務(wù):
- 任務(wù)1 動態(tài)創(chuàng)建PDF并將其發(fā)送到客戶端瀏覽器。
- 任務(wù)2 加載現(xiàn)有的PDF文件并將其發(fā)送到客戶端瀏覽器(這是Spire.PDF for .NET的附加功能)
首先創(chuàng)建一個Asp.net應(yīng)用程序并添加Spire.PDF.dll程序集。您可以在VS中的Aspx頁面上添加兩個按鈕。指定其中一名負(fù)責(zé)任務(wù) 1,另一名負(fù)責(zé)任務(wù) 2。
對于任務(wù)1,首先需要啟動一個Spire.PdfDocument對象
[C#]
PdfDocument doc = new PdfDocument();
并在這個新的 PDF 文檔中添加一個新頁面
[C#]
PdfPageBase page = newDoc.Pages.Add();
注意在該pdf頁面上繪制字符串時需要相關(guān)的輔助對象。
[C#]
string message = "Hello world!"; PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f); PdfBrush brush = PdfBrushes.Red; PointF location = new PointF(20, 20);
然后你可以在pdf頁面中繪制一個字符串,如下所示:
[C#]
page.Canvas.DrawString(message, font, brush, location);
最后您可以在客戶端瀏覽器中打開這個新生成的PDF文檔:
[C#]
newDoc.SaveToHttpResponse("sample.pdf",HttpContext.Current.Response, HttpReadType.Open);
對于任務(wù)2,3行代碼就可以直接解決。
啟動 Spire.PdfDocument 對象
[C#]
pdfDocument doc = new PdfDocument();
加載 pdf 文件
[C#]
doc.LoadFromFile(this.Server.MapPath("/sample.pdf"));
加載pdf文檔,然后將其作為附件發(fā)送到客戶端瀏覽器。
[C#]
doc.SaveToHttpResponse("sample.pdf", this.Response, HttpReadType.Save);
綜上所述,以下是這兩個任務(wù)所需的完整代碼片段:
[C#]
using System;
using System; using System.Collections.Generic; using System.Drawing; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Spire.Pdf; using Spire.Pdf.Graphics; namespace SendPdfToWebBrowser { public partial class WebForm_SendPdf : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } // load a pdf document ,after that ,send it to client browser as an attachment protected void btnClientSavePdf_Click(object sender,EventArgs e) { // initiated an object of Spire.PdfDocument PdfDocument doc = new PdfDocument(); // Load a pdf file doc.LoadFromFile(this.Server.MapPath("/sample.pdf")); // send the pdf document to client browser as an attachment doc.SaveToHttpResponse("sample.pdf",this.Response, HttpReadType.Save); } // Create an pdf document ,then open it in the client browser protected void btnClientOpenPdf_Click(object sender, EventArgs e) { // Initiate an object of Spire.PdfDocument PdfDocument newDoc = new PdfDocument(); // Add a new page in this newly created pdf file PdfPageBase page = newDoc.Pages.Add(); string message = "Hello world!” ; PdfFont font = new PdfFont(PdfFontFamily.Helvetica,13f); PdfBrush brush = PdfBrushes.Red; PointF location = new PointF(20, 20); // Draw a string with designated brush, a font, position in pdf page page.Canvas.DrawString(message, font, brush, location); //To open this pdf document in client browser. newDoc.SaveToHttpResponse("sample.pdf",HttpContext.Current.Response, HttpReadType.Open); } } }
最后,你可以運行它,并得到如下結(jié)果:
動態(tài)創(chuàng)建 PDF 并將其發(fā)送到客戶端瀏覽器的屏幕截圖
加載現(xiàn)有 PDF 文件并將其發(fā)送到客戶端瀏覽器的屏幕截圖
文章來源:http://www.zghlxwxcb.cn/news/detail-858047.html
以上便是如何態(tài)創(chuàng)建 PDF 并將其發(fā)送到客戶端瀏覽器,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程~文章來源地址http://www.zghlxwxcb.cn/news/detail-858047.html
到了這里,關(guān)于Spire.PDF for .NET【文檔操作】演示:動態(tài)創(chuàng)建 PDF 并將其發(fā)送到客戶端瀏覽器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!