protected void WritePackage(OpenXmlWriter writer) { foreach (OpenXmlPart part in this.Parts) { part.WritePart(writer); } this.WriteRelationshipPart(writer); // write content types writer.AddPart("[Content_Types].xml"); writer.WriteStartDocument(); writer.WriteStartElement("Types", OpenXmlNamespaces.ContentTypes); foreach (string extension in _defaultTypes.Keys) { writer.WriteStartElement("Default", OpenXmlNamespaces.ContentTypes); writer.WriteAttributeString("Extension", extension); writer.WriteAttributeString("ContentType", _defaultTypes[extension]); writer.WriteEndElement(); } foreach (string partName in _partOverrides.Keys) { writer.WriteStartElement("Override", OpenXmlNamespaces.ContentTypes); writer.WriteAttributeString("PartName", partName); writer.WriteAttributeString("ContentType", _partOverrides[partName]); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); }
internal virtual void WritePart(OpenXmlWriter writer) { foreach (OpenXmlPart part in this.Parts) { part.WritePart(writer); } writer.AddPart(this.TargetFullName); writer.Write(this.GetStream()); this.WriteRelationshipPart(writer); }
protected virtual void WriteRelationshipPart(OpenXmlWriter writer) { List <OpenXmlPart> allParts = new List <OpenXmlPart>(); allParts.AddRange(this.Parts); allParts.AddRange(this.ReferencedParts); // write part relationships if (allParts.Count > 0 || _externalRelationships.Count > 0) { string relFullName = Path.Combine(Path.Combine(this.TargetDirectoryAbsolute, REL_FOLDER), TargetName + TargetExt + REL_EXTENSION); writer.AddPart(relFullName); writer.WriteStartDocument(); writer.WriteStartElement("Relationships", OpenXmlNamespaces.RelationsshipsPackage); foreach (ExternalRelationship rel in _externalRelationships) { writer.WriteStartElement("Relationship", OpenXmlNamespaces.RelationsshipsPackage); writer.WriteAttributeString("Id", rel.Id); writer.WriteAttributeString("Type", rel.RelationshipType); if (Uri.IsWellFormedUriString(rel.Target, UriKind.RelativeOrAbsolute)) { if (rel.TargetUri.IsAbsoluteUri) { if (rel.TargetUri.IsFile) { //reform the URI path for Word //Word does not accept forward slahes in the path of a local file writer.WriteAttributeString("Target", "file:///" + rel.TargetUri.AbsolutePath.Replace("/", "\\")); } else { writer.WriteAttributeString("Target", rel.Target.ToString()); } } else { writer.WriteAttributeString("Target", Uri.EscapeUriString(rel.Target.ToString())); } } else { writer.WriteAttributeString("Target", Uri.EscapeUriString(rel.Target)); } writer.WriteAttributeString("TargetMode", "External"); writer.WriteEndElement(); } foreach (OpenXmlPart part in allParts) { writer.WriteStartElement("Relationship", OpenXmlNamespaces.RelationsshipsPackage); writer.WriteAttributeString("Id", part.RelIdToString); writer.WriteAttributeString("Type", part.RelationshipType); // write the target relative to the current part writer.WriteAttributeString("Target", "/" + part.TargetFullName.Replace('\\', '/')); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); } }