從Aspose PDF遷移到IronPDF
對於評估C# PDF程式庫的工程經理來說,IronPDF透過減少開發時間並提供完整功能,提供最佳的投資回報。 相比之下,像PDFSharp這樣的開源選項適合基本的程式化需求,並且初始成本較低。
在現代.NET應用程式中處理PDF文件越來越重要。 無論您是生成發票、轉換報告還是將表單整合到Web應用程式中,您都需要可靠的C# PDF程式庫。 有眾多選項可供選擇,哪一個是您專案中最好的PDF程式庫?
This article examines IronPDF alongside popular choices like iText, PDFSharp, Aspose, Syncfusion, QuestPDF, and more. 您將學習每個.NET PDF程式庫如何處理PDF生成、HTML轉換為PDF、編輯及其他關鍵功能。 本比較涵蓋授權、性能和易用性,幫助您做出正確決策。
為什麼您需要 C# PDF 程式庫?
在檢視特定的產品前,先考慮為何您需要C#程式庫來處理PDF文件:
-
使用CSS和JavaScript支援的HTML轉換為PDF。
- 確保在Windows、Linux和Docker上保持一致的佈局。
您的PDF程式庫應易於使用,所需的程式碼量要少,並提供高品質的渲染。 對於雲端部署,可參考《Azure部署》和《AWS Lambda整合》指南。
PDF程式庫的哪些功能對您的團隊最重要?
| 程式庫 | HTML轉PDF | JS 支援 | 編輯PDF | 許可證 | 最佳使用案例 |
|---|---|---|---|---|---|
| IronPDF | 是 | 完整 | Yes | 商業 | 具有動態內容的Web應用 |
| iText | 是 | 有限 | Yes | 商業 | 企業合規及靜態HTML |
PDFSharp / MigraDoc |
否 | 不適用 | Partial | 開源 | 程式化PDF建立和自定義佈局 |
| Aspose.PDF | 是 | 部分 | Yes | 商業 | 企業自動化和多格式轉換 |
| Syncfusion PDF | 是 | 部分 | Yes | 商業 | 報告和儀表板 |
| QuestPDF | 否 | 不適用 | Yes | 開源 | 結構化的程式化PDF文件 |
wkhtmltopdf (DinkToPdf) |
是 | 有限 | 否 | 開源 | 靜態HTML轉換為PDF |
此表提供了每個程式庫的核心優勢概覽,幫助您基於HTML/JS支援、編輯功能和授權來識別合適的工具。 下列部分檢驗了各程式庫如何處理基本任務如HTML轉換為PDF或一般PDF建立。
什麼是IronPDF,何時應該使用?
IronPDF 是一款現代商業.NET PDF 程式庫,旨在使PDF處理簡單而有效。與需要手動繪制或低層次API的程式庫不同,IronPDF專注於實際的使用案例:HTML轉為PDF、編輯PDF,以及生成報告,所需程式碼量極少。 實現處理了大部分複雜性,特別是在內容以HTML格式存在的Web應用程式中。 IronPDF 運行於Windows、.NET Framework、.NET Core 和如 Docker 這樣的容器化平台上,對於本地和雲端部署都十分靈活。
為什麼您的團隊應選擇IronPDF?
IronPDF如何處理複雜的網頁內容?
為了展示IronPDF處理含有複雜CSS或JavaScript的HTML內容,以下是一個URL轉換的例子:
using IronPdf;
class Program
{
static void Main()
{
// Initialize the Chrome PDF renderer
var renderer = new ChromePdfRenderer();
// Configure rendering options for optimal output
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(3000); // Wait 3 seconds for JS execution
// Set page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 10; // millimeters
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.MarginLeft = 10;
renderer.RenderingOptions.MarginRight = 10;
// Convert URL to PDF
var pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_170___");
// Save the PDF document
pdf.SaveAs("output.pdf");
// Optional: Add metadata
pdf.MetaData.Title = "Wikipedia Main Page";
pdf.MetaData.Author = "IronPDF Example";
pdf.MetaData.Subject = "URL to PDF Conversion";
}
}
using IronPdf;
class Program
{
static void Main()
{
// Initialize the Chrome PDF renderer
var renderer = new ChromePdfRenderer();
// Configure rendering options for optimal output
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.JavaScript(3000); // Wait 3 seconds for JS execution
// Set page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 10; // millimeters
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.MarginLeft = 10;
renderer.RenderingOptions.MarginRight = 10;
// Convert URL to PDF
var pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_170___");
// Save the PDF document
pdf.SaveAs("output.pdf");
// Optional: Add metadata
pdf.MetaData.Title = "Wikipedia Main Page";
pdf.MetaData.Author = "IronPDF Example";
pdf.MetaData.Subject = "URL to PDF Conversion";
}
}
Imports IronPdf
Class Program
Shared Sub Main()
' Initialize the Chrome PDF renderer
Dim renderer = New ChromePdfRenderer()
' Configure rendering options for optimal output
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
renderer.RenderingOptions.EnableJavaScript = True
renderer.RenderingOptions.WaitFor.JavaScript(3000) ' Wait 3 seconds for JS execution
' Set page size and margins
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
renderer.RenderingOptions.MarginTop = 10 ' millimeters
renderer.RenderingOptions.MarginBottom = 10
renderer.RenderingOptions.MarginLeft = 10
renderer.RenderingOptions.MarginRight = 10
' Convert URL to PDF
Dim pdf = renderer.RenderUrlAsPdf("___PROTECTED_URL_170___")
' Save the PDF document
pdf.SaveAs("output.pdf")
' Optional: Add metadata
pdf.MetaData.Title = "Wikipedia Main Page"
pdf.MetaData.Author = "IronPDF Example"
pdf.MetaData.Subject = "URL to PDF Conversion"
End Sub
End Class
您可以預期的結果是什麼?

借助極少的程式碼,IronPDF產出高保真PDF,渲染複雜的CSS佈局和動態JavaScript內容。 該程式庫的Chrome渲染引擎保證與現代瀏覽器匹配的圖元完美輸出。 您的應用程式可立即使用此功能來處理cookies、HTTP標頭或驗證。
結論:IronPDF是易於使用、高性能PDF生成的首選,有出色的HTML/CSS/JS渲染和專業支援。 查看演示以了解它的運作。
PDF程式庫如何處理Bootstrap和現代CSS框架?
選擇適用於使用Bootstrap和現代CSS框架的應用程式的C# PDF程式庫時,框架相容性決定了設計是否準確轉換或需調整。 這種方法為使用響應式設計模式的團隊提供了明確的好處。
為什麼Chromium引擎對Bootstrap很重要?
IronPDF的Chromium引擎提供完整支援:
- Bootstrap 5:完整的flexbox佈局、CSS Grid、實用工具類別、所有元件
- Bootstrap 4:完整的卡片系統、導航、彈性實用程式、響應式設計
- Tailwind CSS:所有實用工具類別與瀏覽器準確渲染
- Foundation:完整的網格系統和元件支持
- 現代CSS3:Flexbox、CSS Grid、自訂屬性、動畫、過渡
實體驗證:IronPDF以圖元完美的準確度渲染Bootstrap主頁和官方範例。 該程式庫為您管理了複雜性,包括Google字體、SVG圖形和自訂字體。
應該預計哪些Bootstrap限制?
iText:有限的flexbox支援(v7.1.15增加),無CSS Grid,Bootstrap 3的限制,需現代元件的逆工作纏。
PDFSharp & MigraDoc:無原生HTML渲染——僅手動PDF構建,無Bootstrap支援。
Aspose.PDF:自訂引擎,約90% CSS3支援,部分flexbox,需全面測試Bootstrap元件。
Syncfusion PDF:基於WebKit的引擎,無flexbox/CSS Grid,Bootstrap 3最高,安全問題(上次更新於2016年)。
QuestPDF:流暢API進行手動佈局——無HTML/CSS渲染,無Bootstrap支援。
開發影響:非Chromium程式庫需平行"PDF安全"佈局,顯著增加開發時間並減少設計一致性。
什麼是iText,何時應該使用?
iText是一個可靠的企業級C# PDF程式庫,用於生成、編輯和確保PDF安全。 實現了PDF/A、數位簽名、 隱藏和合規性滿滿的工作流程,適用於金融、法律和企業應用程式。 儘管iText可以處理HTML到PDF的轉換,但它無法本地執行JavaScript,需對動態內容進行預處理。 版本7.1.15增加了有限的flexbox支援,但許多CSS3特性仍不受支援。 查看我們的iText vs IronPDF比較。
什麼使iText對企業應用程式適合?
iText如何將URL轉換為PDF?
using iText.Html2pdf;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var url = "___PROTECTED_URL_171___";
// Create HTTP client with browser-like settings
using var client = new HttpClient();
// Add user agent to avoid 403 Forbidden responses
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36");
// Add timeout for reliability
client.Timeout = TimeSpan.FromSeconds(30);
// Fetch HTML content
string htmlContent = await client.GetStringAsync(url);
// Configure conversion properties
var converterProperties = new ConverterProperties();
converterProperties.SetBaseUri(url); // Important for resolving relative URLs
// Create PDF from HTML
using var fileStream = new FileStream("itext7-output.pdf", FileMode.Create);
HtmlConverter.ConvertToPdf(htmlContent, fileStream, converterProperties);
Console.WriteLine("PDF created successfully!");
}
}
using iText.Html2pdf;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var url = "___PROTECTED_URL_171___";
// Create HTTP client with browser-like settings
using var client = new HttpClient();
// Add user agent to avoid 403 Forbidden responses
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36");
// Add timeout for reliability
client.Timeout = TimeSpan.FromSeconds(30);
// Fetch HTML content
string htmlContent = await client.GetStringAsync(url);
// Configure conversion properties
var converterProperties = new ConverterProperties();
converterProperties.SetBaseUri(url); // Important for resolving relative URLs
// Create PDF from HTML
using var fileStream = new FileStream("itext7-output.pdf", FileMode.Create);
HtmlConverter.ConvertToPdf(htmlContent, fileStream, converterProperties);
Console.WriteLine("PDF created successfully!");
}
}
Imports iText.Html2pdf
Imports System.Net.Http
Imports System.IO
Imports System.Threading.Tasks
Module Program
Async Function Main() As Task
Dim url As String = "___PROTECTED_URL_171___"
' Create HTTP client with browser-like settings
Using client As New HttpClient()
' Add user agent to avoid 403 Forbidden responses
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " &
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36")
' Add timeout for reliability
client.Timeout = TimeSpan.FromSeconds(30)
' Fetch HTML content
Dim htmlContent As String = Await client.GetStringAsync(url)
' Configure conversion properties
Dim converterProperties As New ConverterProperties()
converterProperties.SetBaseUri(url) ' Important for resolving relative URLs
' Create PDF from HTML
Using fileStream As New FileStream("itext7-output.pdf", FileMode.Create)
HtmlConverter.ConvertToPdf(htmlContent, fileStream, converterProperties)
End Using
Console.WriteLine("PDF created successfully!")
End Using
End Function
End Module
iText的HTML渲染限制有哪些?

