/// <summary> /// Cleanly removes a part from a package /// </summary> /// <param name="part"></param> internal void RemovePart(OpenXmlSDK.OpenXmlPart part) { var parentParts = part.GetParentParts().ToList(); foreach (var parentPart in parentParts) { parentPart.DeletePart(part); } }
/// <summary> /// Writes the contents of a part into a file /// </summary> /// <param name="part">OpenXml part to read contents from</param> /// <param name="filePath">Full path of file to write part contents to</param> internal static void SavePartAs(OpenXmlSDK.OpenXmlPart part, string filePath) { Stream partStream = part.GetStream(FileMode.Open, FileAccess.Read); byte[] partContent = new byte[partStream.Length]; partStream.Read(partContent, 0, (int)partStream.Length); File.WriteAllBytes(filePath, partContent); }
public bool TryGetValue(string uri, [MaybeNullWhen(false)] out OpenXmlPart part) { if (_parts is null) { part = null; return(false); } return(_parts.TryGetValue(uri, out part)); }
/// <summary> /// Feeds data into the part stream. /// The stream of the part will be truncated at first. /// </summary> /// <param name="sourceStream">The source stream to be read from.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="sourceStream"/> is a null reference.</exception> public void FeedData(Stream sourceStream) { ThrowIfObjectDisposed(); if (sourceStream == null) { throw new ArgumentNullException("sourceStream"); } using (Stream targetStream = this.GetStream(FileMode.Create)) { OpenXmlPart.CopyStream(sourceStream, targetStream); } }
internal void Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, string id, Dictionary <Uri, OpenXmlPart> loadedParts) { if (uriTarget is null) { throw new ArgumentNullException(nameof(uriTarget)); } if (id is null) { throw new ArgumentNullException(nameof(id)); } if (openXmlPackage is null && parent is null) { throw new ArgumentNullException(ExceptionMessages.PackageRelatedArgumentNullException); }
internal static OpenXmlPart CreateOpenXmlPart(OpenXmlPackage openXmlPackage, string relationshipType) { if (relationshipType is null) { throw new ArgumentNullException(nameof(relationshipType)); } OpenXmlPart openXmlPart = null; CreatePartCore(openXmlPackage, relationshipType, ref openXmlPart); if (openXmlPart is null) { openXmlPart = new ExtendedPart(relationshipType); } return(openXmlPart); }
// create a new part in this package internal void CreateInternal2(OpenXmlPackage openXmlPackage, OpenXmlPart parent, string contentType, Uri partUri) { // openXmlPackage, parent can not be all null if (openXmlPackage == null && parent == null) { throw new ArgumentNullException(ExceptionMessages.PackageRelatedArgumentNullException); } else if (parent != null && openXmlPackage != null && parent.OpenXmlPackage != openXmlPackage) { throw new ArgumentOutOfRangeException(nameof(parent)); } else if (parent != null && openXmlPackage == null) { openXmlPackage = parent.OpenXmlPackage; } // throw exception to catch error in our code if (_packagePart != null) { throw new InvalidOperationException(); } // set the _openXmlPackage so ThrowIfObjectDisposed( ) do not throw. _openXmlPackage = openXmlPackage; Uri parentUri; if (parent != null) { parentUri = parent.Uri; } else { parentUri = new Uri("/", UriKind.Relative); } _uri = _openXmlPackage.GetUniquePartUri(contentType, parentUri, partUri); _packagePart = _openXmlPackage.CreateMetroPart(_uri, contentType); }
public IdPartPair(string id, OpenXmlPart part) { RelationshipId = id; OpenXmlPart = part; }
/// <summary> /// Saves the current <see cref="XDocument"/> to the part if it and its <see cref="XDocument.Root"/> /// element is not <see langword="null"/>. /// </summary> /// <remarks> /// Calling this method has the same effect as calling <see cref="SaveXDocument"/>. /// This method is provided for naming consistency with <see cref="GetXElement"/> and /// <see cref="SetXElement"/>. /// </remarks> /// <param name="part">The part to save to.</param> /// <returns> /// <see langword="true"/>, if the current <see cref="XDocument"/> was saved to the part; /// <see langword="false"/>, otherwise. /// </returns> public static bool SaveXElement(this OpenXmlPart part) => part.GetOpenXmlPartRootXElementFeature().Save();
static partial void CreatePartCore(OpenXmlPackage openXmlPackage, string relationshipType, ref OpenXmlPart openXmlPart);
/// <summary> /// Loads an XDocument with contents of a given part /// </summary> /// <param name="part">Part to load contents from</param> /// <returns>XDocument with contents of part</returns> protected internal XDocument GetXDocument(OpenXmlSDK.OpenXmlPart part) { return(part.GetXDocument()); }
/// <summary> /// Gets an <see cref="XElement"/> representation of the <paramref name="part"/>. /// </summary> /// <remarks> /// <para> /// Calling this method has same effect as calling <c>part.GetXDocument().Root</c>. /// </para> /// <para> /// When called with a given <see cref="OpenXmlPart"/> for the first time after having /// opened the containing <see cref="OpenXmlPackage"/> or saved the strongly-typed /// <see cref="OpenXmlPartRootElement"/> to the OpenXmlPart, deserializes, caches, and /// returns the outer XML of an already loaded OpenXmlPartRootElement or the content /// of the OpenXmlPart as an <see cref="XElement"/>. In the following calls, directly /// returns the cached XElement. /// </para> /// </remarks> /// <param name="part">The part to get the contents of.</param> /// <returns>An <see cref="XElement"/>.</returns> /// <seealso cref="GetXDocument"/> /// <seealso cref="SetXElement"/> /// <seealso cref="SaveXElement"/> public static XElement?GetXElement(this OpenXmlPart part) => part.GetOpenXmlPartRootXElementFeature().Root;
/// <summary> /// Gets a value indicating whether the root <see cref="XElement"/> is loaded from the part /// or it has been set. /// </summary> /// <param name="part">The <see cref="OpenXmlPart"/>.</param> /// <returns> /// <see langword="true"/>, if the current root <see cref="XElement"/> is loaded from the part or it has been set; /// <see langword="false"/>, otherwise. /// </returns> public static bool IsRootXElementLoaded(this OpenXmlPart part) => part.GetOpenXmlPartRootXElementFeature().IsRootXElementLoaded;
/// <summary> /// Gets an <see cref="XDocument"/> representation of the <paramref name="part"/>. /// </summary> /// <remarks> /// <para> /// This method always returns the same <see cref="XDocument"/> instance, unless that /// instance is changed via <see cref="SetXDocument"/>. Calling this method has the same /// effect as calling <c>part.GetXElement().Document</c>. /// </para> /// <para> /// When called with a given <see cref="OpenXmlPart"/> for the first time after having /// opened the containing <see cref="OpenXmlPackage"/> or saved the strongly-typed /// <see cref="OpenXmlPartRootElement"/> to the OpenXmlPart, deserializes, caches, and /// returns the outer XML of an already loaded OpenXmlPartRootElement or the content /// of the OpenXmlPart as an <see cref="XDocument"/>. In the following calls, directly /// returns the cached XDocument. /// </para> /// </remarks> /// <param name="part">The part to get the contents of.</param> /// <returns>An <see cref="XDocument"/>.</returns> /// <seealso cref="GetXElement"/> /// <seealso cref="SetXDocument"/> /// <seealso cref="SaveXDocument"/> public static XDocument GetXDocument(this OpenXmlPart part) => part.GetOpenXmlPartRootXElementFeature().Document;
internal void Load(OpenXmlPackage openXmlPackage, OpenXmlPart parent, Uri uriTarget, string id, Dictionary <Uri, OpenXmlPart> loadedParts) { if (uriTarget == null) { throw new ArgumentNullException("uriTarget"); } if (id == null) { throw new ArgumentNullException("id"); } if (openXmlPackage == null && parent == null) { throw new ArgumentNullException(ExceptionMessages.PackageRelatedArgumentNullException); } else if (parent != null && openXmlPackage != null && parent.OpenXmlPackage != openXmlPackage) { throw new ArgumentOutOfRangeException("parent"); } else if (parent != null && openXmlPackage == null) { openXmlPackage = parent.OpenXmlPackage; } this._openXmlPackage = openXmlPackage; //this._ownerPart = parent; Debug.Assert(loadedParts.ContainsKey(uriTarget)); this._uri = uriTarget; // TODO: should we delay load? PackagePart metroPart = this.OpenXmlPackage.Package.GetPart(uriTarget); if (this.IsContentTypeFixed && metroPart.ContentType != this.ContentType) { string errorMessage = String.Format(CultureInfo.CurrentUICulture, ExceptionMessages.InvalidPartContentType, metroPart.Uri.OriginalString, metroPart.ContentType, this.ContentType); OpenXmlPackageException e = new OpenXmlPackageException(errorMessage); //e.Data.Add("Part Uri", metroPart.Uri.OriginalString ); //e.Data.Add("Part Content Type", metroPart.ContentType); //e.Data.Add("Expected Content Type", this.ContentType); throw e; } this._metroPart = metroPart; // add the _uri to be reserved this.OpenXmlPackage.ReserveUri(this.ContentType, this.Uri); // load recursively RelationshipCollection relationshipCollection = new PackagePartRelationshipPropertyCollection(this.PackagePart); LoadReferencedPartsAndRelationships(openXmlPackage, this, relationshipCollection, loadedParts); }
// create a new part in this package internal void CreateInternal(OpenXmlPackage openXmlPackage, OpenXmlPart parent, string contentType, string targetExt) { // openXmlPackage, parent can not be all null if (openXmlPackage == null && parent == null) { throw new ArgumentNullException(ExceptionMessages.PackageRelatedArgumentNullException); } else if (parent != null && openXmlPackage != null && parent.OpenXmlPackage != openXmlPackage) { throw new ArgumentOutOfRangeException(nameof(parent)); } else if (parent != null && openXmlPackage == null) { openXmlPackage = parent.OpenXmlPackage; } // throw exception to catch error in our code if (_packagePart != null) { throw new InvalidOperationException(); } // set the _openXmlPackage so ThrowIfObjectDisposed( ) do not throw. _openXmlPackage = openXmlPackage; Uri parentUri; if (parent != null) { parentUri = parent.Uri; } else { parentUri = new Uri("/", UriKind.Relative); } //OpenXmlPart parentPart = this._ownerPart; //Uri is auto generated to make sure it's unique string targetPath = GetTargetPath(TargetPath); if (targetPath == null) { targetPath = "."; } string targetFileExt = targetExt; if (!IsContentTypeFixed) { if (!_openXmlPackage.PartExtensionProvider.TryGetValue(contentType, out targetFileExt)) { targetFileExt = targetExt; } } if (targetFileExt == null) { targetFileExt = TargetFileExtension; } _uri = _openXmlPackage.GetUniquePartUri(contentType, parentUri, targetPath, TargetName, targetFileExt); _packagePart = _openXmlPackage.CreateMetroPart(_uri, contentType); }
/// <summary> /// Sets the <see cref="OpenXmlPart"/>'s root <see cref="XElement"/> to the given XElement, /// serializes and writes the XElement to the OpenXmlPart, and reloads the OpenXmlPart's /// <see cref="OpenXmlPartRootElement"/> if it was previously loaded. /// </summary> /// <remarks> /// Effectively sets the <see cref="XDocument.Root"/> property of the <see cref="XDocument"/> /// returned by <see cref="GetXDocument"/>. /// </remarks> /// <param name="part">The <see cref="OpenXmlPart"/>.</param> /// <param name="element">The <see cref="XElement"/>.</param> /// <seealso cref="GetXElement"/> /// <seealso cref="SaveXElement"/> public static void SetXElement(this OpenXmlPart part, XElement element) => part.GetOpenXmlPartRootXElementFeature().Root = element;
/// <summary> /// Sets the <see cref="OpenXmlPart"/>'s <see cref="XDocument"/> to the given XDocument, /// serializes and writes the XDocument to the OpenXmlPart, and reloads the OpenXmlPart's /// <see cref="OpenXmlPartRootElement"/> if it was previously loaded. /// </summary> /// <param name="part">The <see cref="OpenXmlPart"/>.</param> /// <param name="document">The <see cref="XDocument"/>.</param> /// <seealso cref="GetXDocument"/> /// <seealso cref="SaveXDocument"/> public static void SetXDocument(this OpenXmlPart part, XDocument document) => part.GetOpenXmlPartRootXElementFeature().Document = document;
/// <summary> /// Will create or get a cached instance of <see cref="IOpenXmlPartRootXElementFeature"/>. /// </summary> /// <param name="part">The part to provide an <see cref="XElement"/> instance.</param> /// <returns>A <see cref="IOpenXmlPartRootXElementFeature"/>.</returns> internal static IOpenXmlPartRootXElementFeature GetOpenXmlPartRootXElementFeature(this OpenXmlPart part) { var feature = part.Features.Get <IOpenXmlPartRootXElementFeature>(); if (feature is not null) { return(feature); } part.AddDisposableFeature(); part.AddPartRootEventsFeature(); feature = new OpenXmlPartRootXElementFeature(part); part.Features.SetDisposable(feature); return(feature); }
public bool ContainsValue(OpenXmlPart part) => _parts is null ? false : _parts.ContainsValue(part);