翻譯|使用教程|編輯:胡濤|2023-06-20 10:05:01.053|閱讀 178 次
概述:在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將文本文件轉換為 PDF。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc 是一款專門對 Word 文檔進行操作的 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
文本文件是一種包含純文本的計算機文件。它幾乎可以在任何計算機上查看,但功能非常基本且有限。如果您想對文本文件執行更多操作,例如插入注釋或表單域,您可以將它們轉換為 PDF。在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將文本文件轉換為 PDF。
首先,您需要將包含在 Spire.PDF for.NET 包中的 DLL 文件添加為您的 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.PDF
以下是使用 Spire.PDF for .NET 將文本文件轉換為 PDF 的主要步驟:
【C#】
	
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.IO;
namespace ConvertTextToPdf
{
class Program
{
static void Main(string[] args)
{
//Read the text from the text file
string text = File.ReadAllText(@"Input.txt");
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Add a page
PdfPageBase page = pdf.Pages.Add();
//Create a PdfFont instance
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);
//Create a PdfTextLayout instance
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.Break = PdfLayoutBreakType.FitPage;
textLayout.Layout = PdfLayoutType.Paginate;
//Create a PdfStringFormat instance
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Justify;
format.LineSpacing = 20f;
//Create a PdfTextWidget instance from the text
PdfTextWidget textWidget = new PdfTextWidget(text, font, PdfBrushes.Black);
//Set string format
textWidget.StringFormat = format;
//Draw the text at the specified location of the page
RectangleF bounds = new RectangleF(new PointF(10, 25), page.Canvas.ClientSize);
textWidget.Draw(page, bounds, textLayout);
//Save the result file
pdf.SaveToFile("TextToPdf.pdf", FileFormat.PDF);
}
}
}
	
【VB.NET】
	
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
Imports System.IO
Namespace ConvertTextToPdf
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Read the text from the text file
Dim text = File.ReadAllText("Input.txt")
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Add a page
Dim page As PdfPageBase = pdf.Pages.Add()
'Create a PdfFont instance
Dim font As PdfFont = New PdfFont(PdfFontFamily.Helvetica, 11)
'Create a PdfTextLayout instance
Dim textLayout As PdfTextLayout = New PdfTextLayout()
textLayout.Break = PdfLayoutBreakType.FitPage
textLayout.Layout = PdfLayoutType.Paginate
'Create a PdfStringFormat instance
Dim format As PdfStringFormat = New PdfStringFormat()
format.Alignment = PdfTextAlignment.Justify
format.LineSpacing = 20F
'Create a PdfTextWidget instance from the text
Dim textWidget As PdfTextWidget = New PdfTextWidget(text, font, PdfBrushes.Black)
'Set string format
textWidget.StringFormat = format
'Draw the text at the specified location of the page
Dim bounds As RectangleF = New RectangleF(New PointF(10, 25), page.Canvas.ClientSize)
textWidget.Draw(page, bounds, textLayout)
'Save the result file
pdf.SaveToFile("TextToPdf.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
	
 
 
以上便是如何本文件轉換為 PDF,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn