VB.NET 與 C# 中的 PDF 生成器 — IronPDF 教學指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

本教學將逐步引導您了解如何在 VB.NET 中建立和編輯 PDF 檔案。 此技術同樣適用於 ASP.NET 網頁應用程式主控台應用程式Windows 服務以及桌面程式。 我們將使用 VB.NET 建立針對 .NET Framework 4 或 .NET Core 2 的 PDF 專案。您只需具備 Visual Basic .NET 開發環境,例如 Microsoft Visual Studio Community。

快速入門:在 VB.NET 中建立您的第一個 PDF 檔案

立即開始使用 VB.NET 版 IronPDF,僅需兩行程式碼即可建立您的第一個 PDF 檔案。 本快速入門指南將示範如何輕鬆將 IronPDF 整合至您的 .NET 應用程式中,讓您能夠快速產生 PDF 文件。 透過 IronPDF,您可以輕鬆地將 HTML 內容轉換為專業品質的 PDF 檔案。 請參閱這個簡單的範例,了解您能多快產出第一個 PDF 檔案,並探索 IronPDF 為您的專案提供的靈活功能。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 請複製並執行此程式碼片段。

    Dim PDF As New IronPdf.PdfDocument() 
    PDF.SaveAs("output.pdf")
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronPDF

    arrow pointer

簡化工作流程(5 個步驟)

  1. 下載 VB.NET PDF 函式庫
  2. 使用 VB.NET 函式庫建立 PDF 文件
  3. 自訂您的 PDF 文件樣式
  4. 選擇用於建立動態內容的方法
  5. 透過 VB.NET 函式庫編輯您的 PDF 檔案

使用 IronPDF 進行 PDF 建立與編輯的 VB .NET 程式碼

using VB.NET 將 HTML 轉為 PDF,套用樣式、運用動態內容,並輕鬆編輯您的檔案。 建立 PDF 檔案的流程簡單直觀,且相容於 .NET Framework 4、.NET Core 3.1、.NET 6 及 5。無需使用專有檔案格式或不同的 API。

本教學提供詳細文件,將引導您逐步使用開發者青睞的免費開發版 IronPDF 軟體來完成各項任務。 VB.NET 程式碼範例針對您的使用情境量身打造,讓您能在熟悉的操作環境中輕鬆掌握操作步驟。 此 VB.NET PDF 函式庫具備全面的建立與設定功能,適用於各種專案,無論是 ASP.NET 應用程式、主控台程式或桌面應用程式皆然。

隨 IronPDF 附帶

  • 由我們的 .NET PDF 函式庫開發團隊提供直接票務支援(真人服務!)
  • 支援 HTML、ASPX 表單、MVC 檢視、圖片,以及您現已使用的所有文件格式
  • 透過安裝 Microsoft Visual Studio 快速設定
  • 無限免費開發,正式上線授權方案起價為 $999

步驟 1

1. 從 IronPDF 免費下載 VB .NET PDF 函式庫

透過 NuGet 安裝:

在 Visual Studio 中,請在專案解決方案資源管理器上按右鍵,然後選取"管理 NuGet 套件..."。 接著,搜尋 IronPDF 並安裝最新版本。 若出現任何對話方塊,請點擊"確定"。

此工具適用於 Framework 4 及以上版本,或 .NET Core 2 及以上版本的任何 C# .NET Framework 專案。 此工具亦適用於 VB.NET 專案。

Install-Package IronPdf

NuGet 套件 - IronPDF

透過 DLL 安裝:

或者,您也可以從 IronPDF 下載頁面下載 IronPDF DLL,並手動將其安裝至專案或 GAC。

請使用 IronPDF 將此陳述加入任何 VB.NET 類別檔案的頂端:

Imports IronPdf
Imports IronPdf
VB .NET

操作教學

2. 使用 VB.NET 建立 PDF 檔案

相較於 iText 等採用專有設計 API 的函式庫,使用 IronPDF 透過 Visual Basic ASP.NET 首次建立 PDF 檔案,其操作簡便程度令人驚訝。

我們可以利用 HTML(搭配基於 Google Chromium 的像素級精準渲染引擎)來定義 PDF 的內容,並直接將其渲染為檔案。

使用 VB.NET 建立 PDF 的最簡單程式碼:

:path=/static-assets/pdf/content-code-examples/tutorials/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
VB .NET

