Convert your Text files to PDF
Feb 13 2011 05:11 PM | AWS in Code Samples
<p>We often get queries from customers who would like to convert their text files to PDF. We are often asked if we can quickly provide some code which can accomplish this task and save them the effort of going through the documentation. So for the benefit of everyone, we present here a simple example which can be used as it is to easily and efficiently convert a text file to PDF using <strong><a href="http://www.aspose.com/categories/file-format-components/aspose.pdf-for-.net-and-java/default.aspx">Aspose.Pdf</a></strong>.</p>
<p><strong>[C#]</strong></p>
<code>
System.IO.TextReader tr = new StreamReader("test.txt");
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();
//Create a new section in the Pdf object<br />
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Text t2 = new Aspose.Pdf.Text(tr.ReadToEnd());
sec1.Paragraphs.Add(t2);
pdf1.Save("test.Pdf");
<p><strong>[VB.NET]</strong></p>
<p>Dim tr As System.IO.TextReader = New StreamReader("test.txt")
\\'Instantiate Pdf pbject by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
\\'Create a new section in the Pdf objects
Dim sec1 As Aspose.Pdf.Section = pdf1.Sections.Add()
\\'Create a new text paragraph and pass the text to its constructor as argument
Dim t2 As Aspose.Pdf.Text = New Aspose.Pdf.Text(tr.ReadToEnd())
sec1.Paragraphs.Add(t2)
pdf1.Save(\\"test.Pdf\\")
</code>
<p><strong>[Java]</strong></p>
<code>
try{ try
StringBuffer sb = new StringBuffer(1024);
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
char[] chars = new char[1024];
int numRead = 0;
while( (numRead = reader.read(chars)) > -1){
sb.append(String.valueOf(chars));
<p>}</p>
<p>reader.close();
//Instantiate Pdf pbject by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a new section in the Pdf object
Section sec1 = pdf1.getSections().add();
//Create a new text paragraph and pass the text to its constructor as argument
Text text1 = new Text(sec1,sb.toString());
sec1.getParagraphs().add(text1);
pdf1.save(new FileOutputStream(new File("test.pdf")));
}catch(java.io.IOException ioe){
System.out.println(ioe.getMessage());
}catch(AsposeBaseException e){
System.out.println(e.getMessage());
}
</code>
<p><strong>More about Aspose.Pdf</strong></p>
<p>- <strong><a href="http://www.aspose.com/community/files/51/file-format-components/aspose.pdf/default.asp\">Download evaluation version</a></strong> of Aspose.Pdf.<br />
- <strong><a href="http://www.aspose.com/documentation/file-format-components/aspose.pdf-for-.net-and-java/index.html">Online documentation</a></strong> of Aspose.Pdf.<br />
- <strong><a href="http://www.aspose.com/demos/aspose.pdf/default.aspx">Demos</a></strong> of Aspose.Pdf.<br />
- Post your technical questions/queries to <strong><a href="http://www.aspose.com/community/forums/aspose.pdf-for-.net-java-and-reporting-services/20/showforum.aspx\\">Aspose.Pdf Forum</a></strong>.</p>
<p><strong>Contact Information</strong><br />
Suite 119, 272 Victoria Avenue<br />
Chatswood, NSW, 2067<br />
Australia<br />
<strong><a href="http://www.aspose.com/">Aspose - The .NET and Java component publisher</a></strong><br />
<strong>sales@aspose.com </strong><br />
Phone: 888.277.6734<br />
Fax: 866.810.9465l</p>
<p><strong>[C#]</strong></p>
<code>
System.IO.TextReader tr = new StreamReader("test.txt");
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();
//Create a new section in the Pdf object<br />
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Text t2 = new Aspose.Pdf.Text(tr.ReadToEnd());
sec1.Paragraphs.Add(t2);
pdf1.Save("test.Pdf");
<p><strong>[VB.NET]</strong></p>
<p>Dim tr As System.IO.TextReader = New StreamReader("test.txt")
\\'Instantiate Pdf pbject by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
\\'Create a new section in the Pdf objects
Dim sec1 As Aspose.Pdf.Section = pdf1.Sections.Add()
\\'Create a new text paragraph and pass the text to its constructor as argument
Dim t2 As Aspose.Pdf.Text = New Aspose.Pdf.Text(tr.ReadToEnd())
sec1.Paragraphs.Add(t2)
pdf1.Save(\\"test.Pdf\\")
</code>
<p><strong>[Java]</strong></p>
<code>
try{ try
StringBuffer sb = new StringBuffer(1024);
BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
char[] chars = new char[1024];
int numRead = 0;
while( (numRead = reader.read(chars)) > -1){
sb.append(String.valueOf(chars));
<p>}</p>
<p>reader.close();
//Instantiate Pdf pbject by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a new section in the Pdf object
Section sec1 = pdf1.getSections().add();
//Create a new text paragraph and pass the text to its constructor as argument
Text text1 = new Text(sec1,sb.toString());
sec1.getParagraphs().add(text1);
pdf1.save(new FileOutputStream(new File("test.pdf")));
}catch(java.io.IOException ioe){
System.out.println(ioe.getMessage());
}catch(AsposeBaseException e){
System.out.println(e.getMessage());
}
</code>
<p><strong>More about Aspose.Pdf</strong></p>
<p>- <strong><a href="http://www.aspose.com/community/files/51/file-format-components/aspose.pdf/default.asp\">Download evaluation version</a></strong> of Aspose.Pdf.<br />
- <strong><a href="http://www.aspose.com/documentation/file-format-components/aspose.pdf-for-.net-and-java/index.html">Online documentation</a></strong> of Aspose.Pdf.<br />
- <strong><a href="http://www.aspose.com/demos/aspose.pdf/default.aspx">Demos</a></strong> of Aspose.Pdf.<br />
- Post your technical questions/queries to <strong><a href="http://www.aspose.com/community/forums/aspose.pdf-for-.net-java-and-reporting-services/20/showforum.aspx\\">Aspose.Pdf Forum</a></strong>.</p>
<p><strong>Contact Information</strong><br />
Suite 119, 272 Victoria Avenue<br />
Chatswood, NSW, 2067<br />
Australia<br />
<strong><a href="http://www.aspose.com/">Aspose - The .NET and Java component publisher</a></strong><br />
<strong>sales@aspose.com </strong><br />
Phone: 888.277.6734<br />
Fax: 866.810.9465l</p>











0 Comments