Beispiel #1
0
        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);
            }

            if (parent is not null && openXmlPackage is not null &&
                parent.OpenXmlPackage != openXmlPackage)
            {
                throw new ArgumentOutOfRangeException(nameof(parent));
            }

            if (parent is not null && openXmlPackage is null)
            {
                openXmlPackage = parent.OpenXmlPackage;
            }

            _openXmlPackage = openXmlPackage ?? throw new ArgumentNullException(nameof(openXmlPackage));

            Debug.Assert(loadedParts.ContainsKey(uriTarget));

            _uri = uriTarget;

            // TODO: should we delay load?
            var part = openXmlPackage.Package.GetPart(uriTarget);

            _packagePart = part;

            if (IsContentTypeFixed && !IsValidContentType(part))
            {
                var errorMessage = SR.Format(
                    ExceptionMessages.InvalidPartContentType,
                    part.Uri.OriginalString,
                    part.ContentType,
                    ContentType);

                throw new OpenXmlPackageException(errorMessage);
            }

            // add the _uri to be reserved
            openXmlPackage.ReserveUri(ContentType, Uri);

            // load recursively
            var relationshipCollection = new PackagePartRelationshipPropertyCollection(PackagePart);

            LoadReferencedPartsAndRelationships(openXmlPackage, this, relationshipCollection, loadedParts);
        }
Beispiel #2
0
        internal static XmlPath?GetXPath(OpenXmlPart?part)
        {
            if (part is null)
            {
                return(null);
            }

            return(new XmlPath(part));
        }
Beispiel #3
0
        private static string GetPartUri(OpenXmlPart?part)
        {
            if (part is null)
            {
                return(string.Empty);
            }

            // Example: WordprocessingCommentsPart{/word/comments.xml}
            return(SR.Format("{0}{1}{2}", '{', part.Uri, '}'));
        }
Beispiel #4
0
        private static string GetPartNameAndUri(OpenXmlPart?part)
        {
            if (part is null)
            {
                return(string.Empty);
            }

            string partClassName = part.GetType().Name;

            // Example: WordprocessingCommentsPart{/word/comments.xml}
            return(SR.Format("{0}{1}{2}{3}", partClassName, '{', part.Uri, '}'));
        }
Beispiel #5
0
 public ImageUsageInfo(
     OpenXmlPart?part, Blip?blip,
     Transform2D?transform2D,
     SourceRectangle?sourceRectangle)
 {
     Part  = part;
     RelId = blip?.Embed;
     Size  = new Size
     {
         Width  = (int)(transform2D?.Extents?.Cx?.Value ?? 0),
         Height = (int)(transform2D?.Extents?.Cy?.Value ?? 0)
     };
     Crop = ParseCrop(sourceRectangle);
 }
        public IDisposable Push(OpenXmlPackage?package = null, OpenXmlPart?part = null, OpenXmlElement?element = null)
        {
            var current = Current;
            var updated = GetOrCreateElement();

            updated.CopyFrom(current);

            updated.Package = package ?? current?.Package;
            updated.Part    = part ?? current?.Part;
            updated.Element = element ?? current?.Element;

            _elements.Push(updated);

            return(_popDisposable);
        }
Beispiel #7
0
        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);
            }
        private static void RemoveElementsForDocumentComparison(WordprocessingDocument doc)
        {
            OpenXmlPart?part = doc.ExtendedFilePropertiesPart;

            if (part != null)
            {
                var appPropsXDoc = part.GetXDocument();
                appPropsXDoc.Descendants(EP.TotalTime).Remove();
                part.PutXDocument();
            }

            part = doc.CoreFilePropertiesPart;
            if (part != null)
            {
                var corePropsXDoc = part.GetXDocument();
                corePropsXDoc.Descendants(CP.revision).Remove();
                corePropsXDoc.Descendants(DCTERMS.created).Remove();
                corePropsXDoc.Descendants(DCTERMS.modified).Remove();
                part.PutXDocument();
            }

            var mainXDoc      = doc.MainDocumentPart.GetXDocument();
            var bookmarkStart = mainXDoc
                                .Descendants(W.bookmarkStart)
                                .Where(b => (string)b.Attribute(W.name) == "_GoBack")
                                .ToList();

            foreach (var item in bookmarkStart)
            {
                var bookmarkEnd = mainXDoc
                                  .Descendants(W.bookmarkEnd)
                                  .Where(be => (int)be.Attribute(W.id) == (int)item.Attribute(W.id));
                bookmarkEnd.Remove();
            }

            bookmarkStart.Remove();
            doc.MainDocumentPart.PutXDocument();
        }
Beispiel #9
0
 public PartHolder(T item, OpenXmlPart?part)
 {
     Item = item;
     Part = part;
 }
 internal static void OnChange(this IPartRootEventsFeature events, EventType type, OpenXmlPart?part)
 {
     if (part is not null && events is IRaiseFeatureEvent <OpenXmlPart> raise)
     {
         raise.OnChange(type, part);
     }
 }