Convert text file into PDF using Aspose.PDF for .NET

Hello,
When using this line to try and instantiate the PDF instance, I get a red line error stating Type Expected.

'Create a file stream to create the PDF document
Dim fs As FileStream = New FileStream(“e:/temp/SingleSeg-d.pdf”,FileMode.Create)

'Instantiate the Pdf instance and pass the file stream object to its constructor
Dim pdf As Pdf = New Pdf(fs) <----- ERROR HERE ON THIS LINE FOR “as PDF”

I am going by a suggested code sample for creating a PDF in a faster way… It didnt mention any needed Import. I have tried several different imports though which none helped.

@LeeWheeler

Please try to load a PDF document using Aspose.PDF for .NET 20.7 and following code snippet:

Dim fs As FileStream = New FileStream(“e:/temp/SingleSeg-d.pdf”,FileMode.Create)

'Instantiate the Pdf instance and pass the file stream object to its constructor
Dim pdf As Document = New Document(fs)

In case issue still persists, please share your sample PDF with us. We will test it and address the issue accordingly.

Using the document methodology is what we are currently using though. When we get to the bottom of the routine where the “document.Save” happens it is VERY SLOW. One of your websites recommends using the approach mentioned above to “addparagraph” to the PDF step by step loop instead of doing it all the end where it chews up too much memory and causes the extreme slow down upon save.

@LeeWheeler

You can use following approach in order to add text which is much faster:

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);
                //page.Paragraphs.Add(text);
                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();

You can convert this C# code into VB on any online converter. Meanwhile, we will respond to your other inquiry in other forum thread where you have shared your code snippet and .txt file.

Using this code snippet below, whenever it gets to the document.save() line at the bottom of the processing, it errors out saying “Cannot access a disposed object. Object name: ‘Page’.”

                    Case "1" ' create new page
                        page.Dispose()
                        page = document.Pages.Add
                        text.IsInNewPage = True
                        priorLine = " "
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); 
//page.Paragraphs.Add(text); 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();

We cannot get this logic to work. The line “System.IO.TextReader tr = new StreamReader(dataDir + “dummy.txt”, Encoding.UTF8, true);” errors out. The page.dispose also causes problems when trying to save because it wont save a disposed of item…

We are not sure what to try. The only version we can get to work takes 10 minutes to save a large PDF file.

@LeeWheeler

Would you please share what error you observed while reading the text file? You can please use any other method to read the .txt file line by line and add it to the PDF.

You may please remove the page.Dispose() line of code and try again. In case the issue still persists, please let us know. Furthermore, we have also logged an issue in our issue tracking system for your main case in other related forum post you opened earlier.