透過使用 System.Diagnostics.Process.Start 方法,我們會在作業系統的預設 PDF 檢視器中開啟 PDF 檔案,使專案更具實用價值。

將任何現有網頁從 URL 渲染為 PDF:

:path=/static-assets/pdf/content-code-examples/tutorials/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
VB .NET

若要產生 PDF/A 格式的 PDF 檔案,請先以 IronPDF 格式渲染,再使用 Ghostscript 將其轉換為 PDF/A。


3. 為 VB.NET PDF 套用樣式

若要在 VB.NET 中為 PDF 內容設定樣式,我們可以充分利用 CSS、JavaScript 和圖片。 我們可能會連結至本地資源,或遠端/基於 CDN 的資源(例如 Google Fonts)。 我們也可以使用 DataURIs,將圖片和資源以字串形式嵌入您的 HTML 中

針對進階設計,我們採用兩階段流程:

  1. 完善開發與設計 HTML,此過程可能需要內部設計人員的參與。
  2. 使用 VB.NET 及我們的 PDF 函式庫,將該檔案渲染為 PDF 檔案。

將 HTML 檔案渲染為 PDF 的 VB.NET 程式碼:

Imports IronPdf

Module Module3
    Sub Main()
        ' Initialize PDF renderer
        Dim Renderer = New HtmlToPdf()

        ' Set rendering options
        Renderer.PrintOptions.CssMediaType = IronPdf.Rendering.PdfPrintOptions.PdfCssMediaType.Print
        Renderer.PrintOptions.EnableHtmlBackgrounds = False
        Renderer.PrintOptions.PaperOrientation = IronPdf.Rendering.PdfPrintOptions.PdfPaperOrientation.Landscape
        Renderer.PrintOptions.RenderDelay = 500 ' milliseconds

        ' Render the HTML file as a PDF
        Dim PDF = Renderer.RenderHtmlFileAsPdf("yourfile.html")

        ' Save PDF file
        PDF.SaveAs("styled_output.pdf")
    End Sub
End Module
Imports IronPdf

Module Module3
    Sub Main()
        ' Initialize PDF renderer
        Dim Renderer = New HtmlToPdf()

        ' Set rendering options
        Renderer.PrintOptions.CssMediaType = IronPdf.Rendering.PdfPrintOptions.PdfCssMediaType.Print
        Renderer.PrintOptions.EnableHtmlBackgrounds = False
        Renderer.PrintOptions.PaperOrientation = IronPdf.Rendering.PdfPrintOptions.PdfPaperOrientation.Landscape
        Renderer.PrintOptions.RenderDelay = 500 ' milliseconds

        ' Render the HTML file as a PDF
        Dim PDF = Renderer.RenderHtmlFileAsPdf("yourfile.html")

        ' Save PDF file
        PDF.SaveAs("styled_output.pdf")
    End Sub
End Module
VB .NET

範例 HTML 檔案

此 HTML 程式碼可建立動態且支援行動裝置的幻燈片展示,網址為 https://github.com/leemark/better-simple-slideshow

:path=/static-assets/pdf/content-code-examples/tutorials/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 &hearts; 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">&lt;div&gt;</span>) and each slide is a <span class="code">&lt;figure&gt;</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>&#9774; + &hearts;</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

如您所見,此範例運用了 HTML 網頁功能的"全方位"組合。 渲染作業由 IronPDF 內部執行,採用 Google 的 Chromium HTML 引擎與 v8 JavaScript 引擎。 您無需在系統上安裝這些工具,當您使用 IronPDF 時,整個套件會自動加入您的專案中。

3.1. 新增頁首與頁尾

當您獲得精美的 PDF 渲染效果後,您可能會想添加吸引人的頁首和頁尾。

:path=/static-assets/pdf/content-code-examples/tutorials/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

您可以依照線上 VB.NET PDF 開發者 API 參考文件中的說明,新增基於 HTML 的頁首與頁尾。

您可以下載並探索此"VB.NET HTML 轉 PDF"專案的原始碼,該專案為 VB.NET Visual Studio 專案。


4. 建立含動態內容的 PDF:2 種方法

長期以來,PDF"範本化"對軟體工程師而言始終是一項艱鉅的任務。 由於報告中的內容類型和長度各不相同,將內容直接嵌入 PDF 範本的做法通常難以奏效。 所幸,HTML 在處理動態資料方面表現極為出色。

