Hi Eric,
Tthe code is working fine, but when i open the word document after completing the process, the picture is updating perfectly but still there in the icon is showing in the picutre content control like "Click to insert a picture".
is there any way to diable that after update the image?
Thanks
LathaGokul
Any luck on getting rid of the icon that is covering the picture?
update: an icon saying "click here to insert a picture" was being displayed becuase I placed a picture content control on the document, but did not select an initial image. When you look at the xml of the document I noticed that a ws:showingPlcHdr tag was in the properties collection for the image. By removing this property (of type ShowingPlaceholder) then the placeholder icon goes away.
I've implemented the code above, but sill finding that the content controls are all changing not just the one with the tag - if (contentPictureTagName == tag.Val)?
Hi Weston
Just Try This
Below Code Replace Particular Image
Public string ApplySignature(string fileName, string imageName, string imageTagName, string SignatureName, string SignatureTag, string SignatureDesignation, string SignatureDesignationTag) { string folderPath = AppDomain.CurrentDomain.BaseDirectory + "shared-data\\"; string imagePath = AppDomain.CurrentDomain.BaseDirectory + "temp\\" + imageName; string filePath = folderPath + fileName ; ApplyContentControl(SignatureName, SignatureTag, SignatureDesignation, SignatureDesignationTag, fileName); //ApplyImage(imagePath, fileName, imageTagName, filePath); string[] results = new string[2]; string strResult = ""; try { FileInfo fInfo = new FileInfo(imagePath); using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true)) { DocumentFormat.OpenXml.Drawing.Blip blip = this.GetBlip(doc, imageTagName); if (blip != null) { // Add image and change embeded id. blip.Embed = this.AddImagePart(doc, imagePath, imageTagName); doc.Package.Flush(); results[0] = "OK"; results[1] = "0"; } } GC.Collect(); } catch (Exception ex) { results[0] = ex.Message; results[1] = "-1"; } strResult = results[0] + "|" + results[1]; GenerateValidationFile(filePath + ".log", strResult); return strResult; }
public DocumentFormat.OpenXml.Drawing.Blip GetBlip(WordprocessingDocument pDocument, string pPictureContentControlTagName) { foreach (SdtAlias alias in pDocument.MainDocumentPart.Document.Descendants<SdtAlias>().ToList()) if (alias.Val == pPictureContentControlTagName) return alias.Parent.Parent.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault(); return null; } public string AddImagePart(WordprocessingDocument pDocument, string pPicturePath, string pImagePartId) { OpenXmlPart part = pDocument.MainDocumentPart.GetPartById(pImagePartId); if (part != null) { pDocument.MainDocumentPart.DeletePart(part); } ImagePart imagePart = pDocument.MainDocumentPart.AddImagePart(GetImagePartType(pPicturePath), pImagePartId); using (FileStream stream = new FileStream(pPicturePath, FileMode.Open, FileAccess.Read)) { imagePart.FeedData(stream); } return pImagePartId; } public ImagePartType GetImagePartType(string imagePath) { ImagePartType imagePartType = ImagePartType.Bmp; switch (System.IO.Path.GetExtension(imagePath).ToUpper()) { case "BMP": imagePartType = ImagePartType.Bmp; break; case "JPEG": case "JPG": imagePartType = ImagePartType.Jpeg; break; case "PNG": imagePartType = ImagePartType.Png; break; case "GIF": imagePartType = ImagePartType.Gif; break; default: imagePartType = ImagePartType.Bmp; break; } return imagePartType; } private string GenerateValidationFile(string filePath, string Message) { string directoryPath = Path.GetDirectoryName(filePath); string finalFilename = Path.GetFileNameWithoutExtension(filePath) + ".log"; StreamWriter SW; SW = System.IO.File.CreateText(directoryPath + @"\" + finalFilename); SW.WriteLine(Message); SW.Close(); GC.Collect(); return finalFilename; }
Cheers
Jinesh