private Relationship AddRelationship(string location, string relationshipType, string parentLocation, TargetMode mode) { if (!_relationships.ContainsKey(parentLocation) || _relationships[parentLocation] == null) { _relationships[parentLocation] = new List <Relationship>(); } else { foreach (Relationship item in _relationships[parentLocation]) { if (item.RelatedPart == location) { return(item); } } } Relationship relationship = new Relationship(); relationship.RelatedPart = location; relationship.RelationshipId = NextRelationshipId(); relationship.RelationshipType = relationshipType; relationship.Mode = mode; _relationships[parentLocation].Add(relationship); Uri targetUri = (relationship.Mode != 0) ? new Uri(relationship.RelatedPart, UriKind.Absolute) : new Uri(WordOpenXmlUtils.CleanName(relationship.RelatedPart), UriKind.Relative); if (parentLocation == "/") { _package.CreateRelationship(targetUri, relationship.Mode, relationship.RelationshipType, relationship.RelationshipId); } else { _package.GetPart(new Uri(WordOpenXmlUtils.CleanName(parentLocation), UriKind.Relative)).CreateRelationship(targetUri, relationship.Mode, relationship.RelationshipType, relationship.RelationshipId); } return(relationship); }
public Relationship AddImageToTree(ImageHash hash, string extension, string relationshipType, string locationHint, string parentLocation, ContentTypeAction ctypeAction, out bool newBlob) { PhantomPart phantomPart = new PhantomPart(); phantomPart.ContentType = "image/" + extension; if (_blobPathsByUniqueId.TryGetValue(hash, out string value)) { phantomPart.Location = value; newBlob = false; } else { int num; switch (extension) { default: num = 0; break; case "jpg": case "png": case "gif": num = -1; break; } CompressionOption compressionOption = (CompressionOption)num; value = string.Format(CultureInfo.InvariantCulture, UniqueLocation(locationHint), extension); _blobPathsByUniqueId[hash] = value; phantomPart.Location = value; _parts.Add(phantomPart.Location, phantomPart); _package.CreatePart(new Uri(WordOpenXmlUtils.CleanName(phantomPart.Location), UriKind.Relative), phantomPart.ContentType, compressionOption); newBlob = true; } return(AddRelationship(phantomPart.Location, relationshipType, parentLocation)); }
public Relationship AddStreamingPartToTree(string contentType, string relationshipType, string locationHint, string parentLocation, ContentTypeAction ctypeAction) { string location = this.UniqueLocation(locationHint); PhantomPart phantomPart = new PhantomPart(); phantomPart.ContentType = contentType; phantomPart.Location = location; this._parts.Add(phantomPart.Location, phantomPart); this._package.CreatePart(new Uri(WordOpenXmlUtils.CleanName(phantomPart.Location), UriKind.Relative), phantomPart.ContentType, CompressionOption.Normal); return(this.AddRelationship(location, relationshipType, parentLocation)); }
public Relationship AddImageToTree(Stream data, ImageHash hash, string extension, string relationshipType, string locationHint, string parentLocation) { bool newBlob; Relationship relationship = _relationshipTree.AddImageToTree(hash, extension, relationshipType, locationHint, parentLocation, ContentTypeAction.Default, out newBlob); if (newBlob) { Stream stream = _zipPackage.GetPart(new Uri(WordOpenXmlUtils.CleanName(relationship.RelatedPart), UriKind.Relative)).GetStream(); WordOpenXmlUtils.CopyStream(data, stream); } return(relationship); }
public Relationship AddImageToTree(Stream data, ImageHash hash, string extension, string relationshipType, string locationHint, string parentLocation) { bool flag = default(bool); Relationship relationship = this._relationshipTree.AddImageToTree(hash, extension, relationshipType, locationHint, parentLocation, ContentTypeAction.Default, out flag); if (flag) { Package zipPackage = this._zipPackage; PackagePart part = zipPackage.GetPart(new Uri(WordOpenXmlUtils.CleanName(relationship.RelatedPart), UriKind.Relative)); Stream stream = part.GetStream(); WordOpenXmlUtils.CopyStream(data, stream); } return(relationship); }
public void WritePart(string location) { PackagePart part = this._package.GetPart(new Uri(WordOpenXmlUtils.CleanName(location), UriKind.Relative)); StreamWriter streamWriter = new StreamWriter(part.GetStream()); ((XmlPart)this._parts[location]).HydratedPart.Write(streamWriter); streamWriter.Flush(); PhantomPart phantomPart = new PhantomPart(); phantomPart.ContentType = part.ContentType; phantomPart.Location = location; PhantomPart value = phantomPart; this._parts[location] = value; }
private Relationship AddPart(OoxmlPart part, string contentType, string relationshipType, string locationHint, string parentLocation, ContentTypeAction ctypeAction) { XmlPart xmlPart = new XmlPart(); xmlPart.ContentType = contentType; xmlPart.HydratedPart = part; if (locationHint.Contains("{0}")) { xmlPart.Location = this.UniqueLocation(locationHint); } else { xmlPart.Location = locationHint; } this._parts.Add(xmlPart.Location, xmlPart); this._package.CreatePart(new Uri(WordOpenXmlUtils.CleanName(xmlPart.Location), UriKind.Relative), xmlPart.ContentType, CompressionOption.Normal); return(this.AddRelationship(xmlPart.Location, relationshipType, parentLocation)); }
public Relationship AddImageToTree(ImageHash hash, string extension, string relationshipType, string locationHint, string parentLocation, ContentTypeAction ctypeAction, out bool newBlob) { PhantomPart phantomPart = new PhantomPart(); phantomPart.ContentType = "image/" + extension; string text = default(string); if (this._blobPathsByUniqueId.TryGetValue(hash, out text)) { phantomPart.Location = text; newBlob = false; } else { CompressionOption compressionOption = (CompressionOption)((extension == "jpg" || extension == "png" || extension == "gif") ? (-1) : 0); text = string.Format(CultureInfo.InvariantCulture, this.UniqueLocation(locationHint), extension); this._blobPathsByUniqueId[hash] = text; phantomPart.Location = text; this._parts.Add(phantomPart.Location, phantomPart); this._package.CreatePart(new Uri(WordOpenXmlUtils.CleanName(phantomPart.Location), UriKind.Relative), phantomPart.ContentType, compressionOption); newBlob = true; } return(this.AddRelationship(phantomPart.Location, relationshipType, parentLocation)); }