I have a problem with setting the image of a picture content control. It works fine if the SDT has NodeContext Block_LEVEL, but when the context is INLINE_LEVEL I can’t get the image inserted at the right position.
I use code similare to this:
// Open the document.
Document doc = new Document("C:\Temp\in.doc");
// Create DocumentBuidler object, it will help use to insert Image.
DocumentBuilder builder = new DocumentBuilder(doc);
// Get content contorl where you would like to insert Image.
// Currently there is no way to identify content controls by title or tag,
// so to demonstrate the technique I just get the first SDT
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.getChild(NodeType.StructuredDocumentTag, 0, true);
// Remove old content from the SDT
sdt.removeAllChildren();
// Now, create an empty paragraph and append it to the content control
sdt.appendChild(new Paragraph(doc));
// Move DocumentBuilder cursor to the first child of SDT (newly created paragraph)
builder.moveTo(sdt.getFirstChild());
// Insert Image
builder.InsertImage("C:\Temp\img.jpg");
// Save output
doc.Save("C:\Temp\out.doc");
This will give an error of “Cannot
insert a node of this type at this location.” on the line that inserts a new Paragraph. How can I insert an image inside the content control when the context is INLINE_LEVEL?
Attaching a docx-file that gives the error when the code above is run.
Hi Daniel,
Thanks for your inquiry. Please use the following simple code:
Document doc = new Document(filePath);
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
((Shape)sdt.FirstChild).ImageData.SetImage(Image.FromFile(MyDir + "Aspose.Words.jpg"));
doc.Save(MyDir + @"16.4.0.docx");
Hope, this helps.
Best regards,
Thanks for the response.
However your solution doesn’t quite do what I want to achieve. I have a scenario where the picture content controls can be either empty (like the example I attached in the previous post) or already populated with a picture (see the new attachement).
With the solution you posted the original image size is preserved, which leads to a stretched image if the new image has a different aspect ratios. With the code I posted the size is determined by the new image, but there’s the problem with the error.
Is there any solution that respects the size of the new image? I’ve tried modifying the code, for example by not inserting a paragraph as a child to the sdt. However I can’t figure out how to place the image inside the sdt, it always ends up outside of it.
Hi Daniel,
Thanks for your inquiry. Please try using the following workaround.
Document doc = new Document(filePath);
Image image = Image.FromFile(MyDir + "Aspose.Words.jpg");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Shape shape = (Shape)sdt.FirstChild;
shape.ImageData.SetImage(image);
shape.Width = ConvertUtil.PixelToPoint(image.Width);
shape.Height = ConvertUtil.PixelToPoint(image.Height);
doc.Save(MyDir + @"16.4.0.docx");
Hope, this helps.
Best regards,
Thanks, that did the trick!
For reference, with this solution I had to handle large images to avoid errors. This was done with the help of this code: https://docs.aspose.com/words/java/serialize-and-work-with-a-document-in-a-database/
Hi Daniel,
It is great you were able to find what you were looking for. Please let us know any time you have any further queries.
Best regards,