We have an application that creates New PDF documents using Aspose.Pdf.Document version 17.8 for .NET
The application adds between 50 and 750 text fragments to each page of the PDF using an Aspose.Pdf.TextBuilder
The application is also using the xImage functionality to create a watermark annotation to every page.
When an end user attempts to print the Output PDF, Adobe Reader displays a “Flattening” progress bar for each page. This progress bar is on screen for approximately 30 seconds per page of the PDF.
It may not be related, but the output of a PDF validation tool reports that “Maximum depth of graphics state nesting by q and Q operators exceeded.” We investigated the output file and found that each page begins with a number of consecutive q operators equal to the number of text fragments + 15. (65 opening “q” operators for 50 text fragments)
Code Snippet
`
Public Sub SavePDF(szOutputFile As String)
Dim sourcePage As PDF_Page
Dim sourceText As PDF_Text
Dim pdf As New Aspose.Pdf.Document()
Dim curPage As Aspose.Pdf.Page
Dim textBuilder As Aspose.Pdf.Text.TextBuilder
pdf.Info.Author = "Rich Clark"
pdf.Info.Subject = "EBE Rendtion Output PDF"
For Each sourcePage In lstPages
'''''''''PAGE LEVEL STUFF'''''''''''
curPage = pdf.Pages.Add()
curPage.SetPageSize(612, 792)
'595 x 842 is 8.26 x 11.69
curPage.PageInfo.Margin.Left = 0
curPage.PageInfo.Margin.Right = 0
curPage.PageInfo.Margin.Top = 0
curPage.PageInfo.Margin.Bottom = 0
textBuilder = New Aspose.Pdf.Text.TextBuilder(curPage)
For Each sourceText In sourcePage.TextList
With sourceText
If .HasLink And .ModifyLinkSourceFont Then
WriteLinkTextToPDF(textBuilder, .Left, .Top, .FontName, .FontSize, .Bold, .Italic, .Underline, .Value)
Else
WriteTextToPDF(textBuilder, .Left, .Top, .FontName, .FontSize, .Bold, .Italic, .Underline, .Value)
End If
If .HasLink Then
Dim link As Aspose.Pdf.Annotations.LinkAnnotation
Dim dLeft As Long
Dim dTop As Long
Dim dWidth As Long
Dim dHeight As Long
Dim targetColor As System.Drawing.Color
dLeft = .Left * (72 / m_X_DPI)
dWidth = .LinkWidth * (72 / m_X_DPI) ''365
dTop = (3300 - .Top) * (72 / m_Y_DPI) - (.FontSize * mFontScaleFactor) - mFontHeightOffset
dHeight = (.FontSize * mFontScaleFactor) * 1.05
link = New Aspose.Pdf.Annotations.LinkAnnotation(curPage, New Aspose.Pdf.Rectangle(dLeft, dTop, dLeft + dWidth, dTop + dHeight))
targetColor = System.Drawing.Color.FromName(.LinkBorderColor)
If targetColor = Drawing.Color.Transparent Then
link.Color = Aspose.Pdf.Color.FromArgb(0, 0, 0, 0)
link.Border = New Aspose.Pdf.Annotations.Border(link)
link.Border.Width = 0
Else
link.Color = Aspose.Pdf.Color.FromRgb(targetColor)
End If
'link.Border.Style = Aspose.Pdf.Annotations.BorderStyle.Underline
link.Action = New Aspose.Pdf.Annotations.GoToURIAction(.LinkTarget)
curPage.Annotations.Add(link)
End If
End With
Next
Next
''''''''''APPLY OVERLAY TO ALL PAGES
'Dim imageStream As Object
Using imageStream = System.IO.File.Open(mOverlayFilePath, IO.FileMode.Open)
Dim image As Aspose.Pdf.XImage
Dim imageRectangle As New Aspose.Pdf.Rectangle(0, 0, 612, 792)
Dim form As Aspose.Pdf.XForm
Dim szOverlay As String
'form = Aspose.Pdf.XForm.CreateNewForm(pdf.Pages(1), pdf)
'form.BBox = curPage.Rect
Dim annotation As New Aspose.Pdf.Annotations.WatermarkAnnotation(curPage, curPage.Rect)
form = annotation.Appearance("N")
form.BBox = curPage.Rect
If image Is Nothing Then
'szOverlay = form.Resources.Images.Add(imageStream)
form.Resources.Images.Add(imageStream)
szOverlay = form.Resources.Images(1).Name
image = form.Resources.Images(szOverlay)
Else
szOverlay = form.Resources.Images(1).Name
End If
form.Contents.Add(New Aspose.Pdf.Operator.GSave())
form.Contents.Add(New Aspose.Pdf.Operator.ConcatenateMatrix(New Aspose.Pdf.Matrix(imageRectangle.Width, 0, 0, imageRectangle.Height, 0, 0)))
form.Contents.Add(New Aspose.Pdf.Operator.Do(szOverlay))
form.Contents.Add(New Aspose.Pdf.Operator.GRestore())
For Each curPage In pdf.Pages
curPage.Annotations.Add(annotation, False)
imageRectangle = New Aspose.Pdf.Rectangle(0, 0, imageRectangle.Width, imageRectangle.Height)
Next
End Using
'SAVE IT
pdf.Save(szOutputFile)
End Sub
`
`
Private Sub WriteTextToPDF(textBuilder As Aspose.Pdf.Text.TextBuilder, sourceLeft As Integer, sourceTop As Integer, FontName As String, fontSize As Single, bBold As Boolean, bUnderline As Boolean, bItalic As Boolean, szText As String)
Dim textFragment As New Aspose.Pdf.Text.TextFragment
Dim xPointCord As Single
Dim yPointCord As Single
Dim AdjustedFontSize As Single
Dim FontFactor As Single
FontFactor = mFontHeightOffset
AdjustedFontSize = fontSize * mFontScaleFactor
xPointCord = sourceLeft * (72 / m_X_DPI)
yPointCord = (3300 - sourceTop) * (72 / m_Y_DPI) - AdjustedFontSize - FontFactor
textFragment.Text = szText
textFragment.TextState.FontSize = AdjustedFontSize
textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(FontName)
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White)
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black)
If bItalic Then
textFragment.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Italic
End If
If bUnderline Then
textFragment.TextState.Underline = True
End If
If bBold Then
textFragment.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold
End If
'textFragment.ZIndex = 99
textFragment.Position = New Aspose.Pdf.Text.Position(xPointCord, yPointCord)
textBuilder.AppendText(textFragment)
End Sub
`
sample PDF produced
001.pdf (1.0 MB)