예제 #1
0
        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++;
                }
            }
        }
        public void Bug225919Test()
        {
            string propertiesXml = "<ap:Properties xmlns:ap=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\">" +
                                   "<ap:TitlesOfParts>" +
                                   "<vt:vector baseType=\"lpstr\" size=\"1\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">" +
                                   "<vt:lpstr />" +
                                   "</vt:vector>" +
                                   "</ap:TitlesOfParts>" +
                                   "</ap:Properties>";

            var properties = new DocumentFormat.OpenXml.ExtendedProperties.Properties();
            var property   = properties.AppendChild(new DocumentFormat.OpenXml.ExtendedProperties.TitlesOfParts());

            property.VTVector          = new DocumentFormat.OpenXml.VariantTypes.VTVector();
            property.VTVector.BaseType = DocumentFormat.OpenXml.VariantTypes.VectorBaseValues.Lpstr;
            property.VTVector.Size     = 1;
            property.VTVector.AppendChild(new DocumentFormat.OpenXml.VariantTypes.VTLPSTR());

            Assert.Equal(propertiesXml, properties.OuterXml);

            propertiesXml = "<op:Properties xmlns:op=\"http://schemas.openxmlformats.org/officeDocument/2006/custom-properties\">" +
                            "<op:property fmtid=\"{D5CDD505-2E9C-101B-9397-08002B2CF9AE}\" pid=\"2\" name=\"crap\">" +
                            "<vt:bool xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">true</vt:bool>" +
                            "</op:property>" +
                            "</op:Properties>";

            var customProperties = new DocumentFormat.OpenXml.CustomProperties.Properties();
            var foo = new DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty();

            foo.Name = "crap";

            // format id is required, i copied this from a Word document.
            foo.FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}";

            // property id is required.
            foo.PropertyId = 2;
            foo.VTBool     = new DocumentFormat.OpenXml.VariantTypes.VTBool("true");
            customProperties.AppendChild(foo);

            Assert.Equal(propertiesXml, customProperties.OuterXml);

            var clone = customProperties.CloneNode(true);

            Assert.Equal(0, clone.GetAttributes().Count);
            Assert.Equal(propertiesXml, clone.OuterXml);
        }
        public static void GenerateCustomFilePropertiesPart1Content(CustomFilePropertiesPart customFilePropertiesPart1)
        {
            var properties2 = new DocumentFormat.OpenXml.CustomProperties.Properties();

            properties2.AddNamespaceDeclaration("vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes");

            var customDocumentProperty1 = new DocumentFormat.OpenXml.CustomProperties.CustomDocumentProperty()
            {
                FormatId = "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}", PropertyId = 2, Name = "ContentTypeId"
            };
            var vTLPWSTR1 =
                new DocumentFormat.OpenXml.VariantTypes.VTLPWSTR {
                Text = "0x0101004B3CC135CC07AD41A19C6A3D7A557156"
            };

            customDocumentProperty1.Append(vTLPWSTR1);

            properties2.Append(customDocumentProperty1);

            customFilePropertiesPart1.Properties = properties2;
        }