We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert picture field

Hi !

I want to add picture field into my word document, the field is insert, but when I open the document, and I clic ALT+F9, my picture don’t appear.
The field is empty, I did not empty picture with a red cross.
I use Aspose.word version 3.5.3.0

My code :

builder.InsertField(“INCLUDEPICTURE “Picture.gif” \d \*”, “”);

Thxs

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the following code:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertField("INCLUDEPICTURE \"C:\\\\Temp\\\\test.jpg\" \\d \\* MERGEFORMAT", string.Empty);

doc.Save(@"Test126\out.doc");

This works fine on my side.

Best regards.

Hi !

Thanks for your response.

This code don’t work for me. It’s always the same error

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertField(“INCLUDEPICTURE “C:\\Temp\\test.jpg” \d \* MERGEFORMAT”, string.Empty);
doc.Save(“Out.doc”);

I attached my “Out.doc” document

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please make sure that an image exists at “C:\Temp\test.jpg”.

Best regards.

Yes, this image exist, but if I clic ALT+F9 in my field, I didn’t an empty picture, It’s empty text.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. When you press Alt+F9 you will see Field codes. In your case you will see the following.

{ INCLUDEPICTURE "C:\\Temp\\test.jpg" \d \* MERGEFORMAT }

If you need to update Fields you should press Ctrl+A and then F9, then your will see included picture.

Best regards.

Yes, if I try F9, It’s good.
Now, I want to test if my Shape or InlineShape object is in a header or a footer, because I want to replace pictures only if the picture is out header and footer

shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in shapes)
{
if (shape.ImageFormat != null)
{
if (/* If shape is out header and footer /)
{
builder.MoveTo(shape);
builder.InsertField("INCLUDEPICTURE “” + LienImage + “” \d \
MERGEFORMAT", string.Empty);
ListeShapeDelete.Add(shape);
}
}
}

Thxs

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can try using the following code:

if (shape.GetAncestor(NodeType.Body) != null)

Hope this helps.

Best regards.

I try this, but I’ve got an error message

Erreur 2 Argument ‘1’ : impossible de convertir de ‘Aspose.Words.NodeType’ en ‘System.Type’ Z:\TestSuppressionImagesDocWord\ConsoleApplication1\Program.cs 56 57 ConsoleApplication1

I’ve so to try
shape.GetAncestor(NodeType.Body.GetType()) != null
but it’s never true

Thxs

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I tested this on the latest version. 3.5 version does not have overload of GetAncestor that takes NodeType as parameter. Please try using the following:

if (shape.GetAncestor(typeof(Body)) != null)

Best regards.

Yes Thanks, I think It’s good.

Now, I want to replace the first picture of the first page by a picture field.

My code :
Document doc = new Document(NameFile);
DocumentBuilder builder = new DocumentBuilder(doc);

NodeCollection shapes = null;
Shape shapeDelete = null;
InlineShape shapeInlineDelete = null;

// Search images in Shape
shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach (Shape shape in shapes)
{
// If it’s an image and into the body
if (shape.ImageFormat != null && shape.GetAncestor(typeof(Body)) != null)
{
builder.MoveTo(shape);
builder.InsertField(“INCLUDEPICTURE “” + LienImage + “” \d \* MERGEFORMAT”, string.Empty);
shapeDelete = shape;
break;
}
}

// If shapeDelete is null, we continue to search with InlineShape
if (shapeDelete == null)
{
shapes = doc.GetChildNodes(NodeType.InlineShape, true);
foreach (InlineShape shape in shapes)
{
if (shape.ImageFormat != null && shape.GetAncestor(typeof(Body)) != null)
{
builder.MoveTo(shape);
builder.InsertField(“INCLUDEPICTURE “” + LienImage + “” \d \* MERGEFORMAT”, string.Empty);
shapeInlineDelete = shape;
break;
}
}
}

// Delete picture
if (shapeDelete != null) shapeDelete.Remove();
if (shapeInlineDelete != null) shapeInlineDelete.Remove();

doc.Save(NameFile);

But if the true first picture is in InlineShape, and that we take the first picture with Shape, I haven’t got good picture to replace…

Can I do this to have picture in the good order :

shapes = doc.GetChildNodes(NodeType.Shape | NodeType.InlineShape, true);

or maybe


shapes = doc.GetChildNodes(NodeType.Shape & NodeType.InlineShape, true);


Thanks, and I hope that you understand my english language :slight_smile:

Edit : In fact, It’s good because there is only one picture into my document … Thxs !!!

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. There is no difference between Shape and InlineShape in the latest version of Aspose.Words. And the following code will get all shapes from the document.

//Get collection of Shapes

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

I think you can also achieve what you need using NextPreOrder method. Please see the following code:

//Open Document and Create DocumentBuilder

Document doc = new Document(@"Test124\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

Node curNode = doc;

while (curNode != null)

{

//move to next node

Node nextNode = curNode.NextPreOrder(doc);

//Check whether current node is Shape

if (curNode.NodeType.Equals(NodeType.Shape) && curNode.GetAncestor(typeof(Body)) != null)

{

//Check whether current node can have image

if ((curNode as Shape).CanHaveImage)

{

//move to curent note, insert text (of field) and remove current node.

builder.MoveTo(curNode);

builder.Write("[image]");

curNode.Remove();

break;

}

}

curNode = nextNode;

}

//Save output document

doc.Save(@"Test124\out.doc");

I hope this could help you.

Best regards

Hi !

I’ve got another question :
Can I know if the shape is into my first page or not ?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. No, Unfortunately There is no way to determine whether image is on the first page or not. Word documents do not contain information about document layout.

Best regards.