此程式碼獲取HTML內容並將其轉換為PDF。 結果保留頁面佈局、文字、圖像和CSS樣式,儘管動態JavaScript內容不會渲染。 對於JavaScript頁面,考慮IronPDF的JavaScript渲染或實施自訂渲染延遲。
結論:iText在企業PDF生成和編輯中表現出色,具有強大的合規支援和靜態HTML到PDF轉換。 對於JavaScript或高級CSS,請考慮IronPDF或wkhtmltopdf。
什麼是PDFSharp & MigraDoc ,何時應該使用?
PDFSharp和MigraDoc是免費開放源碼的C# PDF程式庫,用於程式化建立PDF。 PDFSharp處理低層次PDF生成,而MigraDoc 提供用於表格、段落和多頁文件的高級佈局API。 這些程式庫不提供HTML到PDF的轉換,使其在需要完全控制文件結構時理想選擇。 对于程式化繪圖,請參閱畫線和矩形和繪文字和位圖指南。
為什麼選擇PDFSharp進行簡單的PDF生成?
-
免費和開放源碼(MIT授權)。
-
程式化地建立含有文字、圖片和表格的PDF。
-
使用
MigraDoc支援多頁佈局。 -
輕量且易於整合。
-
完全控制繪製和定位。
- Works on .NET Framework and .NET Core.
如何使用PDFSharp程式化生成PDF?
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using static System.Net.Mime.MediaTypeNames;
class Program
{
static void Main()
{
// Configure font resolver for cross-platform compatibility
GlobalFontSettings.UseWindowsFontsUnderWindows = true;
// Create a new PDF document
var document = new PdfDocument();
document.Info.Title = "PDFSharp Example";
document.Info.Author = "Your Team";
document.Info.Subject = "Demonstrating PDFSharp capabilities";
// Add a page to the document
var page = document.AddPage();
page.Size = PdfSharp.PageSize.A4;
page.Orientation = PdfSharp.PageOrientation.Portrait;
// Create graphics object for drawing
var gfx = XGraphics.FromPdfPage(page);
// Draw text at specific coordinates
var font = new XFont("Verdana", 20, XFontStyleEx.Regular);
gfx.DrawString("Hello from PDFSharp!", font, XBrushes.Black,
new XPoint(50, 100));
// Add more content - a rectangle
var pen = new XPen(XColors.Navy, 2);
gfx.DrawRectangle(pen, XBrushes.LightBlue, 50, 150, 200, 100);
// Add text inside rectangle
var smallFont = new XFont("Arial", 12, XFontStyleEx.Regular);
gfx.DrawString("Custom drawing example", smallFont, XBrushes.DarkBlue,
new XPoint(60, 200));
// Save the document
document.Save("pdfsharp-example.pdf");
Console.WriteLine("PDF created with PDFSharp!");
}
}
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using static System.Net.Mime.MediaTypeNames;
class Program
{
static void Main()
{
// Configure font resolver for cross-platform compatibility
GlobalFontSettings.UseWindowsFontsUnderWindows = true;
// Create a new PDF document
var document = new PdfDocument();
document.Info.Title = "PDFSharp Example";
document.Info.Author = "Your Team";
document.Info.Subject = "Demonstrating PDFSharp capabilities";
// Add a page to the document
var page = document.AddPage();
page.Size = PdfSharp.PageSize.A4;
page.Orientation = PdfSharp.PageOrientation.Portrait;
// Create graphics object for drawing
var gfx = XGraphics.FromPdfPage(page);
// Draw text at specific coordinates
var font = new XFont("Verdana", 20, XFontStyleEx.Regular);
gfx.DrawString("Hello from PDFSharp!", font, XBrushes.Black,
new XPoint(50, 100));
// Add more content - a rectangle
var pen = new XPen(XColors.Navy, 2);
gfx.DrawRectangle(pen, XBrushes.LightBlue, 50, 150, 200, 100);
// Add text inside rectangle
var smallFont = new XFont("Arial", 12, XFontStyleEx.Regular);
gfx.DrawString("Custom drawing example", smallFont, XBrushes.DarkBlue,
new XPoint(60, 200));
// Save the document
document.Save("pdfsharp-example.pdf");
Console.WriteLine("PDF created with PDFSharp!");
}
}
Imports PdfSharp.Drawing
Imports PdfSharp.Fonts
Imports PdfSharp.Pdf
Imports System.Net.Mime.MediaTypeNames
Module Program
Sub Main()
' Configure font resolver for cross-platform compatibility
GlobalFontSettings.UseWindowsFontsUnderWindows = True
' Create a new PDF document
Dim document As New PdfDocument()
document.Info.Title = "PDFSharp Example"
document.Info.Author = "Your Team"
document.Info.Subject = "Demonstrating PDFSharp capabilities"
' Add a page to the document
Dim page = document.AddPage()
page.Size = PdfSharp.PageSize.A4
page.Orientation = PdfSharp.PageOrientation.Portrait
' Create graphics object for drawing
Dim gfx = XGraphics.FromPdfPage(page)
' Draw text at specific coordinates
Dim font As New XFont("Verdana", 20, XFontStyleEx.Regular)
gfx.DrawString("Hello from PDFSharp!", font, XBrushes.Black, New XPoint(50, 100))
' Add more content - a rectangle
Dim pen As New XPen(XColors.Navy, 2)
gfx.DrawRectangle(pen, XBrushes.LightBlue, 50, 150, 200, 100)
' Add text inside rectangle
Dim smallFont As New XFont("Arial", 12, XFontStyleEx.Regular)
gfx.DrawString("Custom drawing example", smallFont, XBrushes.DarkBlue, New XPoint(60, 200))
' Save the document
document.Save("pdfsharp-example.pdf")
Console.WriteLine("PDF created with PDFSharp!")
End Sub
End Module
PDFSharp什麼時候是合適的選擇?

