简体   繁体   中英

iText7: Unable to read license key json file

I am trying to load the license file of iText7 in my Dot Net application (C# programming language). I have given the correct path to the json file using the syntax mentioned below.

LicenseKey.LoadLicenseFile(new FileStream(@"pathtofile\iTextKey.json", FileMode.Open)); 

The error message I get is:

Data at the root level is invalid. Line 1, position 1.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at iText.License.Util.XmlWorker..ctor(Stream stream)
   at iText.License.LicenseKey.LoadLicenseFileInternal(Stream licenseIs)
   at iText.License.LicenseKey.LoadLicenseFile(Stream licenseIs)

I verified the JSON file and it looks okay to me.

Any help to resolve this issue will be of great help.

As you can read from the stack trace, it's trying to parse the file as XML.

See docs :

However, if you have a JSON license key file, see the instructions for iText 7.2.x.

Install iText 7.2.

I had the same error despite having iText v7.2.4 and iText.Licensing.Base v4.0.4 via nuget packages. Turns out my solution also had an older version of iText (through an imported library) and the LoadLicenseFile method being used was from that old library.

In other words, make sure the fully qualified method you are invoking is iText.Licensing.Base.LicenseKey.LoadLicenseFile

use itext.licensing.base nuget package. itext7.licensekey is not correct package. here is package link.. https://www.nuget.org/packages/itext.licensing.base/

here is code

using iText.Licensing.Base;

var licenseFilePath = @"C:\itext\iText7.x_license.json";
if (File.Exists(licenseFilePath))
{
    LicenseKey.LoadLicenseFile(new FileInfo(licenseFilePath));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM