Ejemplo n.º 1
0
 /// <summary>
 /// Sets the extended document properties
 /// </summary>
 /// <param name="document">WordprocessingDocument reference</param>
 /// <param name="company">Company</param>
 /// <param name="application">Application</param>
 public static void SetExtendedProperties(this WordprocessingDocument document, Company company, Application application)
 {
     //Add extended Document properties
     if (document.ExtendedFilePropertiesPart == null)
     {
         document.AddExtendedFilePropertiesPart();
     }
     document.ExtendedFilePropertiesPart.Properties = new Properties
     {
         Company     = company ?? new Company(@"izrra.ch"),
         Application = application ?? new Application("Combine.Sdk.Word")
     };
     //Save all changes
     document.Save();
 }
Ejemplo n.º 2
0
        public override void ProcessDocument()
        {
            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(FileStream, true);

            if (wordprocessingDocument.ExtendedFilePropertiesPart == null)
            {
                wordprocessingDocument.AddExtendedFilePropertiesPart();
            }
            AddExtraInfo(wordprocessingDocument);
            FillDocumentSpecificInfo(wordprocessingDocument);
            wordprocessingDocument.ExtendedFilePropertiesPart.Properties.Save();
            wordprocessingDocument.Close();
            FileStream.Position = 0;
            DocumentFile        = FileStream.ToArray();
            FileType            = DocumentTypesEnum.Docx;
            FileStream.Close();
        }
        /// <summary>
        /// Adds an ExtendedFilePropertiesPart to the specified document if it's not already there.
        /// </summary>
        /// <param name="doc">The document object</param>
        /// <remarks>
        /// This method is used when creating a new document. At that time, the freshly created doc does
        /// not have extended properties. This method is also used when saving extended properties; if
        /// the extended property element doesn't exist, this creates it and allows the extended property
        /// to be saved.
        /// </remarks>
        internal static void AddExtendedFilePropertiesPartIfNeeded(WordprocessingDocument doc)
        {
            if (doc == null)
            {
                throw new ArgumentNullException(nameof(doc));
            }

            if (doc.ExtendedFilePropertiesPart == null)
            {
                doc.AddExtendedFilePropertiesPart();
            }

            if (doc.ExtendedFilePropertiesPart.Properties == null)
            {
                doc.ExtendedFilePropertiesPart.Properties = new DocumentFormat.OpenXml.ExtendedProperties.Properties()
                {
                    Company = new DocumentFormat.OpenXml.ExtendedProperties.Company(),
                };
            }
        }
        private static void CopyStartingParts(WordprocessingDocument sourceDocument, WordprocessingDocument newDocument,
            List<ImageData> images)
        {
            // A Core File Properties part does not have implicit or explicit relationships to other parts.
            CoreFilePropertiesPart corePart = sourceDocument.CoreFilePropertiesPart;
            if (corePart != null && corePart.GetXDocument().Root != null)
            {
                newDocument.AddCoreFilePropertiesPart();
                XDocument newXDoc = newDocument.CoreFilePropertiesPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                XDocument sourceXDoc = corePart.GetXDocument();
                newXDoc.Add(sourceXDoc.Root);
            }

            // An application attributes part does not have implicit or explicit relationships to other parts.
            ExtendedFilePropertiesPart extPart = sourceDocument.ExtendedFilePropertiesPart;
            if (extPart != null)
            {
                OpenXmlPart newPart = newDocument.AddExtendedFilePropertiesPart();
                XDocument newXDoc = newDocument.ExtendedFilePropertiesPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(extPart.GetXDocument().Root);
            }

            // An custom file properties part does not have implicit or explicit relationships to other parts.
            CustomFilePropertiesPart customPart = sourceDocument.CustomFilePropertiesPart;
            if (customPart != null)
            {
                newDocument.AddCustomFilePropertiesPart();
                XDocument newXDoc = newDocument.CustomFilePropertiesPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(customPart.GetXDocument().Root);
            }

            DocumentSettingsPart oldSettingsPart = sourceDocument.MainDocumentPart.DocumentSettingsPart;
            if (oldSettingsPart != null)
            {
                DocumentSettingsPart newSettingsPart = newDocument.MainDocumentPart.AddNewPart<DocumentSettingsPart>();
                XDocument settingsXDoc = oldSettingsPart.GetXDocument();
                AddRelationships(oldSettingsPart, newSettingsPart, new[] { settingsXDoc.Root });
                CopyFootnotesPart(sourceDocument, newDocument, settingsXDoc, images);
                CopyEndnotesPart(sourceDocument, newDocument, settingsXDoc, images);
                XDocument newXDoc = newDocument.MainDocumentPart.DocumentSettingsPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(settingsXDoc.Root);
                CopyRelatedPartsForContentParts(oldSettingsPart, newSettingsPart, new[] { newXDoc.Root }, images);
            }

            WebSettingsPart oldWebSettingsPart = sourceDocument.MainDocumentPart.WebSettingsPart;
            if (oldWebSettingsPart != null)
            {
                WebSettingsPart newWebSettingsPart = newDocument.MainDocumentPart.AddNewPart<WebSettingsPart>();
                XDocument settingsXDoc = oldWebSettingsPart.GetXDocument();
                AddRelationships(oldWebSettingsPart, newWebSettingsPart, new[] { settingsXDoc.Root });
                XDocument newXDoc = newDocument.MainDocumentPart.WebSettingsPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(settingsXDoc.Root);
            }

            ThemePart themePart = sourceDocument.MainDocumentPart.ThemePart;
            if (themePart != null)
            {
                ThemePart newThemePart = newDocument.MainDocumentPart.AddNewPart<ThemePart>();
                XDocument newXDoc = newDocument.MainDocumentPart.ThemePart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(themePart.GetXDocument().Root);
                CopyRelatedPartsForContentParts(themePart, newThemePart, new[] { newThemePart.GetXDocument().Root }, images);
            }

            // If needed to handle GlossaryDocumentPart in the future, then
            // this code should handle the following parts:
            //   MainDocumentPart.GlossaryDocumentPart.StyleDefinitionsPart
            //   MainDocumentPart.GlossaryDocumentPart.StylesWithEffectsPart

            // A Style Definitions part shall not have implicit or explicit relationships to any other part.
            StyleDefinitionsPart stylesPart = sourceDocument.MainDocumentPart.StyleDefinitionsPart;
            if (stylesPart != null)
            {
                newDocument.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
                XDocument newXDoc = newDocument.MainDocumentPart.StyleDefinitionsPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(stylesPart.GetXDocument().Root);
            }

            // A StylesWithEffects part shall not have implicit or explicit relationships to any other part.
            StylesWithEffectsPart stylesWithEffectsPart = sourceDocument.MainDocumentPart.StylesWithEffectsPart;
            if (stylesWithEffectsPart != null)
            {
                newDocument.MainDocumentPart.AddNewPart<StylesWithEffectsPart>();
                XDocument newXDoc = newDocument.MainDocumentPart.StylesWithEffectsPart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                newXDoc.Add(stylesWithEffectsPart.GetXDocument().Root);
            }

            // Note: Do not copy the numbering part.  For every source, create new numbering definitions from
            // scratch.
            //NumberingDefinitionsPart numberingPart = sourceDocument.MainDocumentPart.NumberingDefinitionsPart;
            //if (numberingPart != null)
            //{
            //    newDocument.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>();
            //    XDocument newXDoc = newDocument.MainDocumentPart.NumberingDefinitionsPart.GetXDocument();
            //    newXDoc.Declaration.Standalone = "yes";
            //    newXDoc.Declaration.Encoding = "UTF-8";
            //    newXDoc.Add(numberingPart.GetXDocument().Root);
            //    newXDoc.Descendants(W.numIdMacAtCleanup).Remove();
            //}

            // A Font Table part shall not have any implicit or explicit relationships to any other part.
            FontTablePart fontTablePart = sourceDocument.MainDocumentPart.FontTablePart;
            if (fontTablePart != null)
            {
                newDocument.MainDocumentPart.AddNewPart<FontTablePart>();
                XDocument newXDoc = newDocument.MainDocumentPart.FontTablePart.GetXDocument();
                newXDoc.Declaration.Standalone = "yes";
                newXDoc.Declaration.Encoding = "UTF-8";
                CopyFontTable(sourceDocument.MainDocumentPart.FontTablePart, newDocument.MainDocumentPart.FontTablePart);
                newXDoc.Add(fontTablePart.GetXDocument().Root);
            }
        }