public void SaveCustomPart(XMLParts partType, string text, bool isCreatingNewPart) { Debug.Assert(!_isReadOnly, "File is ReadOnly!"); if (_isReadOnly) { return; } OfficePart targetPart = RetrieveCustomPart(partType); if (targetPart == null) { if (isCreatingNewPart) { targetPart = this.CreateCustomPart(partType); } else { return; } } Debug.Assert(targetPart != null); targetPart.Save(text); _isDirty = true; }
public void RemoveCustomPart(XMLParts partType) { Debug.Assert(!_isReadOnly, "File is ReadOnly!"); if (_isReadOnly) { return; } for (int i = _xmlParts.Count - 1; i >= 0; i--) { if (_xmlParts[i].PartType != partType) { continue; } OfficePart part = _xmlParts[i]; part.Remove(); part = null; _xmlParts.RemoveAt(i); _package.Flush(); _isDirty = true; } }
private void tvDocument_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.CancelEdit || e.Label == null || e.Label.Length == 0) { return; } if (e.Node.Text == e.Label) { return; } OfficePart part = this.package.RetrieveCustomPart((XMLParts)e.Node.Tag); Debug.Assert(part != null); if (part == null) { return; } try { part.ChangeImageId(e.Node.Text, e.Label); this.package.IsDirty = true; } catch (Exception ex) { ShowError(ex.Message); e.CancelEdit = true; } }
private void insertIcons_FileOk(object sender, CancelEventArgs e) { XMLParts partType = (XMLParts)(sender as OpenFileDialog).Tag; OfficePart part = this.package.RetrieveCustomPart(partType); Debug.Assert(part != null); TreeNode partNode = null; foreach (TreeNode node in tvDocument.Nodes[0].Nodes) { if (node.Text == part.Name) { partNode = node; break; } } Debug.Assert(partNode != null); tvDocument.SuspendLayout(); foreach (string fileName in (sender as OpenFileDialog).FileNames) { try { string id = XmlConvert.EncodeName(Path.GetFileNameWithoutExtension(fileName)); Stream imageStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream, true, true); // The file is a valid image at this point. id = part.AddImage(fileName, id); Debug.Assert(id != null, "Cannot create image part."); if (id == null) { continue; } imageStream.Close(); TreeNode imageNode = new TreeNode(id); imageNode.ImageKey = "_" + id; imageNode.SelectedImageKey = imageNode.ImageKey; imageNode.ContextMenuStrip = imageContextMenu; imageNode.Tag = partType; tvDocument.ImageList.Images.Add(imageNode.ImageKey, image); partNode.Nodes.Add(imageNode); this.package.IsDirty = true; } catch (Exception ex) { ShowError(ex.Message); continue; } } tvDocument.ResumeLayout(); }
public OfficePart CreateCustomPart(XMLParts partType) { string relativePath; string relType; switch (partType) { case XMLParts.RibbonX12: relativePath = "/customUI/customUI.xml"; relType = CustomUIPartRelType; break; case XMLParts.RibbonX14: relativePath = "/customUI/customUI14.xml"; relType = CustomUI14PartRelType; break; case XMLParts.QAT12: relativePath = "/customUI/qat.xml"; relType = QATPartRelType; break; default: Debug.Assert(false, "Unknown type"); return(null); } Uri customUIUri = new Uri(relativePath, UriKind.Relative); PackageRelationship relationship = _package.CreateRelationship(customUIUri, TargetMode.Internal, relType); OfficePart part = null; if (!_package.PartExists(customUIUri)) { part = new OfficePart(_package.CreatePart(customUIUri, "application/xml"), partType, relationship.Id); } else { part = new OfficePart(_package.GetPart(customUIUri), partType, relationship.Id); } Debug.Assert(part != null, "Fail to create custom part."); _xmlParts.Add(part); _isDirty = true; return(part); }
private void removeImage_Click(object sender, EventArgs e) { TreeNode currentNode = this.tvDocument.Tag as TreeNode; Debug.Assert(currentNode != null); if (currentNode == null) { return; } OfficePart part = this.package.RetrieveCustomPart((XMLParts)currentNode.Tag); Debug.Assert(part != null); part.RemoveImage(currentNode.Text); currentNode.Remove(); this.package.IsDirty = true; }
private TreeNode ConstructPartNode(OfficePart part) { TreeNode node = new TreeNode(part.Name); node.Tag = part.PartType; node.ImageKey = OfficeApplications.XML.ToString(); node.SelectedImageKey = node.ImageKey; if (!this.package.ReadOnly) { node.ContextMenuStrip = partContextMenu; } List <TreeNode> images = part.GetImages(this.tvDocument.ImageList, this.package.ReadOnly ? null : imageContextMenu); if (images != null && images.Count > 0) { node.Nodes.AddRange(images.ToArray()); node.Collapse(); } return(node); }
private string AddImageHelper(string fileName, string id) { if (fileName == null) { throw new ArgumentNullException("fileName"); } Debug.Assert(File.Exists(fileName), fileName + "does not exist."); if (!File.Exists(fileName)) { return(null); } BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); Debug.Assert(br != null, "Fail to create a BinaryReader from file."); if (br == null) { return(null); } Uri imageUri = new Uri("images/" + Path.GetFileName(fileName), UriKind.Relative); int fileIndex = 0; while (true) { if (_part.Package.PartExists(PackUriHelper.ResolvePartUri(_part.Uri, imageUri))) { Debug.Write(imageUri.ToString() + " already exists."); imageUri = new Uri( "images/" + Path.GetFileNameWithoutExtension(fileName) + (fileIndex++).ToString() + Path.GetExtension(fileName), UriKind.Relative); continue; } break; } if (id != null) { int idIndex = 0; string testId = id; while (true) { if (_part.RelationshipExists(testId)) { Debug.Write(testId + " already exists."); testId = id + (idIndex++); continue; } id = testId; break; } } PackageRelationship imageRel = _part.CreateRelationship(imageUri, TargetMode.Internal, OfficeDocument.ImagePartRelType, id); Debug.Assert(imageRel != null, "Fail to create image relationship."); if (imageRel == null) { return(null); } PackagePart imagePart = _part.Package.CreatePart( PackUriHelper.ResolvePartUri(imageRel.SourceUri, imageRel.TargetUri), OfficePart.MapImageContentType(Path.GetExtension(fileName))); Debug.Assert(imagePart != null, "Fail to create image part."); if (imagePart == null) { return(null); } BinaryWriter bw = new BinaryWriter(imagePart.GetStream(FileMode.Create, FileAccess.Write)); Debug.Assert(bw != null, "Fail to create a BinaryWriter to write to part."); if (bw == null) { return(null); } byte[] buffer = new byte[1024]; int byteCount = 0; while ((byteCount = br.Read(buffer, 0, buffer.Length)) > 0) { bw.Write(buffer, 0, byteCount); } bw.Flush(); bw.Close(); br.Close(); return(imageRel.Id); }