原創(chuàng)|其它|編輯:郝浩|2011-10-28 11:32:41.000|閱讀 2306 次
概述:經(jīng)常有人問(wèn)道如何將文本文件轉(zhuǎn)換成PDF,希望我們提供一些代碼可以完成這項(xiàng)任務(wù),這樣他們就可以省下一些精力。因此,在本文中,我列出了以下兩個(gè)例子,通過(guò)使用Aspose.Pdf,可以快速高效地將文本文件轉(zhuǎn)換成PDF,希望對(duì)大家有幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
經(jīng)常有人問(wèn)道如何將文本文件轉(zhuǎn)換成PDF,希望我們提供一些代碼可以完成這項(xiàng)任務(wù),這樣他們就可以省下一些精力。因此,在本文中,我列出了以下兩個(gè)例子,通過(guò)使用Aspose.Pdf,可以快速高效地將文本文件轉(zhuǎn)換成PDF,希望對(duì)大家有幫助。
[C#]
System.IO.TextReader tr = new StreamReader("test.txt");
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a new section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Generator.Text t2 = new Aspose.Pdf.Generator.Text(tr.ReadToEnd());
sec1.Paragraphs.Add(t2);
pdf1.Save("test.Pdf");
[VB.NET]
Dim tr As System.IO.TextReader = New StreamReader("test.txt")
'Instantiate Pdf pbject by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Create a new section in the Pdf object
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
'Create a new text paragraph and pass the text to its constructor as argument
Dim t2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(tr.ReadToEnd())
sec1.Paragraphs.Add(t2)
pdf1.Save("test.Pdf")
大型文本文件轉(zhuǎn)換成PDF
有時(shí)候,我們可能需要將大型的文本文件轉(zhuǎn)換成PDF格式,使用上面的示例就不能滿足這一需求了,因?yàn)樵谏厦媸纠惺褂玫募夹g(shù),我們利用ReadToEnd方法讀取了整個(gè)文件,它會(huì)產(chǎn)生OutOfMemoryException異?;蛘呤瓜到y(tǒng)變慢。因此,在下面的示例中,我們利用了ReadLine方法取代了ReadToEnd方法。所謂ReadLine方法,顧名思義,每一次只讀取一行文本。為了做到這一點(diǎn),你需要使用一個(gè)循環(huán)。然后,在每一行之間循環(huán),并為每一行建立一個(gè)文本對(duì)象,將它添加到段落的集合。
為了確定文件尾,我們需要使用Peek方法。它可以讓你查看寫(xiě)入的文本字符,每次只能查看一個(gè)字符。如果流中不再有文本字符可讀,則返回-1,這意味著該文本文件結(jié)束。
[C#]
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a new section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Specify the location of input text file
String FILE_NAME = "d:/pdftest/LargeText.txt";
if (File.Exists(FILE_NAME))
{
System.IO.TextReader objReader = new System.IO.StreamReader(FILE_NAME);
// Read the file till the end of the file has come
do
{
//Create a new text paragraph & pass text to its constructor as argument
Aspose.Pdf.Generator.Text t2 = new Aspose.Pdf.Generator.Text(objReader.ReadLine());
// add the text object to paragraphs collection of section
sec1.Paragraphs.Add(t2);
// Read till the end of file
}while(objReader.Peek() != -1);
// Close the StreamReader object
objReader.Close();
}
else
MessageBox.Show("File Does Not Exist");
// Save the PDF file
pdf1.Save("d:/pdftest/large_textfile.pdf");
[VB.NET]
'Instantiate Pdf pbject by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Create a new section in the Pdf object
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
' Specify the location of input text file
Dim FILE_NAME As String = "d:/pdftest/LargeText.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
' Read the file till the end of the file has come
Do While objReader.Peek() <> -1
'Create a new text paragraph and pass text to its constructor as argument
Dim t2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(objReader.ReadLine())
' add the text object to paragraphs collection of section
sec1.Paragraphs.Add(t2)
Loop
' Close the StreamReader object
objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
' Save the PDF file
pdf1.Save("d:/pdftest/large_textfile.pdf")
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)