Xceed Docx ReplaceText always throwing newValue cannot be null.

917 Views Asked by At

I've tried a number of different options, but no matter what I do it either won't do anything or always return newValue error.

newValue cannot be null. 

It seems I'm not the only one but it's had updates since the link below.

docX ReplaceText works incorrect

Below is my original example:-

if (sur.RequestType)
        {
            templateDoc.ReplaceText("[#1]", "x");
            templateDoc.ReplaceText("[#2]", "");
        }
        else
        {
            templateDoc.ReplaceText("[#1]", "");
            templateDoc.ReplaceText("[#2]", "x");
        }

When debugging this it would get to line 4 then jump to line 9 where it would return the newValue cannot be null error on next step.

So I tried:-

string temp1 = "temp1";

        if (sur.RequestType)
        {
            templateDoc.ReplaceText("[#1]", "x");
            templateDoc.ReplaceText("[#2]", temp1, false, RegexOptions.IgnoreCase, paraFormat, paraFormat, MatchFormattingOptions.SubsetMatch);
        }
        else
        {
            templateDoc.ReplaceText("[#1]", "x.x");
            templateDoc.ReplaceText("[#2]", "x", false, RegexOptions.IgnoreCase, paraFormat, paraFormat, MatchFormattingOptions.SubsetMatch);
        }

Along with a couple other tweaks but all returning the same error.

Prior to using ReplaceText I'd used the example from the sample project:-

templateDoc.AddCustomProperty( new CustomProperty( "CompanySlogan", "Always with you" ) );
  templateDoc.AddCustomProperty( new CustomProperty( "ClientName", "James Doh" ) );

Here it would step through each line but the produced document wouldn't have replaced anything.

Lastly more off topic but if anybody has a better solution, I'd been stuck going back and forth trying to output the file without saving it but had issues converting it from the Xceed DocX type to a HttpResponseMessage.

Below was my least favourable implementation of such as I'd either like to save it to a database or skip saving the file and just provide it directly to the user to save where they want instead of having a server side copy.

[HttpGet]
    public HttpResponseMessage DownloadRecord(int id)
    {
        SURequest sur = _sURequestsService.GetRequestData(id);
        var fullPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Content/RequestForm.docx");
        var fullPath2 = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Content/RequestFormUpdated.docx");
        var templateDoc = DocX.Load(fullPath);
        var template = CreateRequestFromTemplate(templateDoc, sur);
        template.SaveAs(fullPath2);
        //using (FileStream fs2 = new FileStream(@"~/Content/RequestFormUpdated.docx", FileMode.Create))
        //{
        //    template.SaveAs(fs2);

        //}
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

        var stream = new FileStream(fullPath2, FileMode.Open);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(fullPath2);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentLength = stream.Length;
        return result;
        //return fs2;
    }

I'm stuck with no clue how to proceed further with Xceed so am going to branch my present code and try using OpenXML to see if I have any better luck or if someone else can spot what I'm doing wrong or how to get past the issue in Xceed?

Any help would be much appreciated.

1

There are 1 best solutions below

0
Myzifer On

Turned out to be an issue with VS17 which was behaving stranging with replacetext and seemed to have cached an earlier issue in it's compiler.

This behaved like the issue was somewhere it wasn't and could only be resolved by manually stopping the compiler process.

Still no resolution for AddCustomProperty or with skipping generating a local file.

I'm going to work on trying to get it not to generate a local file but likely will need to either open a new question specific to that or setup something else to cleanup old files.