Convert .txt file to PDF using Aspose.PDF for .NET - Save() method takes long time

Hello,
I am using Aspose.pdf to create a PDF in a web service. Everything works fine but when the line of code here… " document.Save(pdfPath)" runs, it can take up to 10 minutes to save the file into a folder. Some of the files can be large. The current one is around 10MB. The smaller ones seem to save pretty fast. In my PDF, there are no pictures. Just data. We are upgrading from a very old PDFBox writer software that can produce the same PDF in about 1 minute. The older software produces a smaller PDF file in terms of MB’s. One thing to note is that for the ASPOSE pdf software when I check the FONTS in the properties there are two listed: CourierNew and Helvetica. The older PDF writer just has Courrier. Could that effect this slow saving process? Any help would be greatly appreciated.

Here is some code of what I am doing…

   Dim license As Aspose.Pdf.License = New Aspose.Pdf.License()
    Dim myStream As FileStream = New FileStream("Aspose.Pdf.lic", FileMode.Open)
    license.SetLicense(myStream)


    Try

        ' *************************************************************************************
        Dim document As Document = New Document()

        document.Pages.Add()

        Dim data As System.IO.StreamReader = New StreamReader(txtPath)

        Dim nextLine As String = ""

        Dim page As Page = document.Pages(1)

        Dim tempHeight As Decimal
        Dim tempFontHeight As Decimal

        tempFontHeight = CDec(1.055 * Fontsize * 1.02)

        tempHeight = 1055 * PageHeight

        page.SetPageSize(PageWidth, tempHeight) ' 1055 is the height of the font Courier

        Dim pageMargin As New MarginInfo
        pageMargin.Left = Margin
        pageMargin.Top = CDec(tempHeight) - CDec(tempHeight - Margin + tempFontHeight)
        page.PageInfo.Margin = pageMargin

        Dim MFCommand As String = ""
        Dim i As Integer = 0
        Dim priorLine As String = ""
        Dim pageNum As Integer = 1
        Dim plusFound As String = "N"
        Dim firstRecord As String = "Y"


        'loop through the text file
        While (data.EndOfStream = False)

            If plusFound = "Y" Then
                plusFound = "N"

                If data.EndOfStream = False Then


                    ' ************************************************************************************** Lee 616
                    priorLine = nextLine
                    Dim text As TextFragment = New TextFragment()
                    Dim Segment1 As New TextSegment
                    Dim Segment2 As New TextSegment

                    text.TextState.Font = FontRepository.FindFont("Courier New")
                    text.TextState.FontSize = Fontsize

                    Segment2 = New TextSegment
                    Segment2.Text = priorLine
                    Segment2.Text = Replace(Segment2.Text, "+", " ")
                    text.Segments.Add(Segment2)

                    If Segment2.Text.Contains("_") = False Then
                        page.Paragraphs.Add(text)
                    End If

                    ' **************************************************************************************
                    nextLine = data.ReadLine


                End If

            Else ' else if for plusFound = Y


                If firstRecord = "Y" Then
                    firstRecord = "N"
                    i += 1

                    If i = 1 Then
                        While nextLine = ""
                            nextLine = data.ReadLine
                        End While
                    Else
                        nextLine = data.ReadLine
                    End If

                End If ' end if for firstRecord = "Y"

                If i > 1 Then
                    If nextLine.Length > 0 Then
                        MFCommand = nextLine.Substring(0, 1)
                    Else
                        MFCommand = ""
                    End If
                Else
                    nextLine = " " & nextLine.Substring(1)
                End If
                i += 1

                priorLine = nextLine ' holding the line before we add it to see if it needs to be underlined

                If priorLine <> "" Then
                    priorLine = Replace(priorLine.ToString, vbNullChar, " ")
                End If

                'create fragment to store the line in before adding it to the page(pdf)
                Dim text As TextFragment = New TextFragment(priorLine)

                If MFCommand <> "" Then
                    'Dim text As TextFragment = New TextFragment(nextLine)
                    text.Text = " " & text.Text.Substring(1)
                End If

                text.TextState.Font = FontRepository.FindFont("Courier New")
                text.TextState.FontSize = Fontsize

                'MFCommand - special character at beginning of the line
                Select Case MFCommand
                    Case "0" ' extra line
                        Dim textBlank As TextFragment = New TextFragment("")
                        page.Paragraphs.Add(textBlank)
                    Case "1" ' create new page
                        text.IsInNewPage = True
                        priorLine = " "

                    Case "+" ' underline

                    Case "-" ' blank line twice
                        Dim textBlank As TextFragment = New TextFragment("")
                        page.Paragraphs.Add(textBlank)

                        Dim textBlank2 As TextFragment = New TextFragment("")
                        page.Paragraphs.Add(textBlank2)

                End Select

                'if not end of the text file, read the next line (need to determine if next line is a underline line)
                If data.EndOfStream = False Then
                    nextLine = data.ReadLine
                    If nextLine.Length > 0 Then
                        MFCommand = nextLine.Substring(0, 1)
                    Else
                        MFCommand = ""
                    End If

                    If MFCommand = "+" Then ' if line in text file begins with a "+" - to underline previous line text

                        Dim dataLine As String = nextLine.Replace("+", " ")  'line containing text to be underlined - remove the "+"
                        Dim templength As Integer = 0
                        Dim Segment1 As New TextSegment
                        Dim Segment2 As New TextSegment
                        Dim num As Integer
                        plusFound = "Y"
                        text.Text = ""

                        Dim tempPriorLine As String = ""
                        Dim ii As Integer = 0
                        Dim j As Integer = 0
                        Dim countLineInstance As Integer = 0
                        Dim pad As Char
                        pad = Convert.ToChar(" ")

                        'loop through each character of the line in the textfile that contains the "+"
                        For ii = 0 To dataLine.Length - 1
                            'create new instance of the segments
                            Segment1 = New TextSegment
                            Segment2 = New TextSegment


                            'num contains number of spaces before the underline
                            num = dataLine.Length - CInt(dataLine.TrimStart.Length)

                            'removes the blanks before the underline
                            dataLine = dataLine.Substring(num)

                            ' *****************************************************************************************************************
                            ''If dataLine.Contains("_") Then
                            ''    add the string of text that Is equal to the number of blanks before the underline to the segment
                            ''    Segment1.Text = priorLine.Substring(0, num)
                            ''Else
                            ''    no underline so add all prior line but first character - usually occurs in New page
                            ''    Segment1.Text = priorLine.Substring(1)
                            ''End If
                            ' *****************************************************************************************************************

                            If num > 0 And num < Len(priorLine) Then
                                Segment1.Text = priorLine.Substring(0, num)
                            Else
                                Segment1.Text = priorLine
                            End If



                            'add the segment to the line
                            'If Segment1.Text.Trim <> "1" And Segment1.Text.Trim <> "11" Then ' LEE 617
                            text.Segments.Add(Segment1)
                            'Else
                            '    'Dim xxx As String = Segment1.Text
                            '    Dim tempSegment1 As New TextSegment
                            '    tempSegment1.Text = ""
                            '    text.Segments.Add(tempSegment1)
                            'End If

                            'loop to count the number of underlines
                            countLineInstance = 0   'will indicate how many characters to underline
                            For j = 0 To dataLine.Length - 1
                                If dataLine(j) = "_" Then
                                    countLineInstance = countLineInstance + 1
                                Else
                                    Exit For
                                End If
                            Next

                            'if there are underlines, add the characters that will be underlined to the segment
                            If countLineInstance > 0 Then
                                'make sure the line is the same length as the underline (pad with spaces if shorter)
                                templength = num + countLineInstance
                                If priorLine.Length < templength Then
                                    priorLine = priorLine.PadRight(templength, pad)
                                End If

                                'characters that will be underlined
                                tempPriorLine = priorLine.Substring(num, countLineInstance)

                                'add the text that will be underlined
                                Segment2.Text = tempPriorLine.PadRight(countLineInstance, pad)

                                'underline the text
                                Segment2.TextState.Underline = True

                                'add the segment to the line
                                text.Segments.Add(Segment2)

                                'resize the dataline - underlines and the priorLine that contains the text to be underlined
                                dataLine = dataLine.Substring(countLineInstance)
                                priorLine = priorLine.Substring(num + countLineInstance)


                            Else
                                Segment2.Text = ""
                                'add the segment to the line
                                text.Segments.Add(Segment2)
                            End If

                            If dataLine.Length = 0 Then ' at the end of the underlines

                                If priorLine.Length > 0 Then ' if there is data beyond the underlined portion above add it to the end of the data line
                                    Segment2 = New TextSegment
                                    Segment2.Text = priorLine
                                    text.Segments.Add(Segment2)
                                End If

                                Exit For

                            End If

                        Next
                        'add this line to the PDF
                        page.Paragraphs.Add(text)
                    Else
                        'reset underline flag
                        plusFound = "N"
                        'add this line to the PDF
                        page.Paragraphs.Add(text)
                    End If

                End If

            End If ' end if for plusFound = Y


            If pageNum = 1 Then ' adding index to top of first page
                pageNum += 1

                Dim pdfOutline As OutlineItemCollection = New OutlineItemCollection(document.Outlines)
                pdfOutline.Title = "Index"
                pdfOutline.Action = New Annotations.GoToAction(page)

                document.Outlines.Add(pdfOutline)

            End If


        End While

        'add the last line that was read before it got out of the while loop
        If nextLine.Trim.Length > 0 Then
            Dim lastText As New TextFragment
            lastText.Text = nextLine

            If Mid(lastText.Text, 1, 1) = "0" Or Mid(lastText.Text, 1, 1) = "1" Or Mid(lastText.Text, 1, 1) = "+" Or Mid(lastText.Text, 1, 1) = "-" Then
                lastText.Text = " " & Mid(lastText.Text, 2)
            End If

            lastText.TextState.Font = FontRepository.FindFont("Courier New")
            lastText.TextState.FontSize = Fontsize

            'add this line to the PDF
            page.Paragraphs.Add(lastText)
        End If




        document.Save(pdfPath) ' & "\" & "test_out.pdf")

        document = Nothing
        data.Close()
        data.Dispose()

@LeeWheeler

Would you please also share the .txt file which you are using in your code. Please share it in .zip format so that we can test the scenario in our environment and address it accordingly.

Here is the file.
thanks
ALRRALRRUSS07143137.zip (260.7 KB)

@LeeWheeler

We tried to execute the code snippet that you have shared and found that there were some undefined variables in it and it could not get compiled. Would you kindly provide a minimal code snippet which can be executed without any error and which is able to replicate the issue that you are facing. We will test the scenario in our environment and address it accordingly.

Here is the full Sub routine. In the parameters, the 2nd and 3rd ones are just where the TEXT file sits and where the PDF will reside. You will have to set those.

Private Sub MakeGeneralPDFAspose(ByVal fileName As String, ByVal txtPath As String, ByVal pdfPath As String, Optional ByVal PageWidth As Integer = 1000,
Optional ByVal PageHeight As Decimal = CDec(0.7), Optional ByVal Margin As Integer = 40, Optional ByVal Fontsize As Integer = 7)

    'Initialize license object
    'Dim licensepdf As New Aspose.Pdf.License()
    'Set license
    'licensepdf.SetLicense("/Licenses/Aspose.Pdf.lic")



    Dim license As Aspose.Pdf.License = New Aspose.Pdf.License()
    Dim myStream As FileStream = New FileStream("Aspose.Pdf.lic", FileMode.Open)
    license.SetLicense(myStream)


    Try

        ' *************************************************************************************
        Dim document As Document = New Document()

        document.Pages.Add()

        Dim data As System.IO.StreamReader = New StreamReader(txtPath)

        '' @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        ''Create a file stream to create the PDF document
        'Dim fs As FileStream = New FileStream("\\laspbsdevvm2\mainframePDF\Lee\processed\", FileMode.Create)

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


        Dim nextLine As String = ""

        Dim page As Page = document.Pages(1)

        Dim tempHeight As Decimal
        Dim tempFontHeight As Decimal

        tempFontHeight = CDec(1.055 * Fontsize * 1.02)

        tempHeight = 1055 * PageHeight

        page.SetPageSize(PageWidth, tempHeight) ' 1055 is the height of the font Courier

        Dim pageMargin As New MarginInfo
        pageMargin.Left = Margin
        pageMargin.Top = CDec(tempHeight) - CDec(tempHeight - Margin + tempFontHeight)
        page.PageInfo.Margin = pageMargin

        Dim MFCommand As String = ""
        Dim i As Integer = 0
        Dim priorLine As String = ""
        Dim pageNum As Integer = 1
        Dim plusFound As String = "N"
        Dim firstRecord As String = "Y"


        'loop through the text file
        While (data.EndOfStream = False)

            If plusFound = "Y" Then
                plusFound = "N"

                If data.EndOfStream = False Then


                    ' ************************************************************************************** Lee 616
                    priorLine = nextLine
                    Dim text As TextFragment = New TextFragment()
                    Dim Segment1 As New TextSegment
                    Dim Segment2 As New TextSegment

                    text.TextState.Font = FontRepository.FindFont("Courier New")
                    text.TextState.FontSize = Fontsize

                    Segment2 = New TextSegment
                    Segment2.Text = priorLine
                    Segment2.Text = Replace(Segment2.Text, "+", " ")
                    text.Segments.Add(Segment2)

                    If Segment2.Text.Contains("_") = False Then
                        page.Paragraphs.Add(text)
                    End If

                    ' **************************************************************************************
                    nextLine = data.ReadLine


                End If

            Else ' else if for plusFound = Y


                If firstRecord = "Y" Then
                    firstRecord = "N"
                    i += 1

                    If i = 1 Then
                        While nextLine = ""
                            nextLine = data.ReadLine
                        End While
                    Else
                        nextLine = data.ReadLine
                    End If

                End If ' end if for firstRecord = "Y"

                If i > 1 Then
                    If nextLine.Length > 0 Then
                        MFCommand = nextLine.Substring(0, 1)
                    Else
                        MFCommand = ""
                    End If
                Else
                    nextLine = " " & nextLine.Substring(1)
                End If
                i += 1

                priorLine = nextLine ' holding the line before we add it to see if it needs to be underlined

                If priorLine <> "" Then
                    priorLine = Replace(priorLine.ToString, vbNullChar, " ")
                End If

                'create fragment to store the line in before adding it to the page(pdf)
                Dim text As TextFragment = New TextFragment(priorLine)

                If MFCommand <> "" Then
                    'Dim text As TextFragment = New TextFragment(nextLine)
                    text.Text = " " & text.Text.Substring(1)
                End If

                text.TextState.Font = FontRepository.FindFont("Courier New")
                text.TextState.FontSize = Fontsize

                'MFCommand - special character at beginning of the line
                Select Case MFCommand
                    Case "0" ' extra line
                        Dim textBlank As TextFragment = New TextFragment("")
                        page.Paragraphs.Add(textBlank)
                    Case "1" ' create new page
                        text.IsInNewPage = True
                        priorLine = " "

                    Case "+" ' underline

                    Case "-" ' blank line twice
                        Dim textBlank As TextFragment = New TextFragment("")
                        page.Paragraphs.Add(textBlank)

                        Dim textBlank2 As TextFragment = New TextFragment("")
                        page.Paragraphs.Add(textBlank2)

                End Select

                'if not end of the text file, read the next line (need to determine if next line is a underline line)
                If data.EndOfStream = False Then
                    nextLine = data.ReadLine
                    If nextLine.Length > 0 Then
                        MFCommand = nextLine.Substring(0, 1)
                    Else
                        MFCommand = ""
                    End If

                    If MFCommand = "+" Then ' if line in text file begins with a "+" - to underline previous line text

                        Dim dataLine As String = nextLine.Replace("+", " ")  'line containing text to be underlined - remove the "+"
                        Dim templength As Integer = 0
                        Dim Segment1 As New TextSegment
                        Dim Segment2 As New TextSegment
                        Dim num As Integer
                        plusFound = "Y"
                        text.Text = ""

                        Dim tempPriorLine As String = ""
                        Dim ii As Integer = 0
                        Dim j As Integer = 0
                        Dim countLineInstance As Integer = 0
                        Dim pad As Char
                        pad = Convert.ToChar(" ")

                        'loop through each character of the line in the textfile that contains the "+"
                        For ii = 0 To dataLine.Length - 1
                            'create new instance of the segments
                            Segment1 = New TextSegment
                            Segment2 = New TextSegment


                            'num contains number of spaces before the underline
                            num = dataLine.Length - CInt(dataLine.TrimStart.Length)

                            'removes the blanks before the underline
                            dataLine = dataLine.Substring(num)

                            ' *****************************************************************************************************************
                            ''If dataLine.Contains("_") Then
                            ''    add the string of text that Is equal to the number of blanks before the underline to the segment
                            ''    Segment1.Text = priorLine.Substring(0, num)
                            ''Else
                            ''    no underline so add all prior line but first character - usually occurs in New page
                            ''    Segment1.Text = priorLine.Substring(1)
                            ''End If
                            ' *****************************************************************************************************************

                            If num > 0 And num < Len(priorLine) Then
                                Segment1.Text = priorLine.Substring(0, num)
                            Else
                                Segment1.Text = priorLine
                            End If



                            'add the segment to the line
                            'If Segment1.Text.Trim <> "1" And Segment1.Text.Trim <> "11" Then ' LEE 617
                            text.Segments.Add(Segment1)
                            'Else
                            '    'Dim xxx As String = Segment1.Text
                            '    Dim tempSegment1 As New TextSegment
                            '    tempSegment1.Text = ""
                            '    text.Segments.Add(tempSegment1)
                            'End If

                            'loop to count the number of underlines
                            countLineInstance = 0   'will indicate how many characters to underline
                            For j = 0 To dataLine.Length - 1
                                If dataLine(j) = "_" Then
                                    countLineInstance = countLineInstance + 1
                                Else
                                    Exit For
                                End If
                            Next

                            'if there are underlines, add the characters that will be underlined to the segment
                            If countLineInstance > 0 Then
                                'make sure the line is the same length as the underline (pad with spaces if shorter)
                                templength = num + countLineInstance
                                If priorLine.Length < templength Then
                                    priorLine = priorLine.PadRight(templength, pad)
                                End If

                                'characters that will be underlined
                                tempPriorLine = priorLine.Substring(num, countLineInstance)

                                'add the text that will be underlined
                                Segment2.Text = tempPriorLine.PadRight(countLineInstance, pad)

                                'underline the text
                                Segment2.TextState.Underline = True

                                'add the segment to the line
                                text.Segments.Add(Segment2)

                                'resize the dataline - underlines and the priorLine that contains the text to be underlined
                                dataLine = dataLine.Substring(countLineInstance)
                                priorLine = priorLine.Substring(num + countLineInstance)


                            Else
                                Segment2.Text = ""
                                'add the segment to the line
                                text.Segments.Add(Segment2)
                            End If

                            If dataLine.Length = 0 Then ' at the end of the underlines

                                If priorLine.Length > 0 Then ' if there is data beyond the underlined portion above add it to the end of the data line
                                    Segment2 = New TextSegment
                                    Segment2.Text = priorLine
                                    text.Segments.Add(Segment2)
                                End If

                                Exit For

                            End If

                        Next
                        'add this line to the PDF
                        page.Paragraphs.Add(text)
                    Else
                        'reset underline flag
                        plusFound = "N"
                        'add this line to the PDF
                        page.Paragraphs.Add(text)
                    End If

                End If

            End If ' end if for plusFound = Y


            If pageNum = 1 Then ' adding index to top of first page
                pageNum += 1

                Dim pdfOutline As OutlineItemCollection = New OutlineItemCollection(document.Outlines)
                pdfOutline.Title = "Index"
                pdfOutline.Action = New Annotations.GoToAction(page)

                document.Outlines.Add(pdfOutline)

            End If


        End While

        'add the last line that was read before it got out of the while loop
        If nextLine.Trim.Length > 0 Then
            Dim lastText As New TextFragment
            lastText.Text = nextLine

            If Mid(lastText.Text, 1, 1) = "0" Or Mid(lastText.Text, 1, 1) = "1" Or Mid(lastText.Text, 1, 1) = "+" Or Mid(lastText.Text, 1, 1) = "-" Then
                lastText.Text = " " & Mid(lastText.Text, 2)
            End If

            lastText.TextState.Font = FontRepository.FindFont("Courier New")
            lastText.TextState.FontSize = Fontsize

            'add this line to the PDF
            page.Paragraphs.Add(lastText)
        End If




        document.Save(pdfPath) ' & "\" & "test_out.pdf")

        document = Nothing
        data.Close()
        data.Dispose()
    Catch ex As Exception

        errorNote(ex)

    End Try
End Sub

The top portion of the code wouldnt go into the code snippet for some reason. Please just copy both. thanks

@LeeWheeler

We have logged an issue as PDFNET-48634 in our issue tracking system for the slow performance of the API. We will further look into its details and keep you informed with the status of its fix. Please be patient and spare us some time.

We are sorry for the inconvenience.