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