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
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
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;
}
}
|