We're sorry GroupDocs doesn't work properply without JavaScript enabled.

Free Support Forum - groupdocs.com

Unable to add signatures in document using .NET Signature API

Hello all,
I am facing one issue with groupdocs signature,

when I add any signature and try to save, I am getting error of

Object reference not set to an instance of an object.

I am getting this error on the below switch case line

case "Portable Document Format":
                signsCollection.Add(signer.SignPdf());
                break;

of the below method

 private static void AddSignOptions(string documentType, List<SignOptions> signsCollection, BaseSigner signer)
    {
        switch (documentType)
        {
            case "Portable Document Format":
                signsCollection.Add(signer.SignPdf());
                break;
            case "Microsoft Word":
                signsCollection.Add(signer.SignWord());
                break;
            case "Microsoft PowerPoint":
                signsCollection.Add(signer.SignSlides());
                break;
            case "image":
                signsCollection.Add(signer.SignImage());
                break;
            case "Microsoft Excel":
                signsCollection.Add(signer.SignCells());
                break;
        }
    }

below is my complete code for the same

[HttpPost]
    [Route("downloadSigned")]
    public HttpResponseMessage DownloadSigned(SignaturePostedDataEntity signDocumentRequest)
    {
        SignatureDataEntity[] signaturesData = signDocumentRequest.signaturesData;
        if (signaturesData == null || signaturesData.Count() == 0)
        {
            // set exception message
            return this.Request.CreateResponse(HttpStatusCode.OK, new Resource().GenerateException(new ArgumentNullException("Signature data is empty")));
        }

        // get document path
        string documentGuid = signDocumentRequest.guid;
        string[] strDocumentGuid = documentGuid.Split('.');
        string newGUID = Guid.NewGuid().ToString();//strDocumentGuid[0]; 
        string format = strDocumentGuid[strDocumentGuid.Length - 1];
        string fileName = Path.GetFileName(documentGuid);
        try
        {
            Stream inputStream = this.SignByStream(signDocumentRequest);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(inputStream);
            // add file into the response
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(fileName);
            var fileBytesResponse = ReadFully(inputStream);

            string fullPath = Path.Combine(DocPro.DMS.WebApp.GD.Utilities.StoragePath, newGUID + "." + format);
            File.WriteAllBytes(fullPath, fileBytesResponse);
            FileInfo fi = new FileInfo(fullPath);
            DocPro.DMS.WebApp.FileServer.ServiceClient fs = new DocPro.DMS.WebApp.FileServer.ServiceClient();
            DocPro.DMS.WebApp.FileServer.RemoteFileInfo uploadRequestInfo = new DocPro.DMS.WebApp.FileServer.RemoteFileInfo();
            using (System.IO.FileStream stream = new System.IO.FileStream(fi.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                uploadRequestInfo.FileServerId = newGUID;
                uploadRequestInfo.FileName = "";
                uploadRequestInfo.FileExtension = "";
                uploadRequestInfo.Length = fi.Length;
                uploadRequestInfo.FileByteStream = stream;
                fs.UploadFileStream(uploadRequestInfo.FileExtension, uploadRequestInfo.FileName, uploadRequestInfo.FileServerId, uploadRequestInfo.Length, stream);
            }
            DocPro.DMS.BusinessLayer.IDocument.IDocumentExp a = (DocPro.DMS.BusinessLayer.IDocument.IDocumentExp)DALFinder.GetInstance(typeof(DocPro.DMS.BusinessLayer.IDocument.IDocumentExp));
            var response2 = a.DocumentSignatureAdd(newGUID, signDocumentRequest.FileVersionId, signDocumentRequest.UserId);
            HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(inputStream),
            };

            // add file into the response
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = Path.GetFileName(fileName),
            };
            return httpResponse;
        }
        catch (System.Exception ex)
        {
            // set exception message
            return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new Resource().GenerateException(ex));
        }
    }

private Stream SignByStream(SignaturePostedDataEntity postedData)
    {
        try
        {
            // get/set parameters
            string documentGuid = postedData.guid;
            string password = postedData.password;
            SignatureDataEntity[] signaturesData = postedData.signaturesData;
            List<SignOptions> signsCollection = new List<SignOptions>();

            // check if document type is image
            if (this.supportedImageFormats.Contains(Path.GetExtension(documentGuid)))
            {
                postedData.documentType = "image";
            }

            // set signature password if required
            for (int i = 0; i < signaturesData.Length; i++)
            {
                switch (signaturesData[i].SignatureType)
                {
                    case "image":
                    case "hand":
                        SignImage(postedData.documentType, signaturesData[i], signsCollection);
                        break;
                    case "stamp":
                        this.SignStamp(postedData.documentType, signaturesData[i], signsCollection);
                        break;
                    case "qrCode":
                    case "barCode":
                        this.SignOptical(postedData.documentType, signaturesData[i], signsCollection);
                        break;
                    case "text":
                        this.SignText(postedData.documentType, signaturesData[i], signsCollection);
                        break;
                    case "digital":
                        SignDigital(postedData.documentType, password, signaturesData[i], signsCollection);
                        break;
                    default:
                        SignImage(postedData.documentType, signaturesData[i], signsCollection);
                        break;
                }
            }

            // return loaded page object
            string documentPath = GetDocumentPath(documentGuid);
            Stream signedDocument = SignDocumentStream(documentPath, password, signsCollection);

            // return loaded page object
            return signedDocument;
        }
        catch (System.Exception e)
        {
            throw new InvalidOperationException(e.Message);
        }
    }

private void SignText(string documentType, SignatureDataEntity signaturesData, List<SignOptions> signsCollection)
    {
        string xmlPath = this.directoryUtils.DataDirectory.TextDirectory.XmlPath;
        try
        {
            // get xml data of the Text signature
            string xmlFileName = Path.GetFileNameWithoutExtension(signaturesData.SignatureGuid);

            // Load xml data
            TextXmlEntity textData = LoadXmlData<TextXmlEntity>(xmlPath, xmlFileName);

            // initiate QRCode signer object
            TextSigner signer = new TextSigner(textData, signaturesData);

            // prepare signing options and sign document
            AddSignOptions(documentType, signsCollection, signer);
        }
        catch (System.Exception e)
        {
            throw new InvalidOperationException(e.Message);
        }
    }

private static void AddSignOptions(string documentType, List<SignOptions> signsCollection, BaseSigner signer)
    {
        switch (documentType)
        {
            case "Portable Document Format":
                signsCollection.Add(signer.SignPdf());
                break;
            case "Microsoft Word":
                signsCollection.Add(signer.SignWord());
                break;
            case "Microsoft PowerPoint":
                signsCollection.Add(signer.SignSlides());
                break;
            case "image":
                signsCollection.Add(signer.SignImage());
                break;
            case "Microsoft Excel":
                signsCollection.Add(signer.SignCells());
                break;
        }
    }

and the problem is that this is working fine in the example I had downloaded but in the example the front end code used angular

@Niteen_Jadhav

Please share following details and we’ll look into this scenario:

  1. API version that you are using (e.g. 21.1)
  2. Which front-end application you are using?
  3. If you are facing this issue in your own UI application, could you please also share that?
  4. Problematic file

Hello @Atir_Tahir this issue has been resolved,
Thank You.

1 Like

@Niteen_Jadhav

You are welcome.