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 文档

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Free Spire.PDF for .NET 是一个用于.NET平台的免费PDF处理库。它提供了一套强大的功能,可以用于创建、编辑和处理PDF文档。无论是在Windows应用程序还是Web应用程序中,Free Spire.PDF for .NET 都能帮助我们轻松实现对PDF文件的操作。 首先,Free Spire.PDF for .NET 支持创建和编辑PDF文档。我们可以使用库中提供的API来创建新的PDF文档,添加页面、文本、图像和表格等内容,并设置页面的布局和样式。同时,我们还可以使用库中的功能来编辑现有的PDF文档,例如添加、提取和删除页面,修改文本、图像和表格等。 其次,Free Spire.PDF for .NET 还提供了丰富的PDF处理功能,可以帮助我们完成一些特定的操作。例如,我们可以使用库中的API来提取、合并和拆分PDF文档,将多个PDF文件合并为一个,或者将一个PDF文件拆分为多个部分。此外,库中还提供了加密和解密PDF文档的功能,可以帮助我们保护敏感的PDF文件内容。 除了基本的PDF处理功能之外,Free Spire.PDF for .NET 还支持高级功能,如转换PDF文档到其他格式,例如HTML、Word、Excel和图像等。我们可以使用库中的API将PDF文件转换为其他格式的文件,便于我们在不同的应用程序中使用。 总之,Free Spire.PDF for .NET 是一个功能强大、易于使用且免费的PDF处理库。它提供了一系列实用的API,可以帮助我们创建、编辑和处理PDF文档,完成各种PDF操作。无论是在个人项目中还是商业应用中,Free Spire.PDF for .NET 都是一个不错的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值