翻譯|行業資訊|編輯:胡濤|2024-09-03 14:38:12.547|閱讀 79 次
概述:在本文中,您將學習如何使用Spire.PDF for .NET在 C# 和 VB.NET 中從頭創建 PDF 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
通過代碼創建 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 包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。 以通過安裝。
PM> Install-Package Spire.PDF
Spire.PDF 中的一個頁面(用PdfPageBase表示)由客戶區和四周的邊距組成。內容區用于用戶書寫各種內容,邊距通常是空白的邊緣。
如下圖所示,頁面上的坐標系原點位于客戶區的左上角,x軸向右水平延伸,y軸向下垂直延伸。所有添加到客戶區的元素都必須以指定的坐標為基準。
此外,下表列出了重要的類和方法,可以幫助您輕松理解下一節提供的代碼片段。
成員 | 描述 |
PdfDocument 類 | 表示 PDF 文檔模型。 |
PdfPageBase 類 | 代表 PDF 文檔中的一頁。 |
PdfSolidBrush 類 | 表示使用純色填充任何對象的畫筆。 |
PdfTrueTypeFont 類 | 代表 True Type 字體。 |
PdfStringFormat 類 | 表示文本格式信息,例如對齊方式、字符間距和縮進。 |
PdfTextWidget 類 | 表示具有跨越多頁能力的文本區域。 |
PdfTextLayout 類 | 表示文本布局信息。 |
PdfDocument.Pages.Add() 方法 | 向 PDF 文檔添加頁面。 |
PdfPageBase.Canvas.DrawString() 方法 | 使用指定的字體和畫筆對象在頁面上的指定位置繪制字符串。 |
PdfTextWidget.Draw() 方法 | 在頁面上的指定位置繪制文本小部件。 |
PdfDocument.Save() 方法 | 將文檔保存為 PDF 文件。 |
雖然 Spire.PDF for .NET 支持向 PDF 文檔添加各種元素,但本文僅演示如何創建純文本的 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
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn