logo
down
shadow

DocumentFormat.OpenXml Modify Creator Propery of Document


DocumentFormat.OpenXml Modify Creator Propery of Document

Content Index :

DocumentFormat.OpenXml Modify Creator Propery of Document
Tag : chash , By : James
Date : November 24 2020, 03:01 PM

I wish this help you I wish to replace Word Document Creator metadata with something else but its not persisted when calling WordprocessingDocument.Save(). I am utilising DocumentFormat.OpenXML version 2.7.2 and the following code to load and change the Creator: , can you try this :
// Change MemoryStream by FileStream
public static SetCreator(FileStream inputStream, string newCreator)
{
    using (WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true))
    {
        document.PackageProperties.Creator = newCreator;
        // You shouldn't need to do document.Save()
    }
}

// Main code
path = "C:\myPath";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    SetCreator(fs, "ME");
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Update TOC in .docx document using DocumentFormat.OpenXml (C#)


Tag : chash , By : nseibert
Date : March 29 2020, 07:55 AM

Documentformat.openxml - create word document with default styles


Tag : chash , By : Gilmar Souza Jr.
Date : March 29 2020, 07:55 AM
it fixes the issue If you're like me and you found this post because you were trying to build documents using OpenXML with the default styles "Heading 1", "Heading 2", "Title", etc. you get when you use Microsoft Word I found the solution after a few hours.
First I tried to find the styles in the normal template "Normal.dotm". This is not where the styles are stored, you are looking in the wrong place. The default styles are actually defined in a "Default.dotx" file in a directory named QuickStyles.
void CreateWordDocumentUsingMSWordStyles(string outputPath, string templatePath)
{
    // create a copy of the template and open the copy
    System.IO.File.Copy(templatePath, outputPath, true);

    using (var document = WordprocessingDocument.Open(outputPath, true))
    {
        document.ChangeDocumentType(WordprocessingDocumentType.Document);

        var mainPart = document.MainDocumentPart;
        var settings = mainPart.DocumentSettingsPart;

        var templateRelationship = new AttachedTemplate { Id = "relationId1" };
        settings.Settings.Append(templateRelationship);

        var templateUri = new Uri("c:\\anything.dotx", UriKind.Absolute); // you can put any path you like and the document styles still work
        settings.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", templateUri, templateRelationship.Id);

        // using Title as it would appear in Microsoft Word
        var paragraphProps = new ParagraphProperties();
        paragraphProps.ParagraphStyleId = new ParagraphStyleId { Val = "Title" };

        // add some text with the "Title" style from the "Default" style set supplied by Microsoft Word
        var run = new Run();
        run.Append(new Text("My Title!"));

        var paragraph = new Paragraph();
        paragraph.Append(paragraphProps);
        paragraph.Append(run);

        mainPart.Document.Body.Append(paragraph);

        mainPart.Document.Save();
    }
}
var path = System.IO.Path.GetTempFileName();

CreateWordDocumentUsingMSWordStyles(path, "C:\\Program Files (x86)\\Microsoft Office\\Office14\\1033\\QuickStyles\\Default.dotx");

Can I refresh all the Word Document metadata using DocumentFormat.OpenXml


Tag : development , By : Sandip
Date : March 29 2020, 07:55 AM
it should still fix some issue Thank you to @Cindy Meister for the answer.
I was hoping that there might be some method or other in the DocumentFormat.OpenXML SDK that I could call, but it seems that is not possible.

Unable to integrate OpenXML (documentformat.openxml.*) in C# bot framework, getting the error " Autofac.Core.Depend


Tag : chash , By : Arun Thomas
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further For all those who have been working on a similar problem, the solution would be to proceed with following OpenXml nuget packages:
DocumentFormat.OpenXml;

create a new document from word template with multiple pages using documentformat.openxml


Tag : development , By : Cosmin
Date : March 29 2020, 07:55 AM
hope this fix your issue The sample code below (which is unit-tested and works) does what you are trying to achieve. It is based on the following interpretation of the question and assumptions:
"Control place holders" means "Rich Text content controls", which are called block-level structured document tags (SDTs) in Open XML lingo and are thus represented by the SdtBlock class in the Open XML SDK. The content controls have tags, meaning the relevant w:sdt elements have grandchild elements like . Those tags are used to link the data received from the REST service to the content controls. The data is provided as a Dictionary, mapping tag values to the content control text (data).
public class ContentControlWriter
{
    private readonly IDictionary<string, string> _contentMap;

    /// <summary>
    /// Initializes a new ContentControlWriter instance.
    /// </summary>
    /// <param name="contentMap">The mapping of content control tags to content control texts.
    /// </param>
    public ContentControlWriter(IDictionary<string, string> contentMap)
    {
        _contentMap = contentMap;
    }

    /// <summary>
    /// Transforms the given WordprocessingDocument by setting the content
    /// of relevant block-level content controls.
    /// </summary>
    /// <param name="wordDocument">The WordprocessingDocument to be transformed.</param>
    public void WriteContentControls(WordprocessingDocument wordDocument)
    {
        MainDocumentPart part = wordDocument.MainDocumentPart;
        part.Document = (Document) TransformDocument(part.Document);
    }

    private object TransformDocument(OpenXmlElement element)
    {
        if (element is SdtBlock sdt)
        {
            string tagValue = GetTagValue(sdt);
            if (_contentMap.TryGetValue(tagValue, out string text))
            {
                return TransformSdtBlock(sdt, text);
            }
        }

        return Transform(element, TransformDocument);
    }

    private static object TransformSdtBlock(OpenXmlElement element, string text)
    {
        return element is SdtContentBlock
            ? new SdtContentBlock(new Paragraph(new Run(new Text(text))))
            : Transform(element, e => TransformSdtBlock(e, text));
    }

    private static string GetTagValue(SdtElement sdt) => sdt
        .Descendants<Tag>()
        .Select(tag => tag.Val.Value)
        .FirstOrDefault();

    private static T Transform<T>(T element, Func<OpenXmlElement, object> transformation)
        where T : OpenXmlElement
    {
        var transformedElement = (T) element.CloneNode(false);
        transformedElement.Append(element.Elements().Select(e => (OpenXmlElement) transformation(e)));
        return transformedElement;
    }
}
Related Posts Related QUESTIONS :
  • In C#, why can't a List<string> object be stored in a List<object> variable
  • C# loop - break vs. continue
  • How to access .Net element on Master page from a Content page?
  • Why are unsigned int's not CLS compliant?
  • Why is Array.Length an int, and not an uint
  • How do I make event callbacks into my win forms thread safe?
  • Genealogy Tree Control
  • WCF Service - Backward compatibility issue
  • Binary patch-generation in C#
  • Tab Escape Character?
  • When do Request.Params and Request.Form differ?
  • The imported project "C:\Microsoft.CSharp.targets" was not found
  • Numeric Data Entry in WPF
  • Print a Winform/visual element
  • C# logic order and compiler behavior
  • When to use an extension method with lambda over LINQtoObjects to filter a collection?
  • How to make a button appear as if it is pressed?
  • C# and Arrow Keys
  • How do you resolve a domain name to an IP address with .NET/C#?
  • Should the folders in a solution match the namespace?
  • How can I evaluate C# code dynamically?
  • CSharpCodeProvider Compilation Performance
  • How can I create Prototype Methods (like JavaScript) in C#.Net?
  • DataTable Loop Performance Comparison
  • CSV string handling
  • What is the best way to do unit testing for ASP.NET 2.0 web pages?
  • High availability
  • What to use for Messaging with C#
  • Accessing a Dictionary.Keys Key through a numeric index
  • ConfigurationManager.AppSettings Performance Concerns
  • What Are Some Good .NET Profilers?
  • Is this a good way to determine OS Architecture?
  • How to create a tree-view preferences dialog type of interface in C#?
  • Searching directories for tons of files?
  • Can I have a method returning IEnumerator<T> and use it in a foreach loop?
  • Why can't I have abstract static methods in C#?
  • Displaying ad content from Respose.WriteFile()/ Response.ContentType
  • Convert integers to written numbers
  • Absolute path back to web-relative path
  • How can we generate getters and setters in Visual Studio?
  • Bringing Window to the Front in C# using Win32 API
  • Possible to "spin off" several GUI threads? (Not halting the system at Application.Run)
  • Reading a C/C++ data structure in C# from a byte array
  • How should I translate from screen space coordinates to image space coordinates in a WinForms PictureBox?
  • Setting Objects to Null/Nothing after use in .NET
  • Converting ARBG to RGB with alpha blending
  • Is it better to create Model classes or stick with generic database utility class?
  • Passing enum type to Converter with integer value
  • Pool of objects with objects that are already on the scene in advance
  • StatusBar text fade-out when binding using Caliburn.Micro
  • Queryfilter on ApplicationUser in OnModelCreating in ApplicationDbContext creates StackOverflowException
  • How to get record form a different table based on a value from first table with linq expression?
  • Show data in Grid from returned model
  • Using Attributes to Override Data Model Conventions
  • Basic OOP console calculator, result Error
  • Compositon and Repository pattern
  • Multiple using statements with if condition
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • How to add a column in an existing AspNetUsers table
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com