/* Pinyin Tone Methods */ private void pinyintones(Func <string, List <XAttribute>, List <XElement>, XNamespace, bool, bool, List <string>, List <XElement> > function) { Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range; try { if (currentRange.Text == null) { System.Windows.Forms.MessageBox.Show("Please select some text."); } else { string rangexml = currentRange.WordOpenXML; XDocument doc = XDocument.Parse(rangexml); XNamespace w = doc.Descendants().First(x => x.Name.LocalName == "document").Name.Namespace; List <XElement> wts = doc.Descendants(w + "t").Where(x => 0 == x.Ancestors(w + "ruby").Count()).ToList(); foreach (XElement ele in wts) // w+"t" equivalent to "w:t" { if (0 == ele.Ancestors(w + "ruby").Count()) { List <XElement> properties = new List <XElement>(); foreach (XElement p in ele.Parent.Descendants(w + "rPr")) { properties.AddRange(p.Descendants()); } string text = ele.Value; List <XAttribute> attributes = ele.Attributes().ToList(); List <XElement> tree = function(text, attributes, properties, w, colorform.getTop(), colorform.getBottom(), colorform.getColors() ); ele.ReplaceWith(tree); } } currentRange.InsertXML(doc.ToString()); currentRange.Select(); } //Hack to prevent other east asian languages (See issue 5.) if (Globals.ThisAddIn.Application.ActiveDocument.Content.LanguageIDFarEast != Word.WdLanguageID.wdSimplifiedChinese) { Globals.ThisAddIn.Application.ActiveDocument.Content.LanguageIDFarEast = Word.WdLanguageID.wdSimplifiedChinese; } } catch (Exception e) { System.Windows.Forms.MessageBox.Show("There was an error(" + e.Message + "), please select less text to determine what it does not like"); } }
/* Converter Methods*/ private void tradSimpConvert(VbStrConv convert) { Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range; if (currentRange.Text == null) { System.Windows.Forms.MessageBox.Show("Please select some text."); } else { // don't use get_XML(), converts equations to pictures, use WordOpenXML currentRange.InsertXML(convertlong(currentRange.WordOpenXML, convert)); currentRange.Select(); } }
private void py2tones_Click(object sender, RibbonControlEventArgs e) { // Get the range Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection; if (currentSelection.Start == currentSelection.End) { currentSelection.MoveRight(Word.WdUnits.wdCharacter, 1, Word.WdMovementType.wdExtend); } Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range; //convert string xml = currentRange.WordOpenXML; List <hanzipytn> datalist = editForm.convert(hanzi2tghz.rubyXML2hanzistring(xml)); foreach (hanzipytn a in datalist) { a.pytn = new Regex(@"\d+").Match(a.pytn).Value; } //unconvert currentRange.InsertXML(ToneResize(hanzi2tghz.XMLhanzistring2XML(xml, editForm.unconvert(datalist)))); currentRange.Select(); }
private void done_Click(object sender, EventArgs e) { currentRange.InsertXML(hanzi2tghz.XMLhanzistring2XML(xml, unconvert(_datalist))); this.Close(); }
private void button1_Click(object sender, EventArgs e) { //Inserts a table with 300 rows using Open Xml object bookmarkName = "bkflatOpc"; if (!this.Bookmarks.Exists("bkflatOpc")) { MessageBox.Show("Please create a bookmark with name bkflatOpc"); return; } Word.Bookmark tblBookmark = this.Bookmarks.get_Item(ref bookmarkName); if (tblBookmark.Range.Tables.Count > 0) { MessageBox.Show("Table already exists.Please delete this table."); return; } this.Application.ScreenUpdating = false; string openxml = string.Empty; int incrementor = 1; //Get existing content control id , if there is any if (this.ContentControls.Count > 0) { object index = this.ContentControls.Count; Word.ContentControl cc = this.ContentControls.get_Item(ref index); contentControlSeedId = Int32.Parse(cc.ID); contentControlSeedId = contentControlSeedId + 1; } //Get stream for the range. This is the System.IO.Packaging.Package stream Stream packageStream = this.Paragraphs[1].Range.GetPackageStreamFromRange(); //Use Open Xml SDK to process it. using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(packageStream, true)) { //Insert image to Image Part ImagePart imgPart = wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg, "RImage"); System.Drawing.Image img = (System.Drawing.Image)Resources.ResourceManager.GetObject("RImage"); img.Save(imgPart.GetStream(), System.Drawing.Imaging.ImageFormat.Jpeg); //Remove all children wordDoc.MainDocumentPart.Document.Body.RemoveAllChildren(); //Generate table markup. DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table(); TableProperties properties = new TableProperties( new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "TableGrid" }, new TableWidth() { Width = 0, Type = TableWidthUnitValues.Auto }, new TableLook() { Val = "04A0" }); table.AppendChild <TableProperties>(properties); for (int i = 0; i < 301; i++) { TableRow row = new TableRow(); for (int j = 0; j < 3; j++) { incrementor++; SdtCell contentControl = GenerateSdtCell("Sample Text", incrementor + contentControlSeedId); UInt32 value = (UInt32)(incrementor + contentControlSeedId); contentControl .Descendants <Run>() .First() .AppendChild <Drawing> (GenerateDrawing("RImage", value, "RImage.jpg", 321933L, 288000L, 19050L, 17957L, 0L, 0L)); row.AppendChild <SdtCell>(contentControl); } table.AppendChild <TableRow>(row); } int bookmarkCount = this.Bookmarks.Count; table. PrependChild <BookmarkStart>(new BookmarkStart() { Name = "bkflatOpc", Id = bookmarkCount.ToString() } ); table. AppendChild <BookmarkEnd> (new BookmarkEnd() { Id = bookmarkCount.ToString() }); wordDoc.MainDocumentPart.Document .Body .AppendChild <DocumentFormat.OpenXml.Wordprocessing.Table>(table); wordDoc.MainDocumentPart.Document.Save(); //Flush the contents of the package wordDoc.Package.Flush(); //Convert back to flat opc using this in-memory package XDocument xDoc = OpcHelper.OpcToFlatOpc(wordDoc.Package); openxml = xDoc.ToString(); } this.Application.ScreenUpdating = false; Word.Range range = FindRange("bkflatOpc"); //Insert this flat opc Xml range.InsertXML(openxml, ref missing); this.Application.ScreenUpdating = true; }