翻譯|使用教程|編輯:胡濤|2023-06-20 10:05:01.053|閱讀 178 次
概述:在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將文本文件轉(zhuǎn)換為 PDF。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc 是一款專門對(duì) Word 文檔進(jìn)行操作的 類庫(kù)。在于幫助開發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
文本文件是一種包含純文本的計(jì)算機(jī)文件。它幾乎可以在任何計(jì)算機(jī)上查看,但功能非常基本且有限。如果您想對(duì)文本文件執(zhí)行更多操作,例如插入注釋或表單域,您可以將它們轉(zhuǎn)換為 PDF。在本文中,我們將演示如何使用Spire.PDF for .NET在 C# 和 VB.NET 中將文本文件轉(zhuǎn)換為 PDF。
首先,您需要將包含在 Spire.PDF for.NET 包中的 DLL 文件添加為您的 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝。
PM> Install-Package Spire.PDF
以下是使用 Spire.PDF for .NET 將文本文件轉(zhuǎn)換為 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
以上便是如何本文件轉(zhuǎn)換為 PDF,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn