VB.NET PDF 创建器(代码示例教程)
本教程将逐步指导您如何在VB.NET中创建和编辑PDF文件。 这种技术同样适用于ASP.NET web 应用、控制台应用程序、Windows 服务 和 桌面程序。 我们将使用VB.NET创建针对.NET Framework 4.6.2或.NET Core 2的PDF项目。您所需要的只是一个Visual Basic .NET开发环境,例如Microsoft Visual Studio Community。
要了解如何在C#中使用IronPDF,请参阅本指南。
要了解如何使用IronPDF与F#,请参阅本指南。
概述
如何在 VB .NET 库中生成 PDF 文件
- 下载 VB.NET PDF 库
- 使用 VB.NET 库创建 PDF 文档
- 自定义 PDF 文档样式
- 选择创建动态内容的方法
- 通过 VB.NET 库编辑 PDF 文件
使用 IronPDF 创建和编辑 PDF 的 VB .NET 代码
使用 VB.NET 将 HTML 渲染为 PDF,应用样式,利用动态内容,并轻松编辑您的文件。 创建PDFs简单且兼容.NET Framework 4.6.2、.NET Core 3.1、.NET 8、7、6和5。无需专有文件格式或调用不同的API。
本教程提供文档,引导您逐步完成每个任务,全部使用开发者钟爱的免费开发IronPDF软件。 VB.NET代码示例针对您的用例进行了特定设计,因此您可以在熟悉的环境中轻松查看步骤。 此VB .NET PDF库具有全面的创建和设置功能,适用于各种项目,无论是在ASP.NET应用程序、控制台还是桌面上。
包括在 IronPDF 中:
- Ticket support direct from our .NET PDF Library development team (real humans!)
- Works with HTML, ASPX forms, MVC views, images, and all the document formats you already use
- Microsoft Visual Studio installation gets you up and running fast
- Unlimited free development, and licenses to go live starting at $749
步骤 1
1. 从IronPDF免费下载VB .NET PDF库
立即在您的项目中开始使用IronPDF,并享受免费试用。
通过 NuGet 安装
在 Visual Studio 中,右键单击您的项目解决方案资源管理器,然后选择“管理 NuGet 包...”。 从那里开始,搜索IronPDF并安装最新版本... 点击确定以应对任何弹出的对话框。
这将适用于任何C# .NET Framework项目,从Framework 4.6.2及以上版本,或.NET Core 2及以上版本。 它在VB.NET项目中同样有效。
Install-Package IronPdf
https://www.nuget.org/packages/IronPdf
通过 DLL 安装
或者,可以从https://ironpdf.com/packages/IronPdf.zip下载IronPDF DLL,并手动安装到项目或GAC中。
请记得在使用IronPDF的任何vb类文件的开头添加此声明:
Imports IronPdf;
教程
2. 使用VB.NET创建PDF
使用Visual Basic ASP.NET首次创建 PDF 文件时,使用 IronPDF 比起使用像iTextSharp这样具有专有设计 API 的库要容易得多。
我们可以使用HTML(基于Google Chromium的像素完美渲染引擎)来定义PDF的内容,并简单地将其渲染到文件中。
这是我们在VB.NET中创建PDF的最简单代码:
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-1.cs
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim document = renderer.RenderHtmlAsPdf("<h1> My First PDF in VB.NET</h1>")
document.SaveAs("MyFirst.pdf")
End Sub
End Module
这将生成一个.NET生成的PDF文件,包含您的确切文本,尽管此时缺少一些设计。
我们可以通过添加标题行Imports IronPdf来改进此代码。
通过添加最后一行代码System.Diagnostics.Process.Start,我们在操作系统的默认PDF查看器中打开PDF,以使项目更具意义。
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-2.cs
Imports IronPdf
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim document = renderer.RenderHtmlAsPdf("<h1> My First PDF in VB.NET</h1>")
document.SaveAs("MyFirst.pdf")
System.Diagnostics.Process.Start("MyFirst.pdf")
End Sub
End Module
另一种方法是使用 IronPDF 中优雅的 "RenderUrlAsPdf "方法,将 URL 中的任何现有网页渲染为 PDF。
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-3.cs
Imports IronPdf
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim document = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/")
document.SaveAs("UrlToPdf.pdf")
System.Diagnostics.Process.Start("UrlToPdf.pdf")
End Sub
End Module
3. 为VB.NET PDF应用样式
要在 VB.NET 中为我们的PDF内容设置样式,我们可以充分利用 CSS、JavaScript 和图像。 我们可能会链接到本地资源,甚至远程或CDN基础的资源,如Google Fonts。 我们甚至可以使用DataURIs将图像和资产作为字符串嵌入到您的HTML中。
对于高级设计,我们可以使用两阶段过程:
首先,我们完美地开发和设计我们的HTML。 这项任务可能涉及内部设计团队,分担工作量。
使用 VB.NET 和我们的 PDF 库将该文件渲染为 PDF。
将HTML文件渲染为PDF的VB.NET代码:
此方法呈现 HTML 文档,就像打开文件(file:// 协议)一样。
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-4.cs
Imports IronPdf
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.CssMediaType = Rendering.PdfCssMediaType.Print
renderer.RenderingOptions.PrintHtmlBackgrounds = False
renderer.RenderingOptions.PaperOrientation = Rendering.PdfPaperOrientation.Landscape
renderer.RenderingOptions.WaitFor.RenderDelay(150)
Dim document = renderer.RenderHtmlFileAsPdf("C:\Users\jacob\Dropbox\Visual Studio\Tutorials\VB.Net.Pdf.Tutorial\VB.Net.Pdf.Tutorial\slideshow\index.html")
document.SaveAs("Html5.pdf")
System.Diagnostics.Process.Start("Html5.pdf")
End Sub
End Module
我们还可以通过添加项目相对文件路径来缩短 URL,例如
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-5.cs
Dim document = renderer.RenderHtmlFileAsPdf("..\..\slideshow\index.html")
您可以看到ChromePdfRenderer渲染器具有RenderingOptions属性,我们可以在此示例中使用它:
- 将CSS媒体类型设置为“print”,以便我们不看到仅限屏幕的CSS3样式。
- 忽略HTML背景
- 将PDF的虚拟纸张设置为横向布局
在渲染中添加一个小的延迟,以便JavaScript完成处理。
我们的示例 HTML 文件使用了 JavaScript、CSS3 和图像。 此HTML创建了一个动态的、移动设备感知的幻灯片展示,源地址为 https://leemark.github.io/better-simple-slideshow/
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-6.cs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>A simple DIY responsive slideshow made with HTML5, CSS3, and JavaScript</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Open+Sans|Open+Sans+Condensed:700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="demo/css/demostyles.css">
<link rel="stylesheet" href="css/simple-slideshow-styles.css">
</head>
<body>
<!--[if lt IE 8]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<header>
<h1>A Better Simple Slideshow</h1>
<p><span class="desc">A simple DIY responsive JavaScript slideshow.</span> [<a href="https://github.com/leemark/better-simple-slideshow">GitHub<span> repo</span></a>]</p>
</header>
<div class="bss-slides num1" tabindex="1" autofocus="autofocus">
<figure>
<img src="demo/img/medium.jpg" width="100%" /><figcaption>"Medium" by <a href="https://www.flickr.com/photos/thomashawk/14586158819/">Thomas Hawk</a>.</figcaption>
</figure>
<figure>
<img src="demo/img/colorado.jpg" width="100%" /><figcaption>"Colorado" by <a href="https://www.flickr.com/photos/stuckincustoms/88370744">Trey Ratcliff</a>.</figcaption>
</figure>
<figure>
<img src="demo/img/monte-vista.jpg" width="100%" /><figcaption>"Early Morning at the Monte Vista Wildlife Refuge, Colorado" by <a href="https://www.flickr.com/photos/davesoldano/8572429635">Dave Soldano</a>.</figcaption>
</figure>
<figure>
<img src="demo/img/sunrise.jpg" width="100%" /><figcaption>"Sunrise in Eastern Colorado" by <a href="https://www.flickr.com/photos/35528040@N04/6673031153">Pam Morris</a>.</figcaption>
</figure>
<figure>
<img src="demo/img/colorado-colors.jpg" width="100%" /><figcaption>"colorado colors" by <a href="https://www.flickr.com/photos/cptspock/2857543585">Jasen Miller</a>.</figcaption>
</figure>
</div> <!-- // bss-slides -->
<div class="content">
<h2>What is it?</h2>
<p>It's a fairly basic slideshow, written in javascript. This is a dual-purpose project, it's meant to be something you can drop right into your page and use if you so choose, but it's also meant as an example/tutorial script showing how to build a simple DIY slideshow from scratch on your own. <a href="http://themarklee.com/2014/10/05/better-simple-slideshow/">Here is a tutorial/walkthrough</a>.</p>
<h2>Features</h2>
<ul>
<li>fully responsive</li>
<li>option for auto-advancing slides, or manually advancing by user</li>
<li>multiple slideshows per-page</li>
<li>supports arrow-key navigation</li>
<li>full-screen toggle using HTML5 fullscreen api</li>
<li>swipe events supported on touch devices (requires <a href="https://github.com/hammerjs/hammer.js">hammer.js</a>)</li>
<li>written in vanilla JS--this means no jQuery dependency (much ♥ for <a href="https://github.com/jquery/jquery">jQuery</a> though!)</li>
</ul>
<h2>Getting Started</h2>
<ol>
<li><p>HTML markup for the slideshow should look basically like this, with a container element wrapping the whole thing (doesn't have to be a <span class="code"><div></span>) and each slide is a <span class="code"><figure></span>.</p>
<script src="https://gist.github.com/leemark/83571d9f8f0e3ad853a8.js"></script> </li>
<li>Include the script: <span class="code">js/better-simple-slideshow.min.js</span> or <span class="code">js/better-simple-slideshow.js</span></li>
<li>Include the stylesheet <span class="code">css/simple-slideshow-styles.css</span></li>
<li>Initialize the slideshow:
<script src="https://gist.github.com/leemark/479d4ecc4df38fba500c.js"></script>
</li>
</ol>
<h2>Options</h2>
To customize functionality, create an options object, then pass it into <span class="code">makeBSS()</span> as the second argument, as seen below:
<script src="https://gist.github.com/leemark/c6e0f5c47acb7bf9be16.js"></script>
<h2>Demo/Examples</h2>
<h3>Example #1 (slideshow at top of this page)</h3>
<p>HTML markup:</p>
<script src="https://gist.github.com/leemark/19bafdb1abf8f6b4e147.js"></script>
<p>JavaScript code:</p>
<script src="https://gist.github.com/leemark/a09d2726b5bfc92ea68c.js"></script>
<h3>Example #2 (below)</h3>
<div class="bss-slides num2" tabindex="2">
<figure>
<img src="http://themarklee.com/wp-content/uploads/2013/12/snowying.jpg" width="100%" /><figcaption>"Snowying" by <a href="http://www.flickr.com/photos/fiddleoak/8511209344/">fiddleoak</a>.</figcaption>
</figure>
<figure>
<img src="http://themarklee.com/wp-content/uploads/2013/12/starlight.jpg" width="100%" /><figcaption>"Starlight" by <a href="http://www.flickr.com/photos/chaoticmind75/10738494123/in/set-72157626146319517">ChaoticMind75</a>.</figcaption>
</figure>
<figure>
<img src="http://themarklee.com/wp-content/uploads/2013/12/snowstorm.jpg" width="100%" /><figcaption>"Snowstorm" by <a href="http://www.flickr.com/photos/tylerbeaulawrence/8539457508/">Beaulawrence</a>.</figcaption>
</figure>
<figure>
<img src="http://themarklee.com/wp-content/uploads/2013/12/misty-winter-afternoon.jpg" width="100%" /><figcaption>"Misty winter afternoon" by <a href="http://www.flickr.com/photos/22746515@N02/5277611659/">Bert Kaufmann</a>.</figcaption>
</figure>
<figure>
<img src="http://themarklee.com/wp-content/uploads/2013/12/good-morning.jpg" width="100%" /><figcaption>"Good Morning!" by <a href="http://www.flickr.com/photos/frank_wuestefeld/4306107546/">Frank Wuestefeld</a>.</figcaption>
</figure>
</div> <!-- // bss-slides -->
<p>HTML markup:</p>
<script src="https://gist.github.com/leemark/de90c78cb73673650a5a.js"></script>
<p>JavaScript code:</p>
<script src="https://gist.github.com/leemark/046103061c89cdf07e4a.js"></script>
</div> <!-- // content -->
<footer>Example photos are property of their respective owners, all code is <a href="https://github.com/leemark/better-simple-slideshow/blob/gh-pages/LICENSE">freely licensed for your use</a>. <br>Made especially for you by <a href="http://themarklee.com">Mark Lee</a> aka <a href="http://twitter.com/@therealmarklee">@therealmarklee</a> <br><span>☮ + ♥</span></footer>
<script src="demo/js/hammer.min.js"></script><!-- for swipe support on touch interfaces -->
<script src="js/better-simple-slideshow.min.js"></script>
<script>
var opts = {
auto : {
speed : 3500,
pauseOnHover : true
},
fullScreen : false,
swipe : true
};
makeBSS('.num1', opts);
var opts2 = {
auto : false,
fullScreen : true,
swipe : true
};
makeBSS('.num2', opts2);
</script>
</body>
</html>
如您所见,此示例中使用了HTML网页的全部“厨房水槽”功能。 渲染是由IronPDF内部使用来自Google的Chromium HTML引擎和v8 JavaScript引擎执行的。 他们不需要安装在您的系统中,当您使用IronPDF时,整个包会自动添加到您的项目中。
3.1. 添加页眉和页脚
既然我们有了一个漂亮的PDF渲染器,我们现在可能希望添加漂亮的页眉和页脚。
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-7.cs
Imports IronPdf
Imports IronSoftware.Drawing
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.CssMediaType = Rendering.PdfCssMediaType.Print
renderer.RenderingOptions.PrintHtmlBackgrounds = False
renderer.RenderingOptions.PaperOrientation = Rendering.PdfPaperOrientation.Landscape
renderer.RenderingOptions.WaitFor.RenderDelay(150)
renderer.RenderingOptions.TextHeader.CenterText = "VB.NET PDF Slideshow"
renderer.RenderingOptions.TextHeader.DrawDividerLine = True
renderer.RenderingOptions.TextHeader.FontSize = "13"
renderer.RenderingOptions.TextFooter.RightText = "page {page} of {total-pages}"
renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial
renderer.RenderingOptions.TextFooter.FontSize = "9"
Dim document = renderer.RenderHtmlFileAsPdf("..\..\slideshow\index.html")
document.SaveAs("Html5WithHeader.pdf")
System.Diagnostics.Process.Start("Html5WithHeader.pdf")
End Sub
End Module
如所示,支持逻辑页眉和页脚。 您还可以根据VB.NET PDF开发者API在线参考中描述的内容添加基于HTML的页眉和页脚。
您可以下载并探索这个“VB.NET HTML 转 PDF”项目的源代码,作为 VB.NET Visual Studio 项目。
4. 创建包含动态内容的PDF:2种方法
历史上,对于软件工程师来说,PDF“模板化”一直是一项艰巨的任务。 将内容盖章到PDF模板中很少有效。 这是因为每个案例或报告将包含不同类型和长度的内容。 幸运的是,HTML在处理动态数据方面表现出色。
对此,我们有两种解决方案:
使用 .NET 将 HTML 的字符串模板转换为 PDF
- 将内容渲染为 ASP.NET 网页,然后将页面渲染为 PDF
4.1 方法 1 - ASP.NET - 使用 VB.NET Web Forms 将 ASPX 转换为 PDF
幸运的是,这个解决方案出奇的简单。 任何版本的 .NET Web 表单(包括 Razor)都可以使用以下 VB.NET 代码在 VB.NET 代码背后的 Page_Load 子程序中渲染为 PDF 文档。
PDF文档可以设置内容布置以在浏览器中显示,或者作为文件下载。
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-8.cs
Imports IronPdf
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim PdfOptions = New IronPdf.ChromePdfRenderOptions()
IronPdf.AspxToPdf.RenderThisPageAsPDF(AspxToPdf.FileBehavior.Attachment, "MyPdf.pdf", PdfOptions)
End Sub
4.2 方法2 - 使用字符串模板的HTML转PDF
要创建包含实例特定数据的动态PDF文档,我们只需创建一个HTML字符串以匹配我们希望渲染为PDF的数据。
这可能是VB.NET中HTML转PDF解决方案的最大优势——通过动态创建HTML“即时”轻松直观地创建动态PDF文档和报告的能力。
最简单的版本是来自 VB.NET 的 String.Format 方法
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-9.cs
Imports IronPdf
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim Html = "Hello {0}"
String.Format(Html, "World")
Dim document = renderer.RenderHtmlAsPdf(Html)
document.SaveAs("HtmlTemplate.pdf")
System.Diagnostics.Process.Start("HtmlTemplate.pdf")
End Sub
End Module
随着PDF文件变得更加复杂,字符串也会变得更加复杂。 我们可能会考虑使用字符串构建器,甚至是像HandleBars.Net或Razor这样的模板框架。
https://github.com/rexm/Handlebars.Net
5. 使用 VB.NET 编辑 PDF 文件
IronPDF for VB.NET 也允许编辑、加密、加水印或甚至将 PDF 文档转换回纯文本:
5.1.用 VB 将多个 PDF 文件合并为一个文档
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-10.cs
Dim pdfs = New List(Of PdfDocument)
pdfs.Add(PdfDocument.FromFile("A.pdf"))
pdfs.Add(PdfDocument.FromFile("B.pdf"))
pdfs.Add(PdfDocument.FromFile("C.pdf"))
Dim mergedPdf As PdfDocument = PdfDocument.Merge(pdfs)
mergedPdf.SaveAs("merged.pdf")
mergedPdf.Dispose()
For Each pdf As PdfDocument In pdfs
pdf.Dispose()
Next
5.2.为 PDF 添加封面页
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-11.cs
pdf.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"))
5.3.从 PDF 中删除最后一页
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-12.cs
pdf.RemovePage((pdf.PageCount - 1))
5.4.使用 128 位加密技术为 PDF 加密
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-13.cs
// Save with a strong encryption password.
pdf.Password = "my.secure.password";
pdf.SaveAs("secured.pdf")
5.5.用 VB 在页面上添加 HTML 内容
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-14.cs
Imports IronPdf
Imports IronPdf.Editing
Module Module1
Sub Main()
Dim renderer = New ChromePdfRenderer
Dim pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
Dim stamp = New HtmlStamper()
stamp.Html = "<h2>Completed</h2>"
stamp.Opacity = 50
stamp.Rotation = -45
stamp.VerticalAlignment = VerticalAlignment.Top
stamp.VerticalOffset = New Length(10)
pdf.ApplyStamp(stamp)
pdf.SaveAs("C:\Path\To\Stamped.pdf")
End Sub
End Module
5.6. 使用HTML向PDF添加分页符
使用HTML和CSS是最简单的方法。
:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-15.cs
<div style='page-break-after: always;'> </div>
6. 更多 .NET PDF 教程
您可能也会感兴趣:
- 完整的 VB.NET 和 C# MSDN 风格 API 参考
- 关于将 ASPX 转换为 PDF 的教程用于 VB.NET 和 C#
- 关于在 VB.NET 和 C# 中将 HTML 渲染为 PDF 的深入教程
结论
在本教程中,我们发现了使用 VB.NET 作为我们选择的编程语言,实现 VB.NET 到 PDF 转换的6种方法。
- 将 HTML 字符串转换为 PDF
- 使用 HTML 字符串在 VB.NET 中创建 PDF
- 将现有的URL渲染为PDF文件
- 从HTML文件生成PDF
- 在 VB.NET 中的 HTML 模板制作及转换为动态 PDFs
将包含实时数据的ASP.NET页面,例如ASPX转换为PDF文件
对于每一个,我们使用了流行的IronPDF VB.NET库,以便我们可以在.NET项目中直接将HTML转换为PDF文档。
教程快速访问
在 GitHub 上探索此教程
您可能还对我们在 GitHub 上广泛的 VB.NET PDF 生成和操作示例库感兴趣。探索源代码是学习的最快方式,而 GitHub 是在线进行此操作的权威方法。希望这些示例能帮助您深入了解 VB 项目中的 PDF 相关功能。
在 ASP.NET 中使用 VB.NET 和 C# 源代码创建 PDF 文件 使用IronPDF在VB.NET中将HTML渲染为PDF的简单Hello World项目 深入探索HTML To PDF与VB.NET下载 C# PDF 快速入门指南
为了使您在.NET应用程序中开发 PDF 更容易,我们将快速入门指南编译成 PDF 文档。该 "小抄 "提供了在 C# 和 VB.NET 中生成和编辑 PDF 的常用函数和示例的快速访问方法--这将节省您在.NET 项目中开始使用 IronPDF 的时间。
下载