Pdf Generation from Word Doc in external folder Xceed.Pdf.NET

506 Views Asked by At

GoodDay Fellows,

I am facing an issue regarding generating pdf from word file reading from external directory it reads the document text well but when i entered data into the form1form2 but it doesnot write the data into the pdf.Also i have attached a screenshot of external folder nameTemplate.Now when i call below function especially this line executed DocX.ConvertToPdf(document, myPDFFile); this gives me exception Object reference not set to an instance of an object.Secondly Template path i can read the file but i cannot dump

Xceed.Pdf.Stream.ApplyFilters()    at Xceed.Pdf.Document.Generate(IOutputStream output)    at Xceed.Pdf.Document.Generate(Stream stream)    at Xceed.Pdf.Document.Generate(String fileName)    at Xceed.Document.NET.PdfConverter.Convert()    at Xceed.Document.NET.Document.ConvertToPdfCore(Document fileToConvert, String outputFileName, List`1 externalFonts)    at Xceed.Document.NET.Document.ConvertToPdfInternal(Document fileToConvert, String outputFileName, List`1 externalFonts)    at Xceed.Words.NET.DocX.ConvertToPdf(Document fileToConvert, String outputFileName, List`1 externalFonts)    at DocuProp.Services.DocRegistrationService.DocRegistrationService.GenertaePdf(String templatePath, ApplicationEntity applicationDto)

MYCODE WEBAPI C#

public void GenertaePdf(string templatePath, ApplicationEntity applicationDto)
        {
            var applicationDocRepository = _unitOfWork.GetRepository<ApplicationDocumentsEntity>();
            ApplicationDocumentsEntity applicationDocEntity = new ApplicationDocumentsEntity();
            applicationDocEntity = applicationDocRepository.GetAll().SingleOrDefault(x => x.ApplicationId == applicationDto.ApplicationId);
            // Load a document.

   using (var document = DocX.Load(templatePath))
            {
                if (_contractagreement.Count == 0)
                {
                    _contractagreement.Add("CurrentDate", DateTime.Now.ToString("dddd, dd MMMM yyyy"));
                    _contractagreement.Add("SubmissionDate", DateTime.Now.ToString("dddd, dd MMMM yyyy"));
                    _contractagreement.Add("CFirstName", applicationDto.CFirstName);
                    _contractagreement.Add("CLastName", applicationDto.CLastName);
                    _contractagreement.Add("CMiddleName", applicationDto.CMiddleName);
                    _contractagreement.Add("SecurityNumber", applicationDto.CSecurityNumber);
                    _contractagreement.Add("CDateofBirth", applicationDto.CDateOfBirth.Value.ToString(" dd MMMM yyyy"));
                    _contractagreement.Add("CMaritalStatus", applicationDto.CMaritalStatus);
                    _contractagreement.Add("CPhoneNumber", applicationDto.CPhoneNumber);
                    _contractagreement.Add("CAddressLine1", applicationDto.CAddressLine1);
                    _contractagreement.Add("CAddressLine2", applicationDto.CAddressLine2);
                    _contractagreement.Add("CCity", applicationDto.CCity);
                    _contractagreement.Add("CState", applicationDto.CState);
                    _contractagreement.Add("CPostalCode", applicationDto.CPostalCode);

                    _contractagreement.Add("RFirstName", applicationDto.RFirstName);
                    _contractagreement.Add("RLastName", applicationDto.RLastName);
                    _contractagreement.Add("RState", applicationDto.RState);
                    _contractagreement.Add("RCity", applicationDto.RCity);
                    _contractagreement.Add("RCompany", applicationDto.RCompany);
                    _contractagreement.Add("RSubmissionDate", DateTime.Now.ToString("dddd, dd MMMM yyyy"));
                    _contractagreement.Add("WFirstName", applicationDto.WFirstName);
                    _contractagreement.Add("WLastName", applicationDto.WLastName);
                    _contractagreement.Add("WSubmissionDate", DateTime.Now.ToString("dddd, dd MMMM yyyy"));
                }



                var Picture = ConverBase64ToImage(document, applicationDto.CIdPicture.Split(",")[1]);
                var RSignature = ConverBase64ToImage(document, applicationDto.RSignature.Split(",")[1]);
                var CSignature = ConverBase64ToImage(document, applicationDto.CSignature.Split(",")[1]);
                var WSignature = ConverBase64ToImage(document, applicationDto.WSignature.Split(",")[1]);
                //document.ReplaceTextWithObject("<Picture>", Picture, false, RegexOptions.IgnoreCase);
                //document.ReplaceTextWithObject("<CSignature>", CSignature, false, RegexOptions.IgnoreCase);
                //document.ReplaceTextWithObject("<RSignature>", RSignature, false, RegexOptions.IgnoreCase);
                //document.ReplaceTextWithObject("<WSignature>", WSignature, false, RegexOptions.IgnoreCase);

                document.ReplaceText("<(.*?)>", ReplaceFunc, false, RegexOptions.IgnoreCase, new Formatting() { Bold = true, FontColor = System.Drawing.Color.Green });

// var myPDFFile = Path.Combine(DocumentSampleResourcesDirectory, string.Format("MyFile-{0}.pdf", applicationDto.ApplicationId));
                var myPDFFile = Path.Combine(DocumentSampleResourcesDirectory, string.Format("{1}-{0}.pdf" , Guid.NewGuid(), applicationDto.ApplicationId));
                //var myPDFFile = string.Format("{1}-{0}.pdf", Guid.NewGuid(), applicationDto.ApplicationId);
                WriteErrorMsg("myPDFFile= "+ myPDFFile);

                try
                {
                    DocX.ConvertToPdf(document, myPDFFile);
                }
                catch(Exception e)
                {
                    WriteErrorMsg(e.StackTrace);
                }
                Byte[] bytes = File.ReadAllBytes(myPDFFile);
                String pdfInBase64 = Convert.ToBase64String(bytes);


                File.Delete(myPDFFile);

                if (templatePath == ContractAgreement)
                   applicationDocEntity.Contract = pdfInBase64;


                if (templatePath == Header)
                    applicationDocEntity.Header = pdfInBase64;
                if (templatePath == PowerOfattorney)
                    applicationDocEntity.PowerOfAttorney = pdfInBase64;
                if (templatePath == DebitCardAuthorization)
                    applicationDocEntity.DebitAuthorization = pdfInBase64;

                applicationDocRepository.Update(applicationDocEntity);
                
                _unitOfWork.Commit();
                document.Save();
            }
        }

enter image description here

0

There are 0 best solutions below