此程式碼程式化產生PDF,新增文字並手動處理佈局。 實現為不使用HTML輸入的自訂文件如發票、表單或證書提供明確的好處。 對於類似程式化控制但具有更多功能,請參閱建立新PDF。
結論:PDFSharp & MigraDoc 認為是基本PDF建立的理想選擇——免費且易於整合,但缺乏HTML轉換和高級編輯功能。 對於HTML工作流,請考慮IronPDF的HTML到PDF功能。## 什麼是Aspose.PDF,何時應該使用?
Aspose.PDF是一個商業.NET PDF程式庫,提供建立、編輯、轉換和保護PDF的全面工具。 與輕量程式庫不同,Aspose.PDF專注於企業應用,支援包括Word、Excel、HTML和XML在內的文件轉換。 您的應用程式可以立即使用此功能進行文件自動化、報告生成,以及大型應用程式中的高級PDF操作。 查看我們的Aspose vs IronPDF 比較。
Aspose.PDF提供了什麼企業功能?
Aspose.PDF如何處理URL轉PDF的轉換?
using Aspose.Pdf;
using System;
using System.IO;
using System.Net;
class Program
{
static void Main()
{
string url = "___PROTECTED_URL_172___";
// Optional: provide credentials for protected resources
NetworkCredential credentials = null;
// Example for authenticated resources:
// credentials = new NetworkCredential("username", "password");
// Configure HTML load options
var options = new HtmlLoadOptions(url)
{
// Enable external resources loading
ExternalResourcesCredentials = credentials,
// Set page info
PageInfo = new PageInfo
{
Width = PageSize.A4.Width,
Height = PageSize.A4.Height,
Margin = new MarginInfo(20, 20, 20, 20) // left, bottom, right, top
},
// Enable JavaScript execution (limited support)
IsEmbedFonts = true,
IsRenderToSinglePage = false
};
try
{
// Fetch HTML content as stream and load into Document
using (var document = new Document(GetContentFromUrlAsStream(url, credentials), options))
{
// Add metadata
document.Info.Title = "Wikipedia Main Page";
document.Info.Author = "Aspose.PDF Example";
document.Info.Subject = "URL to PDF Conversion";
document.Info.Keywords = "PDF, Aspose, Wikipedia";
// Improve the PDF
document.OptimizeResources();
// Save PDF with specific save options
var saveOptions = new PdfSaveOptions
{
DefaultFontName = "Arial", // Fallback font
EmbedStandardFonts = true
};
document.Save("aspose-output.pdf", saveOptions);
}
Console.WriteLine("PDF successfully created!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private static Stream GetContentFromUrlAsStream(string url, ICredentials credentials = null)
{
using var handler = new System.Net.Http.HttpClientHandler { Credentials = credentials };
using var httpClient = new System.Net.Http.HttpClient(handler);
// Set a browser-like User-Agent to avoid 403 errors
httpClient.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36");
// Set timeout
httpClient.Timeout = TimeSpan.FromSeconds(30);
return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
}
}
using Aspose.Pdf;
using System;
using System.IO;
using System.Net;
class Program
{
static void Main()
{
string url = "___PROTECTED_URL_172___";
// Optional: provide credentials for protected resources
NetworkCredential credentials = null;
// Example for authenticated resources:
// credentials = new NetworkCredential("username", "password");
// Configure HTML load options
var options = new HtmlLoadOptions(url)
{
// Enable external resources loading
ExternalResourcesCredentials = credentials,
// Set page info
PageInfo = new PageInfo
{
Width = PageSize.A4.Width,
Height = PageSize.A4.Height,
Margin = new MarginInfo(20, 20, 20, 20) // left, bottom, right, top
},
// Enable JavaScript execution (limited support)
IsEmbedFonts = true,
IsRenderToSinglePage = false
};
try
{
// Fetch HTML content as stream and load into Document
using (var document = new Document(GetContentFromUrlAsStream(url, credentials), options))
{
// Add metadata
document.Info.Title = "Wikipedia Main Page";
document.Info.Author = "Aspose.PDF Example";
document.Info.Subject = "URL to PDF Conversion";
document.Info.Keywords = "PDF, Aspose, Wikipedia";
// Improve the PDF
document.OptimizeResources();
// Save PDF with specific save options
var saveOptions = new PdfSaveOptions
{
DefaultFontName = "Arial", // Fallback font
EmbedStandardFonts = true
};
document.Save("aspose-output.pdf", saveOptions);
}
Console.WriteLine("PDF successfully created!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private static Stream GetContentFromUrlAsStream(string url, ICredentials credentials = null)
{
using var handler = new System.Net.Http.HttpClientHandler { Credentials = credentials };
using var httpClient = new System.Net.Http.HttpClient(handler);
// Set a browser-like User-Agent to avoid 403 errors
httpClient.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36");
// Set timeout
httpClient.Timeout = TimeSpan.FromSeconds(30);
return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
}
}
Imports Aspose.Pdf
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Http
Class Program
Shared Sub Main()
Dim url As String = "___PROTECTED_URL_172___"
' Optional: provide credentials for protected resources
Dim credentials As NetworkCredential = Nothing
' Example for authenticated resources:
' credentials = New NetworkCredential("username", "password")
' Configure HTML load options
Dim options As New HtmlLoadOptions(url) With {
.ExternalResourcesCredentials = credentials,
.PageInfo = New PageInfo With {
.Width = PageSize.A4.Width,
.Height = PageSize.A4.Height,
.Margin = New MarginInfo(20, 20, 20, 20) ' left, bottom, right, top
},
.IsEmbedFonts = True,
.IsRenderToSinglePage = False
}
Try
' Fetch HTML content as stream and load into Document
Using document As New Document(GetContentFromUrlAsStream(url, credentials), options)
' Add metadata
document.Info.Title = "Wikipedia Main Page"
document.Info.Author = "Aspose.PDF Example"
document.Info.Subject = "URL to PDF Conversion"
document.Info.Keywords = "PDF, Aspose, Wikipedia"
' Improve the PDF
document.OptimizeResources()
' Save PDF with specific save options
Dim saveOptions As New PdfSaveOptions With {
.DefaultFontName = "Arial", ' Fallback font
.EmbedStandardFonts = True
}
document.Save("aspose-output.pdf", saveOptions)
End Using
Console.WriteLine("PDF successfully created!")
Catch ex As Exception
Console.WriteLine($"Error: {ex.Message}")
End Try
End Sub
Private Shared Function GetContentFromUrlAsStream(url As String, Optional credentials As ICredentials = Nothing) As Stream
Using handler As New HttpClientHandler With {.Credentials = credentials}
Using httpClient As New HttpClient(handler)
' Set a browser-like User-Agent to avoid 403 errors
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " &
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36")
' Set timeout
httpClient.Timeout = TimeSpan.FromSeconds(30)
Return httpClient.GetStreamAsync(url).GetAwaiter().GetResult()
End Using
End Using
End Function
End Class

此程式碼使用類似瀏覽器的使用者代理獲取HTML內容,以防止403錯誤,直接將HTML流傳到使用HtmlLoadOptions的Aspose.PDF文件,然後保存為PDF。 保留了頁面佈局、文字、圖像和CSS樣式,具有基本的HTTP驗證支援。 動態JavaScript內容不會執行。 為了更好的JavaScript支援,探索IronPDF的JavaScript渲染。
結論: Aspose.PDF在需要高級功能、多格式轉換和強大安全性的企業應用中表現出色。 雖然商業項目和可能對小項目較為複雜,但在大型文件工作流中無可匹敵。
什麼是Syncfusion PDF,什麼時候應該使用?
Syncfusion PDF是Syncfusion套件的一部分,提供功能豐富的.NET PDF程式庫,適用於網頁和桌面應用程式。 使用生成、編輯和轉換PDF時,程式庫為您管理複雜性,包括HTML到PDF的轉換,與其他Syncfusion元件無縫整合m報告和儀錶板。 使用Syncfusion組件的團隊會受益於緊密的整合。 查看我們的Syncfusion vs IronPDF 比較。
為什麼考慮Syncfusion用於整合解決方案?
Syncfusion如何將網址轉換為PDF?
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
class Program
{
static void Main()
{
// Initialize the HTML to PDF converter
HtmlToPdfConverter converter = new HtmlToPdfConverter();
// Configure WebKit settings for better rendering
WebKitConverterSettings settings = new WebKitConverterSettings();
// Set WebKit path (required for deployment)
settings.WebKitPath = @"C:\QtBinariesPath";
// Configure page settings
settings.PdfPageSize = PdfPageSize.A4;
settings.Orientation = PdfPageOrientation.Portrait;
settings.Margin = new PdfMargins() { All = 20 };
// Enable JavaScript execution
settings.EnableJavaScript = true;
settings.JavaScriptDelay = 3000; // Wait 3 seconds for JS
// Set viewport size for responsive design
settings.ViewPortSize = new System.Drawing.Size(1024, 0);
// Add custom headers if needed
settings.HttpRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
converter.ConverterSettings = settings;
// Convert URL to PDF
PdfDocument document = converter.Convert("___PROTECTED_URL_173___");
// Add document info
document.DocumentInformation.Title = "Wikipedia Main Page";
document.DocumentInformation.Author = "Syncfusion Example";
document.DocumentInformation.Subject = "URL to PDF Conversion";
// Save the PDF
document.Save("syncfusion-output.pdf");
document.Close(true); // true = dispose resources
Console.WriteLine("PDF created successfully!");
}
}
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
class Program
{
static void Main()
{
// Initialize the HTML to PDF converter
HtmlToPdfConverter converter = new HtmlToPdfConverter();
// Configure WebKit settings for better rendering
WebKitConverterSettings settings = new WebKitConverterSettings();
// Set WebKit path (required for deployment)
settings.WebKitPath = @"C:\QtBinariesPath";
// Configure page settings
settings.PdfPageSize = PdfPageSize.A4;
settings.Orientation = PdfPageOrientation.Portrait;
settings.Margin = new PdfMargins() { All = 20 };
// Enable JavaScript execution
settings.EnableJavaScript = true;
settings.JavaScriptDelay = 3000; // Wait 3 seconds for JS
// Set viewport size for responsive design
settings.ViewPortSize = new System.Drawing.Size(1024, 0);
// Add custom headers if needed
settings.HttpRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
converter.ConverterSettings = settings;
// Convert URL to PDF
PdfDocument document = converter.Convert("___PROTECTED_URL_173___");
// Add document info
document.DocumentInformation.Title = "Wikipedia Main Page";
document.DocumentInformation.Author = "Syncfusion Example";
document.DocumentInformation.Subject = "URL to PDF Conversion";
// Save the PDF
document.Save("syncfusion-output.pdf");
document.Close(true); // true = dispose resources
Console.WriteLine("PDF created successfully!");
}
}
Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf
Imports System.Drawing
Class Program
Shared Sub Main()
' Initialize the HTML to PDF converter
Dim converter As New HtmlToPdfConverter()
' Configure WebKit settings for better rendering
Dim settings As New WebKitConverterSettings()
' Set WebKit path (required for deployment)
settings.WebKitPath = "C:\QtBinariesPath"
' Configure page settings
settings.PdfPageSize = PdfPageSize.A4
settings.Orientation = PdfPageOrientation.Portrait
settings.Margin = New PdfMargins() With {.All = 20}
' Enable JavaScript execution
settings.EnableJavaScript = True
settings.JavaScriptDelay = 3000 ' Wait 3 seconds for JS
' Set viewport size for responsive design
settings.ViewPortSize = New Size(1024, 0)
' Add custom headers if needed
settings.HttpRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
converter.ConverterSettings = settings
' Convert URL to PDF
Dim document As PdfDocument = converter.Convert("___PROTECTED_URL_173___")
' Add document info
document.DocumentInformation.Title = "Wikipedia Main Page"
document.DocumentInformation.Author = "Syncfusion Example"
document.DocumentInformation.Subject = "URL to PDF Conversion"
' Save the PDF
document.Save("syncfusion-output.pdf")
document.Close(True) ' true = dispose resources
Console.WriteLine("PDF created successfully!")
End Sub
End Class
Syncfusion的報告優勢在哪裡?

這個例子將網址轉換為PDF,保留佈局、圖像和格式。 Syncfusion PDF在需要可靠的HTML到PDF渲染的報告場景中表現出色。 該程式庫支持專業文件的標頭和尾部、頁碼和書籤。
結論: Syncfusion PDF對於使用Syncfusion元件的團隊來說優秀,需要專業的HTML到PDF渲染,或需要具有廣泛功能的企業PDF生成。 欲尋求替代方案,請查看我們詳細的比較。
什麼是QuestPDF,何時應該使用?
QuestPDF是一個專注於程式化PDF生成的開源C#程式庫,使用宣告式API。 與HTML到PDF轉換器不同,QuestPDF完全在程式碼中構建PDF,提供對佈局、文字、圖像和表格的精確控制。 您的應用程式可以立即使用此功能進行動態生成的自動報告、發票和結構化文件。 查看我們的QuestPDF vs IronPDF 比較。
為什麼選擇QuestPDF進行程式化控制?
-
宣告式API以程式化方式建立PDF。
-
對佈局、表格、圖像和格式進行完全控制。
-
Improve for .NET Core and .NET Framework.
- 適合動態報告生成和自動化工作流程。
如何用QuestPDF的流暢API構建PDF?
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using QuestPDF.Previewer;
class Program
{
static void Main()
{
// Configure license (Community, Professional, or Enterprise)
QuestPDF.Settings.License = LicenseType.Community;
// Create document with fluent API
Document.Create(container =>
{
container.Page(page =>
{
// Page settings
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.PageColor(Colors.White);
page.DefaultTextStyle(x => x.FontSize(12).FontFamily(Fonts.Arial));
// Header section
page.Header()
.Height(100)
.Background(Colors.Grey.Lighten3)
.AlignCenter()
.AlignMiddle()
.Text("QuestPDF Example Document")
.FontSize(20)
.Bold()
.FontColor(Colors.Blue.Darken2);
// Content section
page.Content()
.PaddingVertical(1, Unit.Centimetre)
.Column(column =>
{
column.Spacing(20);
// Add title
column.Item().Text("Hello from QuestPDF!")
.FontSize(16)
.SemiBold()
.FontColor(Colors.Blue.Medium);
// Add paragraph
column.Item().Text(text =>
{
text.Span("This is an example of programmatic PDF generation using ");
text.Span("QuestPDF").Bold();
text.Span(". You have complete control over layout and styling.");
});
// Add table
column.Item().Table(table =>
{
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn();
columns.RelativeColumn();
});
// Table header
table.Header(header =>
{
header.Cell().Background(Colors.Grey.Medium)
.Padding(5).Text("Feature").Bold();
header.Cell().Background(Colors.Grey.Medium)
.Padding(5).Text("Description").Bold();
});
// Table rows
table.Cell().Border(1).Padding(5).Text("Fluent API");
table.Cell().Border(1).Padding(5)
.Text("Build documents using method chaining");
table.Cell().Border(1).Padding(5).Text("Layout Control");
table.Cell().Border(1).Padding(5)
.Text("Precise control over element positioning");
});
});
// Footer section
page.Footer()
.Height(50)
.AlignCenter()
.Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
})
.GeneratePdf("questpdf-output.pdf");
Console.WriteLine("PDF created with QuestPDF!");
}
}
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using QuestPDF.Previewer;
class Program
{
static void Main()
{
// Configure license (Community, Professional, or Enterprise)
QuestPDF.Settings.License = LicenseType.Community;
// Create document with fluent API
Document.Create(container =>
{
container.Page(page =>
{
// Page settings
page.Size(PageSizes.A4);
page.Margin(2, Unit.Centimetre);
page.PageColor(Colors.White);
page.DefaultTextStyle(x => x.FontSize(12).FontFamily(Fonts.Arial));
// Header section
page.Header()
.Height(100)
.Background(Colors.Grey.Lighten3)
.AlignCenter()
.AlignMiddle()
.Text("QuestPDF Example Document")
.FontSize(20)
.Bold()
.FontColor(Colors.Blue.Darken2);
// Content section
page.Content()
.PaddingVertical(1, Unit.Centimetre)
.Column(column =>
{
column.Spacing(20);
// Add title
column.Item().Text("Hello from QuestPDF!")
.FontSize(16)
.SemiBold()
.FontColor(Colors.Blue.Medium);
// Add paragraph
column.Item().Text(text =>
{
text.Span("This is an example of programmatic PDF generation using ");
text.Span("QuestPDF").Bold();
text.Span(". You have complete control over layout and styling.");
});
// Add table
column.Item().Table(table =>
{
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn();
columns.RelativeColumn();
});
// Table header
table.Header(header =>
{
header.Cell().Background(Colors.Grey.Medium)
.Padding(5).Text("Feature").Bold();
header.Cell().Background(Colors.Grey.Medium)
.Padding(5).Text("Description").Bold();
});
// Table rows
table.Cell().Border(1).Padding(5).Text("Fluent API");
table.Cell().Border(1).Padding(5)
.Text("Build documents using method chaining");
table.Cell().Border(1).Padding(5).Text("Layout Control");
table.Cell().Border(1).Padding(5)
.Text("Precise control over element positioning");
});
});
// Footer section
page.Footer()
.Height(50)
.AlignCenter()
.Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
})
.GeneratePdf("questpdf-output.pdf");
Console.WriteLine("PDF created with QuestPDF!");
}
}
Imports QuestPDF.Fluent
Imports QuestPDF.Helpers
Imports QuestPDF.Infrastructure
Imports QuestPDF.Previewer
Module Program
Sub Main()
' Configure license (Community, Professional, or Enterprise)
QuestPDF.Settings.License = LicenseType.Community
' Create document with fluent API
Document.Create(Sub(container)
container.Page(Sub(page)
' Page settings
page.Size(PageSizes.A4)
page.Margin(2, Unit.Centimetre)
page.PageColor(Colors.White)
page.DefaultTextStyle(Function(x) x.FontSize(12).FontFamily(Fonts.Arial))
' Header section
page.Header() _
.Height(100) _
.Background(Colors.Grey.Lighten3) _
.AlignCenter() _
.AlignMiddle() _
.Text("QuestPDF Example Document") _
.FontSize(20) _
.Bold() _
.FontColor(Colors.Blue.Darken2)
' Content section
page.Content() _
.PaddingVertical(1, Unit.Centimetre) _
.Column(Sub(column)
column.Spacing(20)
' Add title
column.Item().Text("Hello from QuestPDF!") _
.FontSize(16) _
.SemiBold() _
.FontColor(Colors.Blue.Medium)
' Add paragraph
column.Item().Text(Sub(text)
text.Span("This is an example of programmatic PDF generation using ")
text.Span("QuestPDF").Bold()
text.Span(". You have complete control over layout and styling.")
End Sub)
' Add table
column.Item().Table(Sub(table)
table.ColumnsDefinition(Sub(columns)
columns.RelativeColumn()
columns.RelativeColumn()
End Sub)
' Table header
table.Header(Sub(header)
header.Cell().Background(Colors.Grey.Medium) _
.Padding(5).Text("Feature").Bold()
header.Cell().Background(Colors.Grey.Medium) _
.Padding(5).Text("Description").Bold()
End Sub)
' Table rows
table.Cell().Border(1).Padding(5).Text("Fluent API")
table.Cell().Border(1).Padding(5) _
.Text("Build documents using method chaining")
table.Cell().Border(1).Padding(5).Text("Layout Control")
table.Cell().Border(1).Padding(5) _
.Text("Precise control over element positioning")
End Sub)
End Sub)
' Footer section
page.Footer() _
.Height(50) _
.AlignCenter() _
.Text(Sub(text)
text.Span("Page ")
text.CurrentPageNumber()
text.Span(" of ")
text.TotalPages()
End Sub)
End Sub)
End Sub) _
.GeneratePdf("questpdf-output.pdf")
Console.WriteLine("PDF created with QuestPDF!")
End Sub
End Module
QuestPDF何時表現卓越?

這一範例展示了無需HTML輸入的程式化PDF建立,內容和佈局完全可控。 QuestPDF在建立發票、表單和需要精確定位的結構化報告方面表現出色。 對於需要類似控制但有HTML支持的團隊,探索IronPDF的程式化功能。
結論: QuestPDF對於需要對PDF內容進行全部程式化控制的開發者來說是完美的選擇。 儘管缺乏HTML轉換,它在結構化、動態生成的PDF報告和自動化方面表現卓著。
哪個C# PDF程式庫應該選擇?
選擇合適的C# PDF程式庫取決於您的專案需求和內容型別。 對於具備複雜CSS或JavaScript的動態網頁內容,IronPDF提供了最可靠的解決方案,具有高保真渲染和簡單的API。對於需要PDF/A合規性、數位簽名或者多格式轉換的企業環境,iText和Aspose.PDF提供了全面的功能、安全性和支援。
對於喜歡程式化控制文件佈局和內容的開發者來說,像PDFSharp/MigraDoc 和QuestPDF這樣的開源程式庫在生成結構化報告、發票或無需HTML的票據方面表現出色。 Syncfusion PDF 提供了功能豐富的報告和儀錶板環境,而 wkhtmltopdf 則在將靜態網頁轉換為具有高 CSS 保真度的 PDF 方麵表現出色。
您如何評估企業準備情況?
在評估企業準備情況時請考慮以下因素:
支援與 SLA:IronPDF 提供24/5 技術支援,並保證回應時間。 iText 和 Aspose 這樣的商業程式庫提供企業支援套件。 開源選項依賴於社群支援。 要獲得協助,請參閱工程支援指南和最佳支援實踐。
安全性與合規性:IronPDF 支援PDF/A、PDF/UA、加密和數位簽名。 該程式庫定期進行安全審計,並提供 SOC 2 合規性。 對於企業級簽章,請參閱HSM 整合。
效能與擴展性:IronPDF 的Chrome 引擎有效處理異步操作和多執行緒。 對於高量場景,請考慮效能最佳化策略和並行處理。
總擁有成本:雖然開源選項沒有授權費用,但需考慮開發者時間、維護和支援成本。 IronPDF 的授權包含更新和支援,降低長期成本。 探索持續專案的擴展。## 為什麼您今天應該嘗試 IronPDF?
您準備好簡化 .NET 應用程式中的 PDF 生成、編輯和 HTML 到 PDF 的轉換嗎? 通過其易於使用的 API、高清晰度的渲染和專業的支援,您可以快速入門並立即看到成果。 探索我們完整的文件、程式碼範例和API 參考以加快開發速度。 查看我們的變更日志以獲取最新的更新以及里程碑以獲得重大改進。
立即開始免费试用,發現為什麼 IronPDF 是建設現代 .NET PDF 應用程式的開發者首選。 對於準備部署的團隊,請探索我們在Windows、Linux、Docker、macOS和Azure、AWS等雲端平台上的授權選項,具備透明的定價和靈活的部署。 了解更多有關IronWord文件和Ironsecuredoc文件的額外PDF安全功能。
常見問題
什麼是用於生成發票的最佳C# PDF程式庫?
IronPDF由於其強大的功能和易於在.NET應用程式中整合而受歡迎,是生成發票的熱門選擇。
IronPDF和iText在PDF生成方面的比較如何?
IronPDF提供更簡單的API和全面的文件,比iText更容易供開發者整合和使用。
我可以使用IronPDF將報告轉換為PDF嗎?
是的,IronPDF非常適合將各種型別的報告有效地轉換為PDF格式。
IronPDF與現代.NET應用程式相容嗎?
IronPDF與現代.NET應用程式完全相容,為開發者提供順暢的整合。
使用IronPDF比PDFSharp有什麼優勢?
IronPDF比PDFSharp提供更多先進功能和在現代.NET環境中更好的支持。
IronPDF支持將表單整合到網頁應用程式中嗎?
是的,IronPDF支持網頁應用程式中的表單整合,提供處理表單資料和PDF交互的工具。
對於C#的初學者,哪個PDF程式庫最好?
IronPDF使用者友好,提供豐富的文件,是C#初學者的絕佳選擇。
IronPDF的定價與其他PDF程式庫比較如何?
IronPDF提供具競爭力的定價和多種授權選項,通常比其他高級PDF程式庫具有更高的價值。
IronPDF能處理大規模PDF處理任務嗎?
是的,IronPDF設計用於高效處理大規模PDF處理任務,非常適合企業級專案。
支持IronPDF使用者的選項有哪些?
IronPDF提供全面的支持,包括文件、教程和快速響應的客服服務以協助使用者。



