Free Support Forum - aspose.com

Aspose PDF.NET High CPU

Hi. I have a webapp where I’m trying to display txt documents and also give the user the opportunity to display them as a pdf.
When the app converts the txt file to a pdf it seems to be maxing out the cpu on the server.
I tried updating to v20.3 since it looked like that resolved a high cpu issue but unfortunately it ended up using more cpu than our current version v20.2.
Can someone provide some assistance on how we can reduce the amount of cpu this consumes?

@everbank

Could you please share the code snippet which you are using to convert the .txt file into PDF. Also, please share a sample .txt file for our reference. We will test the scenario in our environment and address it accordingly.

The below method returns a byte array that our controller sends to its return it to the File action result.

public static byte[] ConvertExportTextFileToPDF(string textFile, bool includeBackgroundBars)
{
var lines = System.IO.File.ReadAllLines(Path.Combine(textFile));
var font = new TextState(“Courier”, 10.0);
var fill = GrayBackground;
var counter = 0;
var doc = new Document();
var output = new MemoryStream();
byte[] retData;

try
{
    var margin = new MarginInfo()
    {
        Left = Margin,
        Right = Margin,
        Top = 30,
        Bottom = 30
    };

    doc.PageInfo = new PageInfo()
    {
        Margin = margin,
        Width = DocumentWidth,
        Height = DocumentHeigth
    };

    var table = BuildPdfPTable();

    doc.Pages.Add();
    counter = 0;
    var pageCounter = 1;

    foreach (var line in lines)
    {
        if(pageCounter == 31)
        {
            pageCounter = 31;
        }
        if (line == newPageMarker)
        {
            AddColorRow(table, ref counter, font, fill);

            doc.Pages[pageCounter].Paragraphs.Add(table);
            doc.Pages.Add();
            pageCounter++;
            table = BuildPdfPTable();

            counter = 0;
            fill = GrayBackground;

            continue;
        }

        if (includeBackgroundBars && counter == 3)
        {
            fill = (Equals(fill, WhiteBackground)) ? GrayBackground : WhiteBackground;
            counter = 0;
        }

        var text = line;

        if (includeBackgroundBars)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                text = " ";
            }
        }

        var row = table.Rows.Add();
        row.Cells.Add(text, font);
        row.BackgroundColor = fill;

        counter++;
    }

    AddColorRow(table, ref counter, font, fill);

    doc.Pages[pageCounter].Paragraphs.Add(table);
    
    doc.Save(output, SaveFormat.Pdf);
    
}
finally
{
    doc.Dispose();
    retData = output.ToArray();
    output.Dispose();
}

return retData;

}

private static Table BuildPdfPTable()
{
var table = new Table();
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None);
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None);
table.ColumnWidths = “100%”;
table.HorizontalAlignment = HorizontalAlignment.Center;
table.VerticalAlignment = VerticalAlignment.Top;
table.DefaultCellTextState.Font = FontRepository.FindFont(“Courier”);
table.DefaultCellTextState.FontSize = 10;

return table;

}

private static void AddColorRow(Table table, ref int counter, TextState font, Color fill)
{
for (var tableRow = table.Rows.Count; tableRow <= 63; tableRow++)
{
if (counter == 3)
{
fill = (Equals(fill, WhiteBackground)) ? GrayBackground : WhiteBackground;
counter = 0;
}

    var row = table.Rows.Add();
    row.Cells.Add("", font);
    row.BackgroundColor = fill;

    counter++;
}

}Preformatted text

@everbank

Thanks for sharing the code snippet. Could you please share the sample .txt file with us in .zip format so that we can test the scenario with same file and observe the issue that you are facing.