///<summary> /// Sets value for specified <paramref name="key"/> into docx document inner key-value storage ///</summary> ///<param name="key">Key name</param> ///<param name="value">Value to be set</param> public void SetCustomProperty(string key, string value) { if (_wordDocument.CustomFilePropertiesPart == null) { var addCustomFilePropertiesPart = _wordDocument.AddCustomFilePropertiesPart(); addCustomFilePropertiesPart.Properties = new Properties(); } var properties = _wordDocument.CustomFilePropertiesPart.Properties; var existsProperty = properties.OfType <CustomDocumentProperty>() .SingleOrDefault(x => x.Name.HasValue && x.Name.Value == key); if (existsProperty != null) { existsProperty.Remove(); } Int32Value nextPropertyId = properties.Elements <CustomDocumentProperty>() .Select(x => x.PropertyId.Value) .Union(new[] { 1 }) .Max() + 1; properties.Append(new CustomDocumentProperty(new VTLPWSTR(value)) { FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", PropertyId = nextPropertyId, Name = key }); }
public void CheckIfDocumentExist(WordprocessingDocument doc, int id) { var customPropsAdd = doc.CustomFilePropertiesPart; if (customPropsAdd == null) { var customFilePropPart = doc.AddCustomFilePropertiesPart(); customFilePropPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); var customProp = new CustomDocumentProperty(); customProp.Name = "ParentId"; customProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"; customProp.VTLPWSTR = new VTLPWSTR(id.ToString()); customFilePropPart.Properties.AppendChild(customProp); int pid = 2; foreach (CustomDocumentProperty item in customFilePropPart.Properties) { item.PropertyId = pid++; } customFilePropPart.Properties.Save(); } }
private void BuildCustomProperties(MemoryStream documentData) { Logger.Info("Building custom properties section for newly-generated document."); using (WordprocessingDocument document = WordprocessingDocument.Open(documentData, true)) { CustomFilePropertiesPart customPropertiesPart = document.CustomFilePropertiesPart; // Add custom properties part if there is none yet. if (customPropertiesPart == null) { customPropertiesPart = document.AddCustomFilePropertiesPart(); customPropertiesPart.Properties = new Properties(); } // Remove all existing custom properties first. Properties customProps = customPropertiesPart.Properties; customProps.RemoveAllChildren <CustomDocumentProperty>(); // Stick all of our generated custom properties in the new document. foreach (CustomDocumentProperty customProp in customProperties) { customProps.AppendChild(customProp); } // Re-number the custom properties (advice given by MSDN). int pid = 2; foreach (CustomDocumentProperty customProp in customProps) { customProp.PropertyId = pid++; } } }
public DocxCustomPropertiesEditor ReadDocProperties() { if (_Document.CustomFilePropertiesPart == null) { _CustomProperties = _Document.AddCustomFilePropertiesPart(); _CustomProperties.Properties = new Properties(); } else { _CustomProperties = _Document.CustomFilePropertiesPart; } return(this); }
void SetCustomProperty(WordprocessingDocument doc, string propertyName, string propertyValue) { var newProp = new DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty(); newProp.VTLPWSTR = new DocumentFormat.OpenXml.VariantTypes.VTLPWSTR(propertyValue); newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"; newProp.Name = propertyName; var customProps = doc.CustomFilePropertiesPart; if (customProps == null) { // No custom properties? Add the part, and the // collection of properties now. customProps = doc.AddCustomFilePropertiesPart(); customProps.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); } var props = customProps.Properties; if (props != null) { // This will trigger an exception if the property's Name // property is null, but if that happens, the property is damaged, // and probably should raise an exception. var prop = props.Where( p => ((DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty)p).Name.Value == propertyName).FirstOrDefault(); // Does the property exist? If so, get the return value, // and then delete the property. if (prop != null) { Debug.WriteLine(prop.InnerText); prop.Remove(); } // Append the new property, and // fix up all the property ID values. // The PropertyId value must start at 2. props.AppendChild(newProp); int pid = 2; foreach (DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty item in props) { item.PropertyId = pid++; } } }
//gavdcodeend 05 //gavdcodebegin 06 public static void WordOpenXmlAddOneCustomPropertyDocument() { string propName = "myCustomProperty"; string returnValue = string.Empty; CustomDocumentProperty newProp = new CustomDocumentProperty(); newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"; newProp.Name = propName; using (WordprocessingDocument myWordDoc = WordprocessingDocument.Open(@"C:\Temporary\WordDoc01.docx", true)) { newProp.VTLPWSTR = new VTLPWSTR("This is the value of the property"); var customPropsPart = myWordDoc.CustomFilePropertiesPart; if (customPropsPart == null) { customPropsPart = myWordDoc.AddCustomFilePropertiesPart(); customPropsPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); } var customProps = customPropsPart.Properties; if (customProps != null) { var oneProp = customProps.Where( prp => ((CustomDocumentProperty)prp).Name.Value == propName).FirstOrDefault(); if (oneProp != null) { returnValue = oneProp.InnerText; oneProp.Remove(); } customProps.AppendChild(newProp); int pid = 2; foreach (CustomDocumentProperty item in customProps) { item.PropertyId = pid++; } customProps.Save(); } } }
/// <summary> /// add a new part that needs a relationship id /// </summary> /// <param name="document"></param> public static void AddNewPart(string document) { // Create a new word processing document. WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document); // Add the MainDocumentPart part in the new word processing document. var mainDocPart = wordDoc.AddNewPart <MainDocumentPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "rId1"); mainDocPart.Document = new Document(); // Add the CustomFilePropertiesPart part in the new word processing document. var customFilePropPart = wordDoc.AddCustomFilePropertiesPart(); customFilePropPart.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); // Add the CoreFilePropertiesPart part in the new word processing document. var coreFilePropPart = wordDoc.AddCoreFilePropertiesPart(); using (XmlTextWriter writer = new XmlTextWriter(coreFilePropPart.GetStream(FileMode.Create), System.Text.Encoding.UTF8)) { writer.WriteRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<cp:coreProperties xmlns:cp=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\"></cp:coreProperties>"); writer.Flush(); } // Add the DigitalSignatureOriginPart part in the new word processing document. wordDoc.AddNewPart <DigitalSignatureOriginPart>("rId4"); // Add the ExtendedFilePropertiesPart part in the new word processing document. var extendedFilePropPart = wordDoc.AddNewPart <ExtendedFilePropertiesPart>("rId5"); extendedFilePropPart.Properties = new DocumentFormat.OpenXml.ExtendedProperties.Properties(); // Add the ThumbnailPart part in the new word processing document. wordDoc.AddNewPart <ThumbnailPart>("image/jpeg", "rId6"); wordDoc.Close(); }
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); } }
public Boolean setCustomProperty(string propertyName, object propertyValue, WordprocessingDocument doc) { // Given a document name, a property name/value, and the property type, // add a custom property to a document. The method returns if it was successful or not var newProp = new CustomDocumentProperty(); // Now that you have handled the parameters, start // working on the document. newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"; newProp.Name = propertyName; newProp.VTLPWSTR = new VTLPWSTR(propertyValue.ToString()); var customProps = doc.CustomFilePropertiesPart; if (customProps == null) { // No custom properties? Add the part, and the // collection of properties now. customProps = doc.AddCustomFilePropertiesPart(); customProps.Properties = new DocumentFormat.OpenXml.CustomProperties.Properties(); } var props = customProps.Properties; if (props != null) { // This will trigger an exception if the property's Name // property is null, but if that happens, the property is damaged, // and probably should raise an exception. var prop = props.Where(p => ((CustomDocumentProperty)p).Name.Value == propertyName).FirstOrDefault(); // Does the property exist? If so, get the return value, // and then delete the property. if (prop != null) { prop.Remove(); } // Append the new property, and // fix up all the property ID values. // The PropertyId value must start at 2. props.AppendChild(newProp); int pid = 2; foreach (CustomDocumentProperty item in props) { item.PropertyId = pid++; } props.Save(); customProps.Properties.Save(); return true; } return false; }
internal static string SetCustomProperty(WordprocessingDocument document, string propertyName, object propertyValue, PropertyTypes propertyType) { string returnValue = null; var newProp = new CustomDocumentProperty(); bool propSet = false; // Calculate the correct type. switch (propertyType) { case PropertyTypes.DateTime: // Be sure you were passed a real date, // and if so, format in the correct way. // The date/time value passed in should // represent a UTC date/time. if ((propertyValue) is DateTime) { newProp.VTFileTime = new VTFileTime(string.Format("{0:s}Z", Convert.ToDateTime(propertyValue))); propSet = true; } break; case PropertyTypes.NumberInteger: if ((propertyValue) is int) { newProp.VTInt32 = new VTInt32(propertyValue.ToString()); propSet = true; } break; case PropertyTypes.NumberDouble: if (propertyValue is double) { newProp.VTFloat = new VTFloat(propertyValue.ToString()); propSet = true; } break; case PropertyTypes.Text: newProp.VTLPWSTR = new VTLPWSTR(propertyValue.ToString()); propSet = true; break; case PropertyTypes.YesNo: if (propertyValue is bool) { // Must be lowercase. newProp.VTBool = new VTBool( Convert.ToBoolean(propertyValue).ToString().ToLower()); propSet = true; } break; } if (!propSet) { // If the code was not able to convert the // property to a valid value, throw an exception. throw new InvalidDataException("propertyValue"); } // Now that you have handled the parameters, start // working on the document. newProp.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}"; newProp.Name = propertyName; var customProps = document.CustomFilePropertiesPart; if (customProps == null) { // No custom properties? Add the part, and the // collection of properties now. customProps = document.AddCustomFilePropertiesPart(); customProps.Properties = new Properties(); } var props = customProps.Properties; if (props != null) { // This will trigger an exception if the property's Name // property is null, but if that happens, the property is damaged, // and probably should raise an exception. var prop = props.Where(p => ((CustomDocumentProperty)p).Name.Value == propertyName).FirstOrDefault(); // Does the property exist? If so, get the return value, // and then delete the property. if (prop != null) { returnValue = prop.InnerText; prop.Remove(); } // Append the new property, and // fix up all the property ID values. // The PropertyId value must start at 2. props.AppendChild(newProp); int pid = 2; foreach (CustomDocumentProperty item in props) { item.PropertyId = pid++; } props.Save(); } return(returnValue); }