翻譯|行業(yè)資訊|編輯:胡濤|2024-09-03 14:38:12.547|閱讀 84 次
概述:在本文中,您將學(xué)習(xí)如何使用Spire.PDF for .NET在 C# 和 VB.NET 中從頭創(chuàng)建 PDF 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
通過代碼創(chuàng)建 PDF 文檔具有多種優(yōu)勢。例如,您可以輕松合并動態(tài)內(nèi)容,如用戶輸入、數(shù)據(jù)庫記錄或?qū)崟r數(shù)據(jù)?;诖a的 PDF 生成允許更大的自定義和自動化,最大限度地減少創(chuàng)建高度定制文檔時的手動干預(yù)。在本文中,您將學(xué)習(xí)如何使用Spire.PDF for .NET在 C# 和 VB.NET 中從頭創(chuàng)建 PDF 文檔。
Spire.PDF for .NET 是一款獨立 PDF 控件,用于 .NET 程序中創(chuàng)建、編輯和操作 PDF 文檔。使用 Spire.PDF 類庫,開發(fā)人員可以新建一個 PDF 文檔或者對現(xiàn)有的 PDF 文檔進行處理,且無需安裝 Adobe Acrobat。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
首先,您需要將 Spire.PDF for.NET 包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。 以通過安裝。
PM> Install-Package Spire.PDF
Spire.PDF 中的一個頁面(用PdfPageBase表示)由客戶區(qū)和四周的邊距組成。內(nèi)容區(qū)用于用戶書寫各種內(nèi)容,邊距通常是空白的邊緣。
如下圖所示,頁面上的坐標(biāo)系原點位于客戶區(qū)的左上角,x軸向右水平延伸,y軸向下垂直延伸。所有添加到客戶區(qū)的元素都必須以指定的坐標(biāo)為基準(zhǔn)。
	 
 
此外,下表列出了重要的類和方法,可以幫助您輕松理解下一節(jié)提供的代碼片段。
| 成員 | 描述 | 
| PdfDocument 類 | 表示 PDF 文檔模型。 | 
| PdfPageBase 類 | 代表 PDF 文檔中的一頁。 | 
| PdfSolidBrush 類 | 表示使用純色填充任何對象的畫筆。 | 
| PdfTrueTypeFont 類 | 代表 True Type 字體。 | 
| PdfStringFormat 類 | 表示文本格式信息,例如對齊方式、字符間距和縮進。 | 
| PdfTextWidget 類 | 表示具有跨越多頁能力的文本區(qū)域。 | 
| PdfTextLayout 類 | 表示文本布局信息。 | 
| PdfDocument.Pages.Add() 方法 | 向 PDF 文檔添加頁面。 | 
| PdfPageBase.Canvas.DrawString() 方法 | 使用指定的字體和畫筆對象在頁面上的指定位置繪制字符串。 | 
| PdfTextWidget.Draw() 方法 | 在頁面上的指定位置繪制文本小部件。 | 
| PdfDocument.Save() 方法 | 將文檔保存為 PDF 文件。 | 
	
 
雖然 Spire.PDF for .NET 支持向 PDF 文檔添加各種元素,但本文僅演示如何創(chuàng)建純文本的 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產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn