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

Free Support Forum - aspose.com

Coverting Text to Pdf does not work


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”);<o:p></o:p>

java.io.FileInputStream fis = new

// System.out.println(file.exists()

  • “!!”);<o:p></o:p>

    // InputStream in =

    java.io.ByteArrayOutputStream bos = new

    byte[] buf = new byte[1024];<o:p></o:p>

    try {<o:p></o:p>

    for (int readNum; (readNum =
    fis.read(buf)) != -1;) {<o:p></o:p>

    bos.write(buf, 0, readNum); //no doubt here is

    //Writes len bytes from the specified
    byte array starting at offset off to this byte array output stream.

    System.out.println(“read " + readNum + " bytes,”);<o:p></o:p>


    } catch (java.io.IOException
    ex) {<o:p></o:p>


    byte[] bytes =

    java.io.ByteArrayInputStream srcStream
    = new

    java.io.BufferedReader reader = new
    java.io.BufferedReader(new java.io.InputStreamReader(srcStream));<o:p></o:p>

    String ls = System.getProperty(“line.separator”);<o:p></o:p>

    String line;<o:p></o:p>

    StringBuilder builder = new StringBuilder(5024);<o:p></o:p>

    try {<o:p></o:p>

    while ((line =
    reader.readLine()) != null) {<o:p></o:p>




    } catch (java.io.IOException
    e) {<o:p></o:p>

    } finally {<o:p></o:p>

    try {<o:p></o:p>


    } catch (java.io.IOException e) {<o:p></o:p>


    // Instantiate a Document object by calling
    its empty constructor

    com.aspose.pdf.Document doc = new

    // Add a new page in Pages
    collection of Document

    Page page = doc.getPages().add();<o:p></o:p>

    // Create an instance of TextFragmet
    and pass the text from reader object to its constructor as argument

    com.aspose.pdf.TextFragment text = new

    //text.TextState.Font =
    FontRepository.FindFont(“Arial Unicode MS”);

    // Add a new text paragraph in
    paragraphs collection and pass the TextFragment object


    // Save resultant PDF file<o:p></o:p>



Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the update.