Major performance issue in latest Aspose PDF version 25.06 compared to version 24.02

Hi Team,

We recently updated Aspose.Pdf from version 24.02 to 25.06 in our .NET Framework application. After the update, we observed a significant performance degradation when generating bigger documents that include header and footer. If we remove header footer, there is no difference in the performance.

The document generation time has increased by 5–6 times in Aspose.Pdf version 25.06 compared to version 24.02. The provided code below can be used to reproduce this issue. Please generate the same document using both versions and compare the time taken to observe the performance difference.

.Net Vesrsion: .Net Framework 4.6.2
Aspose PDF version working: 24.02
Asppose PDF version with performance issue: 25.06

using Aspose.Pdf;
using Aspose.Pdf.Annotations;Preformatted text
using Aspose.Pdf.Optimization;
using Aspose.Pdf.Text;
using System;
using System.IO;

namespace Aspos_HTML_SamplApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            AsposeLicenseStream.SetLicense2504();

            // Create PDF document
            var document = new Aspose.Pdf.Document();
            var page = document.Pages.Add();
            page.SetPageSize(590, 850);
            var marginInfo = new MarginInfo(42, 100,
                    35, 100);
            page.PageInfo.Margin = marginInfo;

            // Add page
            for (int i = 0; i < 500; i++)
            {
                for (int k = 0; k < 10; k++)
                {
                    // Add table
                    var table = GetTable();

                    AddData(table, true, "header");
                    for (int j = 0; j < 20; j++)
                    {
                        AddData(table, false, "value");
                    }

                    page.Paragraphs.Add(table);
                }
            }
            AddHeaderAndFooterAsText(document);
            using (var stream = new MemoryStream())
            {
                var optimizationStrategy = new OptimizationOptions()
                {
                    RemoveUnusedObjects = true,
                    RemoveUnusedStreams = true,
                    ImageCompressionOptions =
                    {
                        CompressImages = true,
                        ImageQuality = 50,
                        Version = ImageCompressionVersion.Standard
                    },
                    ImageEncoding = ImageEncoding.Flate,
                    AllowReusePageContent = true,
                };
                document.OptimizeResources(optimizationStrategy);
                document.Save(stream);
                document = new Document(stream);
                document.Save("C:\\Temp\\BiggerDocument25_04.pdf");
            }
        }

        private static void AddHeaderAndFooterAsText(Document document)
        {
            for (var i = 1; i <= document.Pages.Count; i++)
            {
                var table = GetTable();

                // Create header
                var header = new Aspose.Pdf.HeaderFooter();
                header.Paragraphs.Add(table);
                AddData(table, false, "header footer");

                // Create footer 
                var footer = new Aspose.Pdf.HeaderFooter();
                footer.Paragraphs.Add(table);
                header.Margin = new Aspose.Pdf.MarginInfo
                {
                    Left = 31,
                    Top = 20
                };
                footer.Margin = new Aspose.Pdf.MarginInfo
                {
                    Left = 50,
                    Top = 20
                };
                document.Pages[i].Header = header;
                document.Pages[i].Footer = footer;
            }

            using (var stream = new MemoryStream())
            {
                document.Save(stream);
            }
        }

        private static Table GetTable()
        {
            var table = new Aspose.Pdf.Table
            {
                ColumnWidths = "100 100 100 100",
                Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Box, 1f, Aspose.Pdf.Color.DarkSlateGray),
                DefaultCellBorder =
                    new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Box, 0.5f, Aspose.Pdf.Color.Black),
                DefaultCellPadding = new Aspose.Pdf.MarginInfo(4.5, 4.5, 4.5, 4.5),
                Margin =
                {
                    Bottom = 10
                },
                DefaultCellTextState =
                {
                    FontSize = 10,
                    Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial")
                }
            };
            return table;
        }
        private static void AddData(Table table, bool isHeader, string text)
        {
            var row = table.Rows.Add();
            for (int i = 0; i < 5; i++)
            {
                var cell = new Cell();
                if (isHeader)
                    cell.BackgroundColor = Color.Gray;

                var textFragment = new TextFragment(text);
                var textState = new TextState(System.Drawing.Color.Black, 7);
                textState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
                textFragment.TextState.ApplyChangesFrom(textState);
                textFragment.TextState.ForegroundColor = Color.Black;

                cell.Paragraphs.Add(textFragment);

                row.Cells.Add(cell);
            }
        }
    }
}

@SSKEurofins

Cause

The performance degradation you’re experiencing after upgrading from Aspose.Pdf version 24.02 to 25.06, particularly when generating larger documents with headers and footers, is likely due to changes in the internal processing of these elements in the newer version. Updates can introduce optimizations that may inadvertently affect performance in specific scenarios.

Solution

To address this issue, consider the following steps:

  1. Profile the Code: Use a profiling tool to identify which parts of the document generation process are taking the most time. This can help pinpoint whether the headers, footers, or table generation is the bottleneck.

  2. Optimize Header/Footer Handling: Instead of creating new header and footer objects for each page, reuse the same header/footer instance across pages if the content is identical. This can significantly reduce overhead.

  3. Batch Processing: If possible, batch process the addition of tables and other elements to minimize the number of operations performed on the document.

  4. Test with Different Versions: If feasible, test with version 25.05 or other versions to see if the performance issue persists. This can help determine if the problem is specific to version 25.06.

Code

Here’s a modified version of your AddHeaderAndFooterAsText method that reuses header and footer instances:

private static void AddHeaderAndFooterAsText(Document document)
{
    var header = new Aspose.Pdf.HeaderFooter();
    var footer = new Aspose.Pdf.HeaderFooter();

    // Create header content
    var headerTable = GetTable();
    AddData(headerTable, false, "header footer");
    header.Paragraphs.Add(headerTable);
    
    // Create footer content
    var footerTable = GetTable();
    AddData(footerTable, false, "footer content");
    footer.Paragraphs.Add(footerTable);

    for (var i = 1; i <= document.Pages.Count; i++)
    {
        document.Pages[i].Header = header;
        document.Pages[i].Footer = footer;
    }
}

Next Steps

  1. Implement the suggested optimizations and test the performance again.
  2. If the issue persists, reach out to Aspose support with your profiling results for further assistance.
  3. Monitor release notes for future versions, as performance improvements may be addressed in upcoming updates.

Please let me know if you need any further assistance!

Hi Team,

I have implemented all the suggested solutions, but none have resolved the issue. The problem can be easily reproduced using the provided example. Additionally, the same issue persists in the latest versions, including 25.04 and 25.05.

@SSKEurofins

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60218

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.