Beispiel #1
0
        public void DeletePart(PackageRelationship relationship)
        {
            var packagePartUri = PackagePartHelper.EnsureCorrectUri(relationship.TargetUri);

            if (!package.PartExists(packagePartUri))
            {
                throw new Exception($"Package part {packagePartUri} does not exist");
            }
            package.DeletePart(packagePartUri);
        }
Beispiel #2
0
        private PackagePart CreateDefaultDocumentPart(bool isTemplate)
        {
            var contentType  = isTemplate ? ContentTypes.Template : ContentTypes.Document;
            var documentPart = package.CreatePart(new Uri("/word/document.xml", UriKind.Relative), contentType, CompressionOption.Normal);

            package.CreateRelationship(documentPart.Uri, TargetMode.Internal, RelationshipTypes.OfficeDocument);
            var document = XmlResourceHelper.GetXmlResource("Wordroller.Packages.Defaults.document.xml");

            PackagePartHelper.SavePackagePart(documentPart, document);
            return(documentPart);
        }
Beispiel #3
0
        internal PackagePart CreateDefaultNumberingPart()
        {
            var packagePartUri = new Uri("/word/numbering.xml", UriKind.Relative);
            var part           = package.CreatePart(packagePartUri, ContentTypes.Numbering, CompressionOption.Normal);

            DocumentPart.CreateRelationship(packagePartUri, TargetMode.Internal, RelationshipTypes.Numbering);
            var content = XmlResourceHelper.GetXmlResource("Wordroller.Packages.Defaults.numbering.xml");

            PackagePartHelper.SavePackagePart(part, content);
            return(part);
        }
Beispiel #4
0
        public (PackagePart, PackageRelationship) AddFooterPart()
        {
            var safePartIndex = package.GetParts().Count() + 1;
            var partUri       = new Uri($"/word/footer{safePartIndex}.xml", UriKind.Relative);
            var part          = package.CreatePart(partUri, ContentTypes.Footer, CompressionOption.Normal);
            var content       = XmlResourceHelper.GetXmlResource("Wordroller.Packages.Defaults.footer.xml");

            PackagePartHelper.SavePackagePart(part, content);
            var relationship = DocumentPart.CreateRelationship(part.Uri, TargetMode.Internal, RelationshipTypes.Footer);

            return(part, relationship);
        }
Beispiel #5
0
        internal WordDocumentPackage(Stream stream)
        {
            memoryStream = new MemoryStream();
            stream.CopyTo(memoryStream);
            package = Package.Open(memoryStream, FileMode.Open, FileAccess.ReadWrite);

            var officeDocumentRelationship = package.GetRelationshipsByType(RelationshipTypes.OfficeDocument).Single();

            DocumentPart  = package.GetPart(PackagePartHelper.EnsureCorrectUri(officeDocumentRelationship.TargetUri));
            SettingsPart  = GetSinglePackagePart(RelationshipTypes.Settings);
            StylesPart    = GetSinglePackagePart(RelationshipTypes.Styles);
            NumberingPart = FindSinglePackagePart(RelationshipTypes.Numbering);
        }
Beispiel #6
0
        private PackagePart CreateDefaultSettingsPart(CultureInfo culture, PackagePart documentPart)
        {
            var packagePartUri = new Uri("/word/settings.xml", UriKind.Relative);
            var part           = package.CreatePart(packagePartUri, ContentTypes.Settings, CompressionOption.Normal);

            documentPart.CreateRelationship(packagePartUri, TargetMode.Internal, RelationshipTypes.Settings);
            var settings = XmlResourceHelper.GetXmlResource("Wordroller.Packages.Defaults.settings.xml");

            var themeFontLang = settings.Root?.Element(XName.Get("themeFontLang", Namespaces.w.NamespaceName));

            themeFontLang?.SetAttributeValue(XName.Get("val", Namespaces.w.NamespaceName), culture);
            PackagePartHelper.SavePackagePart(part, settings);
            return(part);
        }
Beispiel #7
0
        private PackagePart CreateDefaultStylesPart(CultureInfo culture, PackagePart documentPart)
        {
            var packagePartUri = new Uri("/word/styles.xml", UriKind.Relative);
            var part           = package.CreatePart(packagePartUri, ContentTypes.Styles, CompressionOption.Normal);

            documentPart.CreateRelationship(packagePartUri, TargetMode.Internal, RelationshipTypes.Styles);
            var content = XmlResourceHelper.GetXmlResource("Wordroller.Packages.Defaults.styles.xml");

            var lang = content.Root
                       ?.Element(XName.Get("docDefaults", Namespaces.w.NamespaceName))
                       ?.Element(XName.Get("rPrDefault", Namespaces.w.NamespaceName))
                       ?.Element(XName.Get("rPr", Namespaces.w.NamespaceName))
                       ?.Element(XName.Get("lang", Namespaces.w.NamespaceName))
                       ?? throw new Exception("The default styles part is corrupt");

            lang.SetAttributeValue(XName.Get("val", Namespaces.w.NamespaceName), culture.ToString());
            PackagePartHelper.SavePackagePart(part, content);
            return(part);
        }
Beispiel #8
0
        private PackagePart?FindSinglePackagePart(string relationshipType)
        {
            var packageRelationship = DocumentPart.GetRelationships().SingleOrDefault(r => r.RelationshipType == relationshipType);

            return(packageRelationship != null?package.GetPart(PackagePartHelper.EnsureCorrectUri(packageRelationship.TargetUri)) : null);
        }
Beispiel #9
0
 public Stream GetPackagePartStream(Uri uri, FileMode mode, FileAccess access)
 {
     return(new PackagePartStream(package.GetPart(PackagePartHelper.EnsureCorrectUri(uri)).GetStream(mode, access)));
 }