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!

@Jorge_Garces,

I am pleased to inform you that the new version of Doconut 24.5.0 is now available. This update includes the ability to configure the license in three different ways using the DoconutLicense method: an XmlDocument, a Stream, and a string that includes the license path.

If you have any questions or need further assistance, please do not hesitate to contact us.

Can you please provide the equivalent code in the new version to be able to use it from a .dll like a previous version?

After their announcement that starting from version 24.5.0, it is now possible

What I want to say is that it is necessary to have a “new DocViewer()” method without input parameters, meaning without dependency injection as the previous version had, because at this moment we have not been able to migrate to the new version.

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,

Thank you for reaching out with your question.

In .NET Core, requiring parameters in the constructor of Viewer() is obligatory. However, I have spoken with our development team, and they understand the need for a more flexible approach that aligns with the functionality you are using from previous versions.

They have decided to implement an alternative method that does not require initializing Viewer directly. Instead, the dev team will create a static method for PDF conversion, allowing you to achieve the same functionality without needing dependency injection.

We hope this solution meets your requirements and facilitates a smoother migration to the new version. If you have any further questions or need additional assistance, please feel free to reach out.