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