当前位置: 首页 > article >正文

Spire.PDF for .NET【文档操作】演示:创建 PDF 文档

通过代码创建 PDF 文档具有多种优势。例如,您可以轻松合并动态内容,如用户输入、数据库记录或实时数据。基于代码的 PDF 生成允许更大的自定义和自动化,最大限度地减少创建高度定制文档时的手动干预。在本文中,您将学习如何使用Spire.PDF for .NET在 C# 和 VB.NET 中从头创建 PDF 文档。

Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。

E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式

Spire.PDF for.net下载icon-default.png?t=N7T8https://work.weixin.qq.com/ca/cawcde0267a7a4be49  

安装 Spire.PDF for .NET

首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。 以通过NuGet安装。

PM> Install-Package Spire.PDF
背景知识

Spire.PDF 中的一个页面(用PdfPageBase表示)由客户区和四周的边距组成。内容区用于用户书写各种内容,边距通常是空白的边缘。

如下图所示,页面上的坐标系原点位于客户区的左上角,x轴向右水平延伸,y轴向下垂直延伸。所有添加到客户区的元素都必须以指定的坐标为基准。

C#/VB.NET:创建 PDF 文档

此外,下表列出了重要的类和方法,可以帮助您轻松理解下一节提供的代码片段。

 成员描述
 PdfDocument 类表示 PDF 文档模型。
 PdfPageBase 类代表 PDF 文档中的一页。
 PdfSolidBrush 类表示使用纯色填充任何对象的画笔。
PdfTrueTypeFont 类代表 True Type 字体。
PdfStringFormat 类表示文本格式信息,例如对齐方式、字符间距和缩进。
PdfTextWidget 类表示具有跨越多页能力的文本区域。
PdfTextLayout 类表示文本布局信息。
PdfDocument.Pages.Add() 方法向 PDF 文档添加页面。
PdfPageBase.Canvas.DrawString() 方法使用指定的字体和画笔对象在页面上的指定位置绘制字符串。
PdfTextWidget.Draw() 方法在页面上的指定位置绘制文本小部件。
PdfDocument.Save() 方法将文档保存为 PDF 文件。

使用 C# 和 VB.NET 从头创建 PDF 文档

虽然 Spire.PDF for .NET 支持向 PDF 文档添加各种元素,但本文仅演示如何创建纯文本的 PDF 文档。以下是详细步骤。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.Pages.Add()方法添加页面。
  • 创建画笔和字体对象。
  • 使用PdfPageBase.Canvas.DrawString()方法在页面上的指定坐标处绘制字符串。
  • 创建一个PdfTextWidget对象来保存一大块文本。
  • 使用PdfTextWidget.Draw()方法在页面上的指定位置绘制文本小部件
  • 使用PdfDocument.Save()方法将文档保存为 PDF 文件。

【C#】

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace CreatePdfDocument
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Add a page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(35f));

//Specify heading text
String titleText = "What is MySQL";

//Create solid brushes
PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
PdfSolidBrush paraBrush = new PdfSolidBrush(new PdfRGBColor(Color.Black));

//Create true type fonts
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Times New Roman", 18f, FontStyle.Bold),true);
PdfTrueTypeFont paraFont = new PdfTrueTypeFont(new Font("Times New Roman", 12f, FontStyle.Regular), true);

//Set the text alignment via PdfStringFormat class
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;

//Draw heading on the center of the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 20, format);

//Get paragraph content from a .txt file
string paraText = File.ReadAllText("C:\\Users\\Administrator\\Desktop\\content.txt");

//Create a PdfTextWidget object to hold the paragrah content
PdfTextWidget widget = new PdfTextWidget(paraText, paraFont, paraBrush);

//Create a rectangle where the paragraph content will be placed
RectangleF rect = new RectangleF(0, 50, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height);

//Set the PdfLayoutType to Paginate to make the content paginated automatically
PdfTextLayout layout = new PdfTextLayout();
layout.Layout = PdfLayoutType.Paginate;

//Draw the widget on the page
widget.Draw(page, rect, layout);

//Save to file
doc.SaveToFile("CreatePdfDocument.pdf");
doc.Dispose();
}
}
}

【VB.NET】

Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace CreatePdfDocument
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Add a page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4,New PdfMargins(35f))

'Specify heading text
Dim titleText As String = "What is MySQL"

'Create solid brushes
Dim titleBrush As PdfSolidBrush = New PdfSolidBrush(New PdfRGBColor(Color.Blue))
Dim paraBrush As PdfSolidBrush = New PdfSolidBrush(New PdfRGBColor(Color.Black))

'Create true type fonts
Dim titleFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Times New Roman",18f,FontStyle.Bold),True)
Dim paraFont As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Times New Roman",12f,FontStyle.Regular),True)

'Set the text alignment via PdfStringFormat class
Dim format As PdfStringFormat = New PdfStringFormat()
format.Alignment = PdfTextAlignment.Center

'Draw heading on the center of the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 20, format)

'Get paragraph content from a .txt file
Dim paraText As String = File.ReadAllText("C:\\Users\\Administrator\\Desktop\\content.txt")

'Create a PdfTextWidget object to hold the paragrah content
Dim widget As PdfTextWidget = New PdfTextWidget(paraText,paraFont,paraBrush)

'Create a rectangle where the paragraph content will be placed
Dim rect As RectangleF = New RectangleF(0,50,page.Canvas.ClientSize.Width,page.Canvas.ClientSize.Height)

'Set the PdfLayoutType to Paginate to make the content paginated automatically
Dim layout As PdfTextLayout = New PdfTextLayout()
lay.Layout = PdfLayoutType.Paginate

'Draw the widget on the page
widget.Draw(page, rect, layout)

'Save to file
doc.SaveToFile("CreatePdfDocument.pdf")
doc.Dispose()
End Sub
End Class
End Namespace

C#/VB.NET:创建 PDF 文档


http://www.kler.cn/news/289731.html

相关文章:

  • python-实战4拆分pdf文件
  • 小土堆pytorch
  • CSS实现水滴效果图
  • 【Linux】进程间的关系(第十三篇)
  • oracle日期加减方式
  • 【区块链 + 物联网】智慧路灯计费和融资区块链解决方案 | FISCO BCOS应用案例
  • H265视频转换H264视频对应m3u8格式地址播放完整案例
  • IP地址查询功能详解—构建风险画像与代理识别
  • 传统CV算法——特征匹配算法
  • 创建MySQL数据库和相应表
  • C#复习之封装_静态成员
  • 「数组」计数排序|桶排序|基数排序(C++)
  • Android的Launch
  • 花10秒进来学学吧!用AI画朵云,点赞也能10万+
  • 深度学习速通系列:鲁棒性和稳定性
  • 二手手机回收小程序搭建,小程序功能特点
  • 力扣9.2
  • iscntrl函数讲解 <ctype.h>头文件函数
  • 运动耳机怎么选购?解密最值得购买的五大品牌!
  • 【算法专场】模拟(上)
  • CAAC执照无人机实训室建设技术详解
  • Nuxt3入门:样式的注入、定义和使用(第3节)
  • AAA原理与配置
  • 嵌入式24千兆电口+4万兆光口管理型三层交换机RTL9301模块
  • 惠中科技智能高效综合光伏清洗技术
  • mysql优化案例分享
  • opencv之几何变换
  • Docker 的安全优化
  • 巴西电商市场消费需求仍坚挺,商机还无限吗?卖家必知的巴西电商平台有哪些?
  • CSS动画(animation)事例