為此,我們有兩種處理方式:

  1. 透過 .NET 進行 HTML 字串範本化,然後轉換為 PDF
  2. 將內容渲染為 ASP.NET 網頁,然後將該網頁渲染為 PDF

4.1. 方法 1 - ASP.NET - 使用 VB.NET Web Forms 將 ASPX 轉為 PDF

所幸,這個解決方案出乎意料地簡單。 任何類型的 .NET Web Form(包括 Razor)皆可透過 VB.NET 程式碼,在 VB.NET 程式碼背後(code-behind)的 Page_Load 子例程中渲染為 PDF 文件。

PDF 文件可能設定了 content-disposition 標頭,使其在瀏覽器中顯示或作為檔案下載。

:path=/static-assets/pdf/content-code-examples/tutorials/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
VB .NET

4.2. 方法 2 - 透過字串範本將 HTML 轉換為 PDF

若要建立包含特定實例資料的動態 PDF 文件,請建立一個與您希望渲染為 PDF 的資料相符的 HTML 字串。

這可能是 VB.NET 中 HTML 轉 PDF 解決方案的最大優勢——能夠透過"即時"生成 HTML,輕鬆且直觀地建立動態 PDF 文件與報告。

使用 VB.NET 中 String.Format 方法的最簡版本:

:path=/static-assets/pdf/content-code-examples/tutorials/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
VB .NET

隨著 PDF 檔案日益複雜,該字串也會變得更加複雜。 建議使用 StringBuilder,或採用 HandleBars.NetRazor 等模板框架:https://github.com/rexm/Handlebars.Net


5. 使用 VB.NET 編輯 PDF 檔案

IronPDF 適用於 VB.NET,可讓 PDF 文件進行編輯、加密、添加浮水印,甚至轉回純文字:

5.1. 在 VB 中將多個 PDF 檔案合併為單一文件

:path=/static-assets/pdf/content-code-examples/tutorials/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
VB .NET

5.2. 為 PDF 檔案新增封面頁

:path=/static-assets/pdf/content-code-examples/tutorials/vb-net-pdf-11.cs
pdf.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"))
VB .NET

5.3. 從 PDF 中移除最後一頁

:path=/static-assets/pdf/content-code-examples/tutorials/vb-net-pdf-12.cs
pdf.RemovePage((pdf.PageCount - 1))
VB .NET

5.4. 使用 128 位元加密來加密 PDF

:path=/static-assets/pdf/content-code-examples/tutorials/vb-net-pdf-13.cs
// Save with a strong encryption password.
pdf.Password = "my.secure.password";
pdf.SaveAs("secured.pdf")
VB .NET

5.5. 在 VB 中將額外的 HTML 內容嵌入頁面

:path=/static-assets/pdf/content-code-examples/tutorials/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
VB .NET

5.6. 使用 HTML 為 PDF 加入分頁符

最簡單的方法是使用 HTML 和 CSS

:path=/static-assets/pdf/content-code-examples/tutorials/vb-net-pdf-15.cs
<div style='page-break-after: always;'>&nbsp;</div>
HTML

6. 更多 .NET PDF 教學

您可能也會對以下內容感興趣:


結論

在本教學中,我們探討了 6 種以 VB.NET 作為首選程式語言,將 VB.NET 內容轉為 PDF 的方法。

  • HTML 字串轉 PDF
  • 使用 VB.NET 透過 HTML 字串定義內容來建立 PDF 檔案
  • 將現有 URL 渲染為 PDF 檔案
  • 從 HTML 檔案產生 PDF
  • VB.NET 中的 HTML 範本處理及轉換為動態 PDF
  • 將包含即時資料的 ASP.NET 頁面(例如 ASPX)轉換為 PDF 檔案

針對每項工具,我們皆採用廣受歡迎的 IronPDF VB.NET 函式庫,在 .NET 專案中將 HTML 直接轉換為 PDF 文件。


教學快速連結

Brand Visual Studio related to 教學快速連結

下載此教學指南的原始碼

本教學的完整免費 VB.NET HTML 轉 PDF 原始碼,可下載為 ZIP 格式的 Visual Studio 專案檔案。

下載

在 GitHub 上探索此教學指南

您可能也會對我們在 GitHub 上豐富的 VB.NET PDF 生成與處理範例庫感興趣。探索原始碼是學習的最快途徑,而 GitHub 正是線上進行此操作的最佳平台。希望這些範例能幫助您在 VB 專案中掌握與 PDF 相關的功能。

使用 VB.NET 和 C# 在 ASP.NET 中建立 PDF 檔案 原始碼 使用 IronPDF 在 VB.NET 中將 HTML 渲染為 PDF 的簡單 Hello World 專案 深入探索使用 VB.NET 將 HTML 轉換為 PDF
Github Icon related to 教學快速連結
Html To Pdf Icon related to 教學快速連結

下載 C# PDF 快速入門指南

為了讓您在 .NET 應用程式中更輕鬆地開發 PDF,我們已將快速入門指南彙編成 PDF 文件。這份"速查表"提供快速存取常用功能與範例,協助您在 C# 和 VB.NET 中建立及編輯 PDF,並能節省您在 .NET 專案中開始使用 IronPDF for .NET 的時間。

下載

查看 API 參考

探索 IronPDF 的 API 參考文件,其中詳述了 IronPDF 所有功能、命名空間、類別、方法、欄位及枚舉的詳細資訊。

查看 API 參考
Documentation related to 教學快速連結

常見問題

如何在 VB.NET 中將 HTML 轉換為 PDF?

您可以使用 RenderHtmlAsPdf IronPDF 中 HtmlToPdf 類別的 `HtmlToPdf()` 方法,將 HTML 字串轉換為 PDF 檔案。此方法允許您包含 CSS 和 JavaScript,以進行樣式設定及呈現動態內容。

設定 VB.NET PDF 函式庫需要哪些步驟?

要設定 VB.NET PDF 函式庫,您需要透過 Visual Studio 的 NuGet 搜尋「IronPDF」並將其新增至專案中。或者,您也可以從 IronPDF 網站下載 DLL 檔案,並手動在專案中引用該檔案。

我可以使用這個函式庫將 ASP.NET 頁面轉為 PDF 檔嗎?

是的,IronPDF 能夠直接將 ASP.NET 網頁渲染為 PDF 文件。這是透過渲染網頁網址,並使用 RenderUrlAsPdf 方法將其轉換為 PDF 文件。

如何在將 HTML 轉為 PDF 時套用 CSS 樣式?

IronPDF 支援在將 HTML 渲染為 PDF 時套用 CSS 樣式。您可以直接在 HTML 字串中包含 CSS,或引用外部樣式表,以確保 PDF 維持預期的樣式。

是否可以在 PDF 文件中添加浮水印?

是的,您可以使用 IronPDF 在 PDF 文件中添加浮水印。在文件建立過程中,您可以在 PDF 的每一頁上疊加文字或圖片,作為浮水印使用。

如何將多個 PDF 合併為單一檔案?

請使用 PdfDocument.Merge 方法將多個 PDF 檔案合併為單一完整的文件。此功能對於整合各類報告或文件非常實用。

有哪些可用選項可對 PDF 文件進行加密?

IronPDF 提供 PdfSecurity 類別,用於啟用 PDF 文件的加密功能。您可以在儲存 PDF 之前設定密碼和加密選項,以確保文件安全。

我可以在從 HTML 建立的 PDF 中加入 JavaScript 嗎?

是的,IronPDF 允許您在將 HTML 內容轉換為 PDF 時,將 JavaScript 嵌入其中。此功能有助於在 PDF 中加入互動元素或動態內容。

哪裡可以找到更多關於使用 VB.NET PDF 函式庫的資源或範例?

您可在 IronPDF 文件、GitHub 儲存庫以及可下載的 Visual Studio 專案中找到更多資源與範例。這些資源針對 VB.NET 的各種使用情境提供了全面的指引。

如何在 PDF 文件中添加頁首和頁尾?

在 IronPDF 中,您可以透過設定 PrintOptions.HeaderPrintOptions.Footer 屬性設定 HTML 內容,從而實現動態且具樣式的頁首與頁尾。

IronPDF 在 VB.NET 專案中是否完全相容於 .NET 10?

是的。IronPDF 完全相容於 .NET 10,並能在 VB.NET 專案中無縫運作。它支援所有現代 .NET 版本(包括 .NET 10),涵蓋桌面、網頁、主控台及雲端應用程式,讓您無需特別的變通方案,即可使用最新的執行環境、語言改進及效能提升功能。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。