本文整理汇总了C#中WordprocessingDocument.AddCustomFilePropertiesPart方法的典型用法代码示例。如果您正苦于以下问题:C# WordprocessingDocument.AddCustomFilePropertiesPart方法的具体用法?C# WordprocessingDocument.AddCustomFilePropertiesPart怎么用?C# WordprocessingDocument.AddCustomFilePropertiesPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WordprocessingDocument
的用法示例。
在下文中一共展示了WordprocessingDocument.AddCustomFilePropertiesPart方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyStartingParts
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);
//.........这里部分代码省略.........
示例2: setCustomProperty
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;
}