从Word Doc在外部文件夹Xceed.Pdf.NET中生成Pdf

问题描述

晚安研究员,

我面临着有关从Word文件读取生成pdf的问题 外部目录可以很好地阅读文档文本,但是当我在form1 form2中输入数据时,却没有将数据写入pdf。此外,我还附加了外部的屏幕截图 文件夹名称Template。现在,当我特别调用以下函数时,此行执行了 DocX.ConvertToPdf(document,myPDFFile); ,这给了我例外 对象引用未设置为对象的实例。其次,模板路径可以读取文件,但无法转储

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,List`1 externalFonts)    at Xceed.Words.NET.DocX.ConvertToPdf(Document fileToConvert,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",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",dd MMMM yyyy"));
                    _contractagreement.Add("WFirstName",applicationDto.WFirstName);
                    _contractagreement.Add("WLastName",applicationDto.WLastName);
                    _contractagreement.Add("WSubmissionDate",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,RegexOptions.IgnoreCase);
                //document.ReplaceTextWithObject("<RSignature>",RSignature,RegexOptions.IgnoreCase);
                //document.ReplaceTextWithObject("<WSignature>",WSignature,RegexOptions.IgnoreCase);

                document.ReplaceText("<(.*?)>",ReplaceFunc,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",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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...