Free Support Forum - aspose.com

Convert TXT to PDF using Aspose.PDF for .NET - OutOfMemoryException with large files

I am using different size text files but one I created that was 16mb was small enough and forget the 130mb text file to ever finish.

The code is taken from the website. This code does not work well to convert TXT to PDF. Is there a better more performant option?

            using (TextReader tr = new StreamReader(inputFile))
        {
            //Instantiate Pdf pbject by calling its empty constructor
            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();

            //Create a new section in the Pdf object
            Aspose.Pdf.Page page = pdfDocument.Pages.Add();

            //Create a new text paragraph and pass the text to its constructor as argument
            Aspose.Pdf.Text.TextFragment textFragment = new 
Aspose.Pdf.Text.TextFragment(tr.ReadToEnd());
            page.Paragraphs.Add(textFragment);

            pdfDocument.Save(outputFile);

@jojoshua

Would you kindly share your sample TXT file with us by uploading it to Dropbox or Google Drive. We will test the scenario in our environment and address it accordingly.

@asad.ali Thanks for the quick reply. Try this link to the text file.

@jojoshua

Would you kindly try using following code snippet to add the text from your .txt file and share the results with us.

System.IO.TextReader tr = new StreamReader(dataDir + "dummy.txt", Encoding.UTF8, true);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
String strLine;
TextBuilder builder = new TextBuilder(page);
double x = 100; double y = 100;
while ((strLine = tr.ReadLine()) != null)
{
 Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment(strLine);
 text.Position = new Position(x, y);
 if (y >= page.PageInfo.Height - 72)
 {
  y = 100;
  page.Dispose();
  page = doc.Pages.Add();
  builder = new TextBuilder(page);
 }
 else
 {
  y += 15;
 }
 builder.AppendText(text);
}
doc.Save(dataDir + "TexttoPDF_out.pdf");
tr.Close();

@asad.ali Thanks for the code. I did try it but the results were not much better. It was still very slow and I killed it before it finished. We also tested GdPicture SDK which is performing much better for txt conversion in comparison. I would rather use Aspose for all our conversions though.

@jojoshua

Thanks for your feedback.

Please note that the text file is quite larger and program itself needs more time to read and process all lines of the files. Since the text is being added line by line in suggested code snippet, the time is being taken. However, the memory consumption is controlled in this case as program was consuming only 200MB during testing in our environment.

Nevertheless, we have logged an investigation ticket for quality as PDFNET-47757 in our issue tracking system. We will further look into details of it and keep you posted with the status of ticket resolution. Please be patient and spare us little time.

We are sorry for the inconvenience.