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