將文本文件轉(zhuǎn)換為 PDF
Spire.PDF for .NET 是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫(kù)。致力于在于幫助開(kāi)發(fā)人員輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔,而無(wú)需安裝 Microsoft Word。
行號(hào)用于在每行文本旁邊顯示 Word 自動(dòng)計(jì)算的行數(shù)。當(dāng)我們需要參考合同或法律文件等文檔中的特定行時(shí),它非常有用。word中的行號(hào)功能允許我們?cè)O(shè)置起始值、編號(hào)間隔、與文本的距離以及行號(hào)的編號(hào)方式。使用 Spire.Doc,我們可以實(shí)現(xiàn)上述所有功能。本文將介紹如何將文本文件轉(zhuǎn)換為 PDF
歡迎加入spire技術(shù)交流群:767755948
文本文件是一種包含純文本的計(jì)算機(jī)文件。它幾乎可以在任何計(jì)算機(jī)上查看,但功能非常基本且有限。如果您想對(duì)文本文件執(zhí)行更多操作,例如插入注釋或表單域,您可以將它們轉(zhuǎn)換為 PDF。在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將文本文件轉(zhuǎn)換為 PDF。
安裝 Spire.PDF for.NET
首先,您需要將包含在 Spire.PDF for.NET 包中的 DLL 文件添加為您的 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.PDF
在 C# 和 VB.NET 中將文本文件轉(zhuǎn)換為 PDF
以下是使用 Spire.PDF for .NET 將文本文件轉(zhuǎn)換為 PDF 的主要步驟:
- 使用File.ReadAllText()方法將文本文件中的文本讀入字符串對(duì)象。
- 創(chuàng)建一個(gè)PdfDocument實(shí)例并使用PdfDocument.Pages.Add()方法將頁(yè)面添加到 PDF 文件。
- 從文本創(chuàng)建一個(gè)PdfTextWidget實(shí)例。
- 使用PdfTextWidget.Draw()方法將文本繪制到 PDF 頁(yè)面上。
- 使用PdfDocument.SaveToFile()方法保存結(jié)果文件。
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