Free Support Forum - aspose.com

Aspose.pdf .NET issue -- document.save execution is extremely slow

Hello,
I am using aspose.pdf .NET version 20.6. In our project, we read a text file then format the text and save it as a pdf on the network. The execution of the logic which formats the text goes very fast. However, when the code reaches the point to where it executes the save, it can take a long time to save the file itself. It can take anywhere from 1-15 minutes for one file, depending on file size. Any file reaching 2 MB and over is going to take a while to generate. I am pasting the logic for building the pdf below. Could you please help me determine why the pdf takes so long to generate? Could it be the way that I increment the document.pages?
Thanks for your help
*** NOTE the start and end of the sub isnt being placed into the code holder below for some reason. They are just outside it.

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)

    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"

        While (data.EndOfStream = False)

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

                If data.EndOfStream = False Then

                    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

                    If nextLine.Length > 0 Then
                        If nextLine.Substring(0, 1) = "+" And Not (nextLine.Contains("_")) Then
                            nextLine = " " + nextLine.Substring(1)
                        End If
                    End If
                    '****

                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
                            '****If line begins with "+" but doesn't not contain any "_", remove the "+"
                            If nextLine.Length > 0 Then
                                If nextLine.Substring(0, 1) = "+" And Not (nextLine.Contains("_")) Then
                                    nextLine = " " + nextLine.Substring(1)
                                End If
                            End If
                            '****
                        End While
                    Else
                        nextLine = data.ReadLine
                        '****If line begins with "+" but doesn't not contain any "_", remove the "+"
                        If nextLine.Length > 0 Then
                            If nextLine.Substring(0, 1) = "+" And Not (nextLine.Contains("_")) Then
                                nextLine = " " + nextLine.Substring(1)
                            End If
                        End If
                        '****
                    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)

                        priorLine = " " & priorLine.Substring(1)
                    Case "1" ' create new page

                        'page.Dispose()
                        page = document.Pages(1)
                        page = document.Pages.Add
                        text.IsInNewPage = True
                        priorLine = " "

                        tempFontHeight = CDec(1.055 * Fontsize * 1.02)

                        tempHeight = 1055 * PageHeight

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

                        pageMargin.Left = Margin
                        pageMargin.Top = CDec(tempHeight) - CDec(tempHeight - Margin + tempFontHeight)
                        page.PageInfo.Margin = pageMargin

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

                    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 line begins with "+" but doesn't not contain any "_", remove the "+"
                    If nextLine.Length > 0 Then
                        If nextLine.Substring(0, 1) = "+" And Not (nextLine.Contains("_")) Then
                            nextLine = " " + nextLine.Substring(1)
                        End If
                    End If
                    '****

                    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(" ")

                        If dataLine.Contains("_") Then

                            '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 num > 0 And num < Len(priorLine) Then
                                    Segment1.Text = priorLine.Substring(0, num)
                                Else
                                    Segment1.Text = priorLine
                                End If

                                text.Segments.Add(Segment1)

                                '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

                        Else  'if there is a line starting with a "+" but doesn't have any underlines 
                            'create new instance of the segments
                            Segment1 = New TextSegment
                            Segment2 = New TextSegment

                            Segment1.Text = priorLine
                            Segment1.BaselinePosition.YIndent = 0
                            text.Segments.Add(Segment1)

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

                            dataLine = ""
                            priorLine = ""

                        End If

                        '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

        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

        data.Close()
        data.Dispose()

        WriteToLog("before save" & fileName, EventLogEntryType.Information)
        document.Save(pdfPath)

        document = Nothing
        document.Dispose()

    Catch ex As Exception


        errorNote(ex)

    Finally

    End Try

End Sub

@LeeWheeler

Would you please also share a sample .txt file so that we can use it to test the code snippet in our environment and address the issue accordingly.

EADREADRHAP07111840.zip (390.7 KB)

Is anyone checking this?

@LeeWheeler

We also noticed the delay in saving the PDF file while using Aspose.PDF for .NET 20.9 in our environment and logged an issue in our issue management system as PDFNET-48885. We will further investigate this case and keep you informed about the status of ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Ok. Thank you for the attention to this matter.

Any updates on this at all?

@LeeWheeler

The ticket has recently been logged in our issue management system and is pending for a review. It will be investigated and resolved on a first come first serve basis. We will surely inform you as soon as we have some definite updates regarding ticket resolution. Please give us some time.

We apologize for the inconvenience faced.

Thanks for the update. Any idea when this might get looked at? Our CIO for the agency has been asking me about this several times a day for the past week. I am concerned over the lack of guidance with this software that we purchased.

@LeeWheeler

As we shared earlier, the issue will be investigated and resolved on a first come first serve basis as this is the policy for the issues in the free support model. Furthermore, the performance-related issues are complex in nature and need a certain amount of time to get fully analyzed and fixed. We surely realize the significance of the issue for you and will consider your concerns during the issue investigation.

We highly appreciate your patience and comprehension in this matter. Please give us some time.

We really apologize for the inconvenience caused.