原創|其它|編輯:郝浩|2011-10-28 11:32:41.000|閱讀 2306 次
概述:經常有人問道如何將文本文件轉換成PDF,希望我們提供一些代碼可以完成這項任務,這樣他們就可以省下一些精力。因此,在本文中,我列出了以下兩個例子,通過使用Aspose.Pdf,可以快速高效地將文本文件轉換成PDF,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
經常有人問道如何將文本文件轉換成PDF,希望我們提供一些代碼可以完成這項任務,這樣他們就可以省下一些精力。因此,在本文中,我列出了以下兩個例子,通過使用Aspose.Pdf,可以快速高效地將文本文件轉換成PDF,希望對大家有幫助。
[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")
大型文本文件轉換成PDF
有時候,我們可能需要將大型的文本文件轉換成PDF格式,使用上面的示例就不能滿足這一需求了,因為在上面示例中使用的技術,我們利用ReadToEnd方法讀取了整個文件,它會產生OutOfMemoryException異常或者使系統變慢。因此,在下面的示例中,我們利用了ReadLine方法取代了ReadToEnd方法。所謂ReadLine方法,顧名思義,每一次只讀取一行文本。為了做到這一點,你需要使用一個循環。然后,在每一行之間循環,并為每一行建立一個文本對象,將它添加到段落的集合。
為了確定文件尾,我們需要使用Peek方法。它可以讓你查看寫入的文本字符,每次只能查看一個字符。如果流中不再有文本字符可讀,則返回-1,這意味著該文本文件結束。
[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")
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網