翻譯|行業(yè)資訊|編輯:胡濤|2024-04-19 11:05:25.580|閱讀 113 次
概述:本文將向您介紹如何在spire.pdf中動(dòng)態(tài)創(chuàng)建 PDF 并將其發(fā)送到客戶端瀏覽器,歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
可移植文檔格式 (PDF) 是 Adobe 制定的獨(dú)立規(guī)范的固定版式文檔。它封裝了完整的描述,包括文本字體、圖形和顯示它所需的其他信息。
Spire.PDF for .NET 是一款獨(dú)立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類(lèi)庫(kù),開(kāi)發(fā)人員可以新建一個(gè) PDF 文檔或者對(duì)現(xiàn)有的 PDF 文檔進(jìn)行處理,且無(wú)需安裝 Adobe Acrobat。
E-iceblue 功能類(lèi)庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
要?jiǎng)討B(tài)生成 PDF 文件然后將其發(fā)送到客戶端瀏覽器,您可以使用Spire.PDF for .NET來(lái)完成此任務(wù)。此外,Spire.PDF還支持加載現(xiàn)有的PDF文件并將其發(fā)送到客戶端瀏覽器。在這篇技術(shù)文章中,我們將結(jié)合這兩個(gè)功能來(lái)完整描述 Spire.PDF 的工作原理。下面是兩個(gè)任務(wù):
首先創(chuàng)建一個(gè)Asp.net應(yīng)用程序并添加Spire.PDF.dll程序集。您可以在VS中的Aspx頁(yè)面上添加兩個(gè)按鈕。指定其中一名負(fù)責(zé)任務(wù) 1,另一名負(fù)責(zé)任務(wù) 2。
對(duì)于任務(wù)1,首先需要啟動(dòng)一個(gè)Spire.PdfDocument對(duì)象
[C#]
PdfDocument doc = new PdfDocument();
并在這個(gè)新的 PDF 文檔中添加一個(gè)新頁(yè)面
[C#]
PdfPageBase page = newDoc.Pages.Add();
注意在該pdf頁(yè)面上繪制字符串時(shí)需要相關(guān)的輔助對(duì)象。
[C#]
string message = "Hello world!"; PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13f); PdfBrush brush = PdfBrushes.Red; PointF location = new PointF(20, 20);
然后你可以在pdf頁(yè)面中繪制一個(gè)字符串,如下所示:
[C#]
page.Canvas.DrawString(message, font, brush, location);
最后您可以在客戶端瀏覽器中打開(kāi)這個(gè)新生成的PDF文檔:
[C#]
newDoc.SaveToHttpResponse("sample.pdf",HttpContext.Current.Response, HttpReadType.Open);
對(duì)于任務(wù)2,3行代碼就可以直接解決。
啟動(dòng) Spire.PdfDocument 對(duì)象
[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);
綜上所述,以下是這兩個(gè)任務(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); } } }
最后,你可以運(yùn)行它,并得到如下結(jié)果:
動(dòng)態(tài)創(chuàng)建 PDF 并將其發(fā)送到客戶端瀏覽器的屏幕截圖
加載現(xiàn)有 PDF 文件并將其發(fā)送到客戶端瀏覽器的屏幕截圖
以上便是如何態(tài)創(chuàng)建 PDF 并將其發(fā)送到客戶端瀏覽器,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn