My Project
ThisAddIn.cs
查看本檔案說明文件.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml.Linq;
6 using Word = Microsoft.Office.Interop.Word;
7 using Office = Microsoft.Office.Core;
8 using Microsoft.Office.Tools.Word;
9 using Microsoft.Office.Interop.Word;
10 using System.IO;
11 using System.Drawing;
12 //using Chem4Word.Core;
13 using Microsoft.Office.Core;
14 using System.Diagnostics;
15 
16 using System.Collections;
17 
18 namespace violet
19 {
20  public partial class ThisAddIn
21  {
22  private void ThisAddIn_Startup(object sender, System.EventArgs e)
23  {
24 
25  }
26 
27  private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
28  {
29  }
30 
31  #region VSTO 產生的程式碼
32 
37  private void InternalStartup()
38  {
39  this.Startup += new System.EventHandler(ThisAddIn_Startup);
40  this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
41  }
42 
43  #endregion
44 
45  bool doonce = false;
46 
47 
48  //程式進入點
49  protected override object RequestService(Guid serviceGuid)
50  {
51  if ( !doonce)
52  {
53  Application.DocumentBeforeSave += Application_DocumentBeforeSave;
54  // Application.WindowSelectionChange += Application_WindowSelectionChange;
55  Application.WindowBeforeDoubleClick += Application_WindowBeforeDoubleClick;
56  doonce = true;
57  }
58 
59  return base.RequestService(serviceGuid);
60  }
61 
62  void Application_WindowBeforeDoubleClick(Selection sel, ref bool Cancel)
63  {
64 
65  int n = sel.ContentControls.Count;
66  Microsoft.Office.Interop.Word.ContentControl cp = sel.Range.ParentContentControl;
67 
68  String mytitle = null;
69  String sid = null;
70  if (cp != null)
71  {
72  mytitle = cp.Title;
73  sid = cp.ID;
74 
75 
76  if (mytitle == "violet")
77  {
78  Debug.WriteLine("control selected " + sid);
79  string xid = cp.Tag;
80 
81  Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
82  CustomXMLPart xmlpart = vstoDocument.CustomXMLParts.SelectByID(xid);
83  string xml = xmlpart.XML;
84 
85 
87 
88  Globals.Ribbons.Ribbon1.RibbonUI.ActivateTabMso("TabAddIns");
89 
90  }
91  }
92 
93 
94 
95  //throw new NotImplementedException();
96  }
97 
98  void Application_DocumentBeforeSave(Word.Document doc, ref bool SaveAsUI, ref bool Cancel)
99  {
100  _Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument) as _Document;
101  //List<ControlProperties> savedControls = new List<ControlProperties>();
102  // Chem4Word.Core.ControlProperties[] controls =new Chem4Word.Core.ControlProperties[100];
103  // Chem4Word.Core.ControlsStorage.Store(vstoDocument, controls);
104  //throw new NotImplementedException();
105 
106 
107 
108  //savedControls.Sort(new ControlCollectionComparer());
109  //ControlsStorage.Store(doc, savedControls.ToArray());
110 
111  }
112 
113 
114  internal void AddPictureContentControl(Utility _utility)
115  {
116  Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
117  Microsoft.Office.Interop.Word.Selection selection = this.Application.Selection;
118  if (selection != null && selection.Range != null)
119  {
120  WdSelectionType sel = selection.Type; // inlineshape
121 
122 
123 
124  // if (sel == WdSelectionType.wdSelectionInlineShape)
125  {
126  // InlineShapes shape = vstoDocument.InlineShapes;
127  //shape[0].
128  MemoryStream ms = new MemoryStream(_utility.BitmapBytes);
129 
130 
131 
132 
133  Image _drawnimage = Image.FromStream(ms);
134 
135  //_drawnimage.Save("temp.jpg");
136  // Word.ContentControl contentControl = Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTitle(_utility.TagName)[0];
137  //foreach (Word.ContentControl contentControl in vstoDocument.Content.ContentControls)
138  {
139  // WdContentControlType type = contentControl.Type;
140  //if (type == WdContentControlType.wdContentControlPicture && contentControl.Tag.Equals(_utility.TagName))
141  {
142 
143  //object missing = Type.Missing;
144  //Microsoft.Office.Interop.Word.ContentControl contentControl = vstoDocument.ContentControls.Add(WdContentControlType.wdContentControlPicture,
145  // ref missing);
146  //contentControl.Range.InlineShapes.AddPicture("temp.jpg", ref missing, ref missing,
147  // ref missing);
148 
149 
150  //contentControl.Title = "violet";
151  Microsoft.Office.Interop.Word.ContentControl cp2 = selection.Range.ParentContentControl;
152 
153  if (cp2!= null )//update
154  {
155 
156 
157 
158  CustomXMLPart xmlpart = vstoDocument.CustomXMLParts.SelectByID(cp2.Tag);
159  xmlpart.Delete();
160  cp2.Delete();
161 
162 
163 
164 
165 
166  }
167 
168 
169  CustomXMLPart cp = vstoDocument.CustomXMLParts.Add(_utility.xml);
170 
171 
172  PictureContentControl piccontrol = vstoDocument.Controls.AddPictureContentControl(selection.Range, Guid.NewGuid().ToString());
173  piccontrol.Image = _drawnimage;// ScaleImage(_drawnimage, 200, 150);//Save(new Bitmap(returnImage), 270, 180, 0);
174  piccontrol.Title = "violet";
175  piccontrol.Tag = cp.Id;
176 
177  }
178 
179  }
180 
181  // vstoDocument.Save();
182  ms.Flush();
183  ms.Close();
184  }
185  }
186 
187 
188  }
189 
190  public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
191  {
192  var ratioX = (double)maxWidth / image.Width;
193  var ratioY = (double)maxHeight / image.Height;
194  var ratio = Math.Min(ratioX, ratioY);
195 
196  var newWidth = (int)(image.Width * ratio);
197  var newHeight = (int)(image.Height * ratio);
198 
199  var newImage = new Bitmap(newWidth, newHeight);
200  Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
201  return newImage;
202  }
203  }
204 }
Microsoft.Office.Interop.Word Word
Definition: ThisAddIn.cs:6
internal void AddPictureContentControl(Utility _utility)
Definition: ThisAddIn.cs:114
static Image ScaleImage(Image image, int maxWidth, int maxHeight)
Definition: ThisAddIn.cs:190
void ThisAddIn_Startup(object sender, System.EventArgs e)
Definition: ThisAddIn.cs:22
static internal ThisRibbonCollection Ribbons
void Application_DocumentBeforeSave(Word.Document doc, ref bool SaveAsUI, ref bool Cancel)
Definition: ThisAddIn.cs:98
internal Microsoft.Office.Interop.Word.Application Application
override object RequestService(Guid serviceGuid)
Definition: ThisAddIn.cs:49
byte [] BitmapBytes
Definition: Utility.cs:14
使UNIX上開發的C程式移植到windows上 /summary>
Definition: Form1.cs:19
static internal global::Microsoft.Office.Tools.Word.ApplicationFactory Factory
void Application_WindowBeforeDoubleClick(Selection sel, ref bool Cancel)
Definition: ThisAddIn.cs:62
string xml
Definition: Utility.cs:21
void initPath(string xml)
載入圖片
Definition: Ribbon1.cs:265
Microsoft.Office.Core Office
Definition: ThisAddIn.cs:7
void InternalStartup()
此為設計工具支援所需的方法 - 請勿使用程式碼編輯器 修改這個方法的內容。
Definition: ThisAddIn.cs:37
void ThisAddIn_Shutdown(object sender, System.EventArgs e)
Definition: ThisAddIn.cs:27