Control external resource access via INCLUDETEXT

The file loading via the INCLUDETEXT field code does not appear to go through the ResourceLoadingCallback. Is there another way to intercept access?

Example
{ INCLUDETEXT “c:\\Temp\\ExternalFile.txt” \c AnsiText }

If there are more in the field engine it would be desirable to control those as well.

@bruceb6,

You can build logic on the following code to get the desired behavior:

Document doc = new Document("E:\\Temp\\inp.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

ArrayList list = new ArrayList();
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldIncludeText)
    {
        FieldIncludeText fieldIncludeText = (FieldIncludeText)field;
        list.Add(fieldIncludeText);
    }
}

foreach (FieldIncludeText fieldIncludeText in list)
{
    LoadOptions opts = new LoadOptions();
    opts.ResourceLoadingCallback = new HandleResourceLoadingCallback();
    Document fieldDoc = new Document(fieldIncludeText.SourceFullName, opts);

    builder.MoveTo(fieldIncludeText.Start);
    fieldIncludeText.Remove();

    builder.InsertDocument(fieldDoc, ImportFormatMode.KeepSourceFormatting);
}

doc.Save("E:\\Temp\\19.8.docx"); 

public class HandleResourceLoadingCallback : IResourceLoadingCallback
{
    public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
    {
        if (args.ResourceType == ResourceType.Image)
        {
                return ResourceLoadingAction.Skip;
        }

        return ResourceLoadingAction.Default;
    }
}

Ok if that’s the recommended approach. I was expecting ResourceLoadingCallback to be called in this situation though. Just need to skip any external references for security.

@bruceb6,

Yes, ResourceLoadingCallback events will be called. For example, if the INCLUDETEXT field is pointing to a HTML file and that HTML file contains an IMG tag containing an image URL, then upon processing, the above code will skip loading that image inside ResourceLoading method. Hope, this helps.