Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created January 12, 2023 11:20
Show Gist options
  • Save devmnj/27a01ee0ec31314ba9f33be05d886062 to your computer and use it in GitHub Desktop.
Save devmnj/27a01ee0ec31314ba9f33be05d886062 to your computer and use it in GitHub Desktop.
Create word document using Open XML SDK in C#.net
public static StyleDefinitionsPart AddStylesPartToPackage2(WordprocessingDocument doc)
{
StyleDefinitionsPart part;
part = doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
Styles root = new Styles();
root.Save(part);
return part;
}
// Create a new style with the specified styleid and stylename and add it to the specified
// style definitions part.
private static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart,
string styleid, string stylename)
{
// Get access to the root element of the styles part.
Styles styles = styleDefinitionsPart.Styles;
// Create a new paragraph style and specify some of the properties.
Style style = new Style()
{
Type = StyleValues.Paragraph,
StyleId = styleid,
CustomStyle = true
};
StyleName styleName1 = new StyleName() { Val = stylename };
BasedOn basedOn1 = new BasedOn() { Val = "Normal" };
NextParagraphStyle nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" };
style.Append(styleName1);
style.Append(basedOn1);
style.Append(nextParagraphStyle1);
// Create the StyleRunProperties object and specify some of the run properties.
StyleRunProperties styleRunProperties1 = new StyleRunProperties();
DocumentFormat.OpenXml.Wordprocessing.Bold bold1 = new DocumentFormat.OpenXml.Wordprocessing.Bold();
DocumentFormat.OpenXml.Wordprocessing.Color color1 = new DocumentFormat.OpenXml.Wordprocessing.Color { ThemeColor = ThemeColorValues.Accent2 };
RunFonts font1 = new RunFonts() { Ascii = "Lucida Console" };
Italic italic1 = new Italic();
// Specify a 12 point size.
FontSize fontSize1 = new FontSize() { Val = "24" };
styleRunProperties1.Append(bold1);
styleRunProperties1.Append(color1);
styleRunProperties1.Append(font1);
styleRunProperties1.Append(fontSize1);
styleRunProperties1.Append(italic1);
// Add the run properties to the style.
style.Append(styleRunProperties1);
// Add the style to the styles part.
styles.Append(style);
}
// Return true if the style id is in the document, false otherwise.
public static bool IsStyleIdInDocument(WordprocessingDocument doc,
string styleid)
{
// Get access to the Styles element for this document.
Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;
// Check that there are styles and how many.
int n = s.Elements<Style>().Count();
if (n == 0)
return false;
// Look for a match on styleid.
Style style = s.Elements<Style>()
.Where(st => (st.StyleId == styleid) && (st.Type == StyleValues.Paragraph))
.FirstOrDefault();
if (style == null)
return false;
return true;
}
// Apply a style to a paragraph.
public static void ApplyStyleToParagraph(WordprocessingDocument doc,
string styleid, string stylename, Paragraph p)
{
// If the paragraph has no ParagraphProperties object, create one.
if (p.Elements<ParagraphProperties>().Count() == 0)
{
p.PrependChild<ParagraphProperties>(new ParagraphProperties());
}
// Get the paragraph properties element of the paragraph.
ParagraphProperties pPr = p.Elements<ParagraphProperties>().First();
// Get the Styles part for this document.
StyleDefinitionsPart part =
doc.MainDocumentPart.StyleDefinitionsPart;
// If the Styles part does not exist, add it and then add the style.
if (part == null)
{
part = AddStylesPartToPackage(doc);
AddNewStyle(part, styleid, stylename);
}
else
{
// If the style is not in the document, add it.
if (IsStyleIdInDocument(doc, styleid) != true)
{
// No match on styleid, so let's try style name.
string styleidFromName = GetStyleIdFromStyleName(doc, stylename);
if (styleidFromName == null)
{
AddNewStyle(part, styleid, stylename);
}
else
styleid = styleidFromName;
}
}
// Set the style of the paragraph.
pPr.ParagraphStyleId = new ParagraphStyleId() { Val = styleid };
}
public static string GetStyleIdFromStyleName(WordprocessingDocument doc, string styleName)
{
StyleDefinitionsPart stylePart = doc.MainDocumentPart.StyleDefinitionsPart;
string styleId = stylePart.Styles.Descendants<StyleName>()
.Where(s => s.Val.Value.Equals(styleName) &&
(((Style)s.Parent).Type == StyleValues.Paragraph))
.Select(n => ((Style)n.Parent).StyleId).FirstOrDefault();
return styleId;
}
private void button_word2(object sender, EventArgs e)
{
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Create("test11.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = doc.AddMainDocumentPart();
var styleid = "ddd";
var stylename = "xx";
// Create the document structure and add some text.
mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
Body body = mainPart.Document.AppendChild(new Body());
DocumentFormat.OpenXml.Wordprocessing.Paragraph para = new Paragraph();
Run run = new Run();
Text text = new Text("Some text");
run.Append(text);
para.Append(run);
body.InsertAt<Paragraph>(para, 0);
Paragraph p = mainPart.Document.Body.Descendants<Paragraph>().ElementAtOrDefault(0);
// Check for a null reference.
if (p == null)
{
throw new ArgumentOutOfRangeException("p","Paragraph was not found.");
}
ApplyStyleToParagraph(doc, "OverdueAmount", "Overdue Amount", p);
}
}
catch (Exception)
{
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment