Remove <<link>> tag if met conditions from code

Hello everyone.

I have put on a word the following tags for putting links from an object:

<<link [urlInfo.url1] [urlInfo.descriptionUrl1]>>
<<link [urlInfo.url2] [urlInfo.descriptionUrl2]>>

since sometimes urlInfo.url1 or url2 might be empty, is there a way to remove those tags if needed?

I cannot put the <<if>> tag, since I have a lot of those documents and modify them is impossible.

Thank you!

@RiccardoDigital Url is a mandatory field so it cannot be empty. In your case, you can use some dummy value for empty url and then remove hyperlink fields with such url. For example see the following code:

string json = "{ \"url\":\"empty\", \"description\":\"empty\"  }";
JsonDataSource data = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(json)));

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<link [url] [description]>>");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, data);

// remove hyperlinks with url set to "empty".
doc.Range.Fields.Where(f => f.Type == FieldType.FieldHyperlink).Cast<FieldHyperlink>()
    .Where(hl => hl.Address == null && hl.SubAddress == "empty").ToList()
    .ForEach(f => f.Remove());

doc.Save(@"C:\Temp\out.docx");

on the document postprocessing all hyperlink field with "empty" sub address and null address are removed.

1 Like