Upgrade code from .Net Framework to Core

Please let me know how I can convert this code from .Net Framework to .Net 8.

public static byte[] ConvertToPdf(byte[] fileByte, string ext)
{
    try
    {
        byte[] archivo = null;
        using (var docViewer = new DocViewer())
        {
            var lic = new XmlDocument()
            {
                XmlResolver = null
            };

            // Configura un XmlReaderSettings con la resolución de entidades desactivada
            var settings = new XmlReaderSettings
            {
                DtdProcessing = DtdProcessing.Prohibit, // Desactiva el procesamiento de DTD
                XmlResolver = null // Configura XmlResolver en null para evitar la resolución de recursos externos
            };

            using (var reader = XmlReader.Create(new StringReader(DocumentoService.doconutlic), settings))
            {
                lic.Load(reader);
            }

            DocViewer.DoconutLicense(lic);

            var success = docViewer.OpenDocument(fileByte, ext);

            if (success.EsNullOrEmpty())
            {
                // export pdf
                archivo = docViewer.ExportToPdf();
            }
            else
            {
                throw new Exception("Error opening the document");
            }
        }
        return archivo;
    }
    catch
    {
        throw;
    }

}

@Jorge_Garces,

To update the code to use Doconut .NET 6 or higher, the following parts of the code need to be updated:

DocViewer object is now Viewer.

In the constructor three parameters must be added:

Viewer(IMemoryCache cache, IHttpContextAccessor httpContextAccessor, string licensePath)

The OpenDocument method returns the token of the file with which, if valid, you can call the ExportToPdf method.

Below I attach the complete method converted compatible with NET 8:

public byte[] ConvertToPdf(byte[] fileByte, string ext)
{
    try
    {
        byte[] archivo = null;

        var licenseFilePath = Path.Combine(_hostingEnvironment.WebRootPath, "Doconut.lic");

        using (var docViewer = new Viewer(_cache, _accessor, licenseFilePath))
        {
            var lic = new XmlDocument()
            {
                XmlResolver = null
            };

            // Configura un XmlReaderSettings con la resolución de entidades desactivada
            var settings = new XmlReaderSettings
            {
                DtdProcessing = DtdProcessing.Prohibit, // Desactiva el procesamiento de DTD
                XmlResolver = null // Configura XmlResolver en null para evitar la resolución de recursos externos
            };


            Viewer.DoconutLicense(lic);

            var token = docViewer.OpenDocument(fileByte, ext);

            if (string.IsNullOrEmpty(token))
            {
                // export pdf
                archivo = docViewer.ExportToPdf();
            }
            else
            {
                throw new Exception("Error opening the document");
            }
        }
        return archivo;
    }
    catch
    {
        throw;
    }
}

Thank you for the information, is it possible for you to include an overload in the method so that we don’t have to pass the license file path, but instead, we can provide the license as text, similar to how it was done in previous versions?

@Jorge_Garces,

I appreciate your feedback. I am happy to inform you that our development team will implement this change in the next Doconut version.

As a temporary solution, you can use the following code snippet to load the license as a string:

XmlDocument lic = new XmlDocument();
lic.LoadXml("the_license_code_goes_here"); // Specify the xml string of the .lic file here (without any line breaks)
Doconut.Viewer.DoconutLicense(lic);

If you have any further questions or need assistance, feel free to ask!