Cannot found INCLUDEPICTURE Field Type via doc.Range.Fields

Description

I want to remove field with INCLUDEPICTURE type, but doc.Range.Fields not found this type.

Env

  • Windows 11
  • Aspose.Words for .NET 24.1

Code

void Test()
{
	var doc=new Aspose.Words.Document(@"C:\Users\Administrator\Desktop\1.docx");
	doc.Range.Fields.Clear();
	doc.Save(@"C:\Users\Administrator\Desktop\1-out.docx");
}

Files



1.docx (1.1 MB)

@xxtears By default INCLUDEPICTURE fields are loaded into Aspose.Words DOM as shapes with linked images. If it is required to load them as field, you should specify LoadOptions.PreserveIncludePictureField option:

LoadOptions opt = new LoadOptions();
opt.PreserveIncludePictureField = true;
Document doc = new Document(@"C:\Temp\in.docx", opt);

List<FieldIncludePicture> includePictureFields = doc.Range.Fields
    .Where(f => f.Type == FieldType.FieldIncludePicture)
    .Cast<FieldIncludePicture>().ToList();

Console.WriteLine(includePictureFields.Count);

Thanks a lot, but it’s not working for the other test file with INCLUDEPICTURE and LINK field type.

Code:

void Test()
{
	var loadOps = new Aspose.Words.Loading.LoadOptions();
	loadOps.PreserveIncludePictureField = true;
	var doc = new Aspose.Words.Document(@"C:\Users\Administrator\Desktop\3.docx", loadOps);
	doc.Range.Fields.Dump();
	doc.Range.Fields.Clear();
	doc.Save(@"C:\Users\Administrator\Desktop\3-out.docx", Aspose.Words.SaveFormat.Docx);
}

File

3.docx (94.5 KB)

image.png (16.4 KB)

image.png (13.8 KB)

@xxtears Internally there are no INCLUDEPICTURE field in the attached document. It is a shape. So Aspose.Words also reads it as shape:

<w:pict>
	<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
		<v:stroke joinstyle="miter" />
		<v:formulas>
			<v:f eqn="if lineDrawn pixelLineWidth 0" />
			<v:f eqn="sum @0 1 0" />
			<v:f eqn="sum 0 0 @1" />
			<v:f eqn="prod @2 1 2" />
			<v:f eqn="prod @3 21600 pixelWidth" />
			<v:f eqn="prod @3 21600 pixelHeight" />
			<v:f eqn="sum @0 0 1" />
			<v:f eqn="prod @6 1 2" />
			<v:f eqn="prod @7 21600 pixelWidth" />
			<v:f eqn="sum @8 21600 0" />
			<v:f eqn="prod @7 21600 pixelHeight" />
			<v:f eqn="sum @10 21600 0" />
		</v:formulas>
		<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
		<o:lock v:ext="edit" aspectratio="t" />
	</v:shapetype>
	<v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:150pt;height:150pt">
		<v:imagedata r:id="rId4" r:href="rId5" o:title="" />
	</v:shape>
</w:pict>

Also, there are not LINK fields, there are linked OLE objects:

<w:object>
	<v:shape id="_x0000_i1026" type="#_x0000_t75" style="width:132.6pt;height:40.8pt" o:ole="">
		<v:imagedata r:id="rId6" o:title="" />
	</v:shape>
	<o:OLEObject Type="Link" ProgID="Package" ShapeID="_x0000_i1026" DrawAspect="Content" r:id="rId7" UpdateMode="OnCall">
		<o:LinkType>Picture</o:LinkType>
		<o:FieldCodes>\f 0</o:FieldCodes>
	</o:OLEObject>
</w:object>

So, Aspose.Words reads them as shapes with linked OLE objects.

Thanks for your help, but what puzzles me is why it is a field type in the SDK, and we need to use shapes to handle it? Referring to the Office Word document, Link and IncludePicture both belong to the category of field codes. Can you tell me the real reason for this design?

@xxtears This is how MS Word stores these fields in the document. Generally fields in MS Word documents are represented by FieldStart, FieldSeparator and FieldEnd nodes. But MS Word does not store LINK and sometimes INCLUDEPICTURE fields as fields. So Aspose.Words reads them as shapes, just as they are stored in the document.
If take a look at the normal field structure in the document, it looks like this:

<w:fldSimple w:instr=" MERGEFIELD Name ">
	<w:r>
		<w:rPr>
			<w:noProof/>
		</w:rPr>
		<w:t>«Name»</w:t>
	</w:r>
</w:fldSimple>

Or like this:

<w:r>
	<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
	<w:instrText xml:space="preserve"> IF test = test "True Text" "False Text" </w:instrText>
</w:r>
<w:r>
	<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
	<w:t>True Text</w:t>
</w:r>
<w:r>
	<w:fldChar w:fldCharType="end"/>
</w:r>

Thanks a lot, are only the two types Link and INCLUDEPICTURE special?

@xxtears As I can see, yes, only these two types of fields are stored as shapes in the document.