Spire.PDF for .NET【文档操作】演示:动态创建 PDF 并将其发送到客户端浏览器

可移植文档格式 (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并将其发送到客户端浏览器

要动态生成 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 文档中添加一个新页面

[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 并将其发送到客户端浏览器,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

相关推荐
久绊A1 小时前
Python 基本语法的详细解释
开发语言·windows·python
Hylan_J5 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
莫忘初心丶5 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
失败尽常态5238 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447748 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农8 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿8 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing8 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
xinxiyinhe9 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语10 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3