使用 ASP.NET 動態創建 PDF 并將其發送至客戶端瀏覽器
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發人員輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
關于 PDF便攜式文檔格式(PDF)是由 Adobe 公司獨立規范的一種固定版面文檔。它封裝了完整的描述,包括文本字體、圖形和其他顯示所需的信息。
動態創建 PDF 并將其發送到客戶端瀏覽器
要動態生成 PDF 文件并將其發送到客戶端瀏覽器,可以使用 Spire.PDF for .NET 來完成這項任務。此外,Spire.PDF 還支持加載現有 PDF 文件并將
發送到客戶端瀏覽器。在這篇技術文章中,我們將結合這兩個功能,全面介紹 Spire.PDF 是如何工作的。以下是這兩項任務:
- 任務 1 動態創建 PDF 并將其發送到客戶端瀏覽器。
- 任務 2 加載現有 PDF 文件并將其發送到客戶端瀏覽器(這是 Spire.PDF for .NET 的附加功能)
首先創建一個 Asp.net 應用程序并添加 Spire.PDF.dll 程序集。您可以在 VS 的 Aspx 頁面上添加兩個按鈕。將其中一個指定為任務 1,另一個指定為任務 2。
	 
 
	在任務 1 中,首先需要啟動 Spire.PdfDocument 對象
 [C#] 
PdfDocument doc = new PdfDocument();并在此新 PDF 文檔中添加新頁面
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#]
C#
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 文件并將其發送至客戶端瀏覽器的截圖:
 
 

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線