We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Coverting Text to Pdf does not work

http://www.aspose.com/docs/display/pdfjava/Converting+Text+File+to+PDF

I am getting an exception when running this code to convert a simple text file to pdf. Using aspose-pdf-9.3.1-jdk16.jar

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1911)
at com.aspose.pdf.internal.p230.z49.m20(Unknown Source)
at com.aspose.pdf.internal.p230.z49.m3(Unknown Source)
at aspose.pdf.internal.z220.m1(Unknown Source)
at aspose.pdf.internal.z239.m7(Unknown Source)
at aspose.pdf.internal.z228.m1(Unknown Source)
at aspose.pdf.pdftext.FontManager.m3(Unknown Source)
at aspose.pdf.pdftext.FontManager.m1(Unknown Source)
at aspose.pdf.internal.z239.m1(Unknown Source)
at aspose.pdf.internal.z239.m1(Unknown Source)
at aspose.pdf.internal.z292.m1(Unknown Source)
at aspose.pdf.internal.z314.m1(Unknown Source)
at aspose.pdf.internal.z303.m1(Unknown Source)
at aspose.pdf.xml.DocumentBase.m1(Unknown Source)
at aspose.pdf.internal.z277.m1(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)

Any idea?

Hi Dave,

Thanks for your inquiry. You are using old generator for Text to PDF conversion. Please use new generator for the conversion, it will help you to accomplish the task. However if the issue persist then please share your sample Text file here, we will look into it and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Thanks, that example worked. Is there a way to preserve formatting (\n, \r, etc) in the generated pdf?

Hi Dave,


Thanks for the acknowledgement.

Please share your input text file, so that we can test the scenario in our environment. We are sorry for your inconvenience.

attaching the text file and the generated pdf. thanks.

Hi Dave,


Thanks for sharing the resource files.

I have tested the scenario and I am able to
notice the same problem. For the sake of correction, I have logged this problem
as PDFNEWJAVA-35412 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.

Hi Dave,

Thank for your patience. In reference to you you above issue, please note It is need to add builder.append("\r\n") in loop of reading lines from buffer as in the snippet these symbols are not preserved and only the text extracted.

Please use the complete code snippet:

java.io.File file = new java.io.File(“c:/pdftest/plain-text.txt”);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
// System.out.println(file.exists()“!!”);
// InputStream in = resource.openStream();
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
System.out.println(“read " + readNum + " bytes,”);
}
} catch (java.io.IOException ex) {
}
byte[] bytes = bos.toByteArray();
java.io.ByteArrayInputStream srcStream = new java.io.ByteArrayInputStream(bytes);
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(srcStream));
String ls = System.getProperty(“line.separator”);
String line;
StringBuilder builder = new StringBuilder(5024);
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append("\r\n");
}
} catch (java.io.IOException e) {
} finally {
try {
reader.close();
} catch (java.io.IOException e) {<o:p></o:p>
}
// Instantiate a Document object by calling
its empty constructor
com.aspose.pdf.Document doc = new
com.aspose.pdf.Document();
// Add a new page in Pages collection of Document
Page page = doc.getPages().add();
// Create an instance of TextFragmet and pass the text from reader object to its constructor as argument
com.aspose.pdf.TextFragment text = new
com.aspose.pdf.TextFragment(builder.toString());
//text.TextState.Font =FontRepository.FindFont(“Arial Unicode MS”);
// Add a new text paragraph in paragraphs collection and pass the TextFragment object
page.getParagraphs().add(text);
// Save resultant PDF file
doc.save(“c:/pdftest/plain-text_TexttoPDF.pdf”);
}

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the update.