private void ManuallySetImage(Document doc, ContentControl cc, Title title) { cc.LockContents = false; PictureContentControl pcc = null; foreach (var obj in doc.Controls) { if (obj is PictureContentControl) { var tempPcc = obj as PictureContentControl; if (tempPcc.ID == cc.ID) { pcc = tempPcc; break; } } } if (pcc == null) { pcc = doc.Controls.AddPictureContentControl(cc, cc.ID); } var newImage = GetBitmapFromUrl(title.BoxArt.LargeUrl); if (newImage != null) { pcc.Image = newImage; pcc.InnerObject.Range.InlineShapes[1].LockAspectRatio = MsoTriState.msoTrue; pcc.InnerObject.Range.InlineShapes[1].Width = 128F; } }
private void WriteControl(string _controlName, byte[] _value) { try { if (document.Controls.IndexOf(_controlName) >= 0) { Type type = document.Controls[_controlName].GetType(); //if (type == typeof(PictureContentControl)) if (type.Name == "PictureContentControlImpl") { PictureContentControl pcc = (PictureContentControl)document.Controls[_controlName]; Image img = Image.FromStream(new MemoryStream(_value)); pcc.Image = new Bitmap(img); } } } catch (Exception err) { MessageBox.Show(err.Message, $"{err.Source}.{err.TargetSite.Name}", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
internal void AddPictureContentControl(Utility _utility) { Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument); Microsoft.Office.Interop.Word.Selection selection = this.Application.Selection; if (selection != null && selection.Range != null) { WdSelectionType sel = selection.Type; // inlineshape // if (sel == WdSelectionType.wdSelectionInlineShape) { // InlineShapes shape = vstoDocument.InlineShapes; //shape[0]. MemoryStream ms = new MemoryStream(_utility.BitmapBytes); Image _drawnimage = Image.FromStream(ms); //_drawnimage.Save("temp.jpg"); // Word.ContentControl contentControl = Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTitle(_utility.TagName)[0]; //foreach (Word.ContentControl contentControl in vstoDocument.Content.ContentControls) { // WdContentControlType type = contentControl.Type; //if (type == WdContentControlType.wdContentControlPicture && contentControl.Tag.Equals(_utility.TagName)) { //object missing = Type.Missing; //Microsoft.Office.Interop.Word.ContentControl contentControl = vstoDocument.ContentControls.Add(WdContentControlType.wdContentControlPicture, // ref missing); //contentControl.Range.InlineShapes.AddPicture("temp.jpg", ref missing, ref missing, // ref missing); //contentControl.Title = "violet"; Microsoft.Office.Interop.Word.ContentControl cp2 = selection.Range.ParentContentControl; if (cp2 != null)//update { CustomXMLPart xmlpart = vstoDocument.CustomXMLParts.SelectByID(cp2.Tag); xmlpart.Delete(); cp2.Delete(); } CustomXMLPart cp = vstoDocument.CustomXMLParts.Add(_utility.xml); PictureContentControl piccontrol = vstoDocument.Controls.AddPictureContentControl(selection.Range, Guid.NewGuid().ToString()); piccontrol.Image = _drawnimage;// ScaleImage(_drawnimage, 200, 150);//Save(new Bitmap(returnImage), 270, 180, 0); piccontrol.Title = "violet"; piccontrol.Tag = cp.Id; } } // vstoDocument.Save(); ms.Flush(); ms.Close(); } } }