GroupDocs.Conversion - Performance Optimization Request

Hi,

I am currently facing performance issues when converting various document files to PDF and rendering them in a web browser using GroupDocs.Conversion. The process is taking anywhere from 30 seconds to 1 minute, which is significantly slower than expected.

Here is the code snippet I’m using:

public static byte[] ConvertDocumentToPdf(byte[] document)
{
SetViewerLicense();

using (MemoryStream documentStream = new MemoryStream(document))
{
    using (var pdfStream = new MemoryStream())
    {
        var converter = new GroupDocs.Conversion.Converter(() => documentStream);

        var pdfConversionOptions = converter.GetPossibleConversions()["pdf"].ConvertOptions;

        converter.Convert(() => pdfStream, pdfConversionOptions);

        return pdfStream.ToArray();
    }
}

}
I’d greatly appreciate your assistance in optimizing this process for better performance. Any insights or recommendations you can provide will be invaluable.

Thank you for your help.

1 Like

@vusal

Could you please share following details, we’ll then look into this scenario:

  • GroupDocs.Conversion API version that you are using
  • Sample/problematic file(s)
  • Do you have the license?

Secondly, please note that document conversion depends on multiple factors (e.g. clipart, tables, images in the documents).

Version: 23.10.0
Sample file:

Summary

Sample word file.docx (3.0 MB)

License: Temporary License

It appears that you’ve shared code using the GroupDocs.Conversion API, but it seems you’re applying a Viewer API license. Could you please clarify?

To address your question, I initially used the Groupdocs Viewer for the operation, and subsequently, I came across Groupdocs Conversion, which prompted me to test my code with it as well. I refrained from applying a license for Groupdocs Conversion since my documents’ page count didn’t exceed three. Surprisingly, I found that the speed of conversion was identical in both cases.

The purpose of my “ConvertDocumentToPdf” method is to convert various document types such as office, image files into PDF format. This led me to conclude that the conversion functionality better suited my needs compared to the viewer.

It represents the initial code that was originally written for use with Groupdocs Viewer:

public static byte[] ConvertDocumentToPdf(byte[] documentContent)
{
SetViewerLicense();

        using (var documentStream = new MemoryStream(documentContent))
        {
            using (Viewer viewer = new Viewer(documentStream))
            using (var pdfStream = new MemoryStream())
            using (var streamFactory = new PdfStreamFactory(pdfStream))
            {
                PdfOptimizationOptions optimizationOptions = new PdfOptimizationOptions
                {
                    OptimizeSpreadsheets = true,
                    CompressImages = true,
                    Lineriaze = true,
                    RemoveAnnotations = true,
                    RemoveFormFields = true,
                    ImageQuality = 60
                };
                PdfViewOptions pdfViewOptions = new PdfViewOptions(streamFactory)
                {
                    PdfOptimizationOptions = optimizationOptions
                };

                viewer.View(pdfViewOptions);
                return pdfStream.ToArray();
            }
        }
    }

internal class PdfStreamFactory : IFileStreamFactory, IDisposable
{
private readonly MemoryStream _stream;

    public PdfStreamFactory(MemoryStream stream)
    {
        _stream = stream;
    }

    public Stream CreateFileStream()
    {
        return _stream;
    }

    public void Dispose()
    {
        _stream.Dispose();
    }

    public void ReleaseFileStream(Stream fileStream)
    {

    }
}

@vusal
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): CONVERSIONNET-6452

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.

1 Like

@vusal

To enhance document conversion performance in GroupDocs.Conversion, consider providing a hint about the source document. Below is a code snippet demonstrating this approach:

const string source = "SampleWordFile.docx";

// Open the source document as a FileStream
using (var fs = File.OpenRead(source))
{
    // Use the Converter with a custom loading function and WordProcessingLoadOptions
    using (var converter = new Converter(() => fs, () => new WordProcessingLoadOptions()))
    {
        // Specify conversion options, e.g., PDF conversion
        var options = new PdfConvertOptions();

        // Perform the conversion and save the result
        converter.Convert("converted.pdf", options);
    }
}

By incorporating this hint into your code, you may observe a significant reduction in conversion time, typically ranging between 8-16 seconds.