private void SetStyles(MainDocumentPart mainDocumentPart) { StyleDefinitionsPart styleDefinitionsPart1 = mainDocumentPart.AddNewPart <StyleDefinitionsPart>("rId1"); Styles styles1 = new Styles();// { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 w15" } }; //styles1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); //styles1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); //styles1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); //styles1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); //styles1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); styles1.Append(SetRunPropertyDefault()); Style style1 = new Style() { Type = StyleValues.Paragraph, StyleId = "Normal", Default = true }; StyleName styleName1 = new StyleName() { Val = "Normal" }; PrimaryStyle primaryStyle1 = new PrimaryStyle(); style1.Append(styleName1); style1.Append(primaryStyle1); Style style2 = new Style() { Type = StyleValues.Character, StyleId = "DefaultParagraphFont", Default = true }; StyleName styleName2 = new StyleName() { Val = "Default Paragraph Font" }; UIPriority uIPriority1 = new UIPriority() { Val = 1 }; SemiHidden semiHidden1 = new SemiHidden(); UnhideWhenUsed unhideWhenUsed1 = new UnhideWhenUsed(); style2.Append(styleName2); style2.Append(uIPriority1); style2.Append(semiHidden1); style2.Append(unhideWhenUsed1); Style style3 = new Style() { Type = StyleValues.Table, StyleId = "TableNormal", Default = true }; StyleName styleName3 = new StyleName() { Val = "Normal Table" }; UIPriority uIPriority2 = new UIPriority() { Val = 99 }; SemiHidden semiHidden2 = new SemiHidden(); UnhideWhenUsed unhideWhenUsed2 = new UnhideWhenUsed(); StyleTableProperties styleTableProperties1 = new StyleTableProperties(); TableIndentation tableIndentation1 = new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa }; TableCellMarginDefault tableCellMarginDefault1 = new TableCellMarginDefault(); TopMargin topMargin1 = new TopMargin() { Width = "0", Type = TableWidthUnitValues.Dxa }; TableCellLeftMargin tableCellLeftMargin1 = new TableCellLeftMargin() { Width = 108, Type = TableWidthValues.Dxa }; BottomMargin bottomMargin1 = new BottomMargin() { Width = "0", Type = TableWidthUnitValues.Dxa }; TableCellRightMargin tableCellRightMargin1 = new TableCellRightMargin() { Width = 108, Type = TableWidthValues.Dxa }; tableCellMarginDefault1.Append(topMargin1); tableCellMarginDefault1.Append(tableCellLeftMargin1); tableCellMarginDefault1.Append(bottomMargin1); tableCellMarginDefault1.Append(tableCellRightMargin1); styleTableProperties1.Append(tableIndentation1); styleTableProperties1.Append(tableCellMarginDefault1); style3.Append(styleName3); style3.Append(uIPriority2); style3.Append(semiHidden2); style3.Append(unhideWhenUsed2); style3.Append(styleTableProperties1); Style style4 = new Style() { Type = StyleValues.Numbering, StyleId = "NoList", Default = true }; StyleName styleName4 = new StyleName() { Val = "No List" }; UIPriority uIPriority3 = new UIPriority() { Val = 99 }; SemiHidden semiHidden3 = new SemiHidden(); UnhideWhenUsed unhideWhenUsed3 = new UnhideWhenUsed(); style4.Append(styleName4); style4.Append(uIPriority3); style4.Append(semiHidden3); style4.Append(unhideWhenUsed3); styles1.Append(style1); styles1.Append(style2); styles1.Append(style3); styles1.Append(style4); styleDefinitionsPart1.Styles = styles1; }
private void CreateProjectHeadingStyle() { // Get the Styles part for this document. StyleDefinitionsPart styleDefinitionsPart = _document.MainDocumentPart.StyleDefinitionsPart; // 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 = "ProjectHeading", CustomStyle = true }; StyleName styleName = new StyleName() { Val = "ProjectHeading" }; BasedOn basedOn = new BasedOn() { Val = "Normal" }; NextParagraphStyle nextParagraphStyle = new NextParagraphStyle() { Val = "Normal" }; style.Append(styleName); style.Append(basedOn); style.Append(nextParagraphStyle); // Create the StyleRunProperties object and specify some of the run properties. StyleRunProperties styleRunProperties = new StyleRunProperties(); Bold bold = new Bold(); // More info: https://stackoverflow.com/a/17221250/9017481 Color color = new Color() { Val = "365F91", ThemeColor = ThemeColorValues.Accent1, ThemeShade = "BF" }; RunFonts font = new RunFonts() { Ascii = "Calibri Light (Headings)" }; // Specify a 18 point size. FontSize fontSize = new FontSize() { Val = "28" }; styleRunProperties.Append(bold); styleRunProperties.Append(color); styleRunProperties.Append(font); styleRunProperties.Append(fontSize); // Centering text // https://stackoverflow.com/a/24827814/9017481 ParagraphProperties paragraphRunProperties = new ParagraphProperties(); Justification CenterHeading = new Justification { Val = JustificationValues.Left }; paragraphRunProperties.Append(CenterHeading); // Add the run properties to the style. style.Append(styleRunProperties); style.Append(paragraphRunProperties); // Add the style to the styles part. styles.Append(style); }
public void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart part) { Styles doc_styles = GenerateDocumentStyles(); DocDefaults document_defaults = new DocDefaults(); RunPropertiesDefault defaultRunProperties = new RunPropertiesDefault(CreateRunBaseStyle()); document_defaults.Append(defaultRunProperties); ParagraphPropertiesBaseStyle paragraphBaseStyle = new ParagraphPropertiesBaseStyle(); paragraphBaseStyle.Append(new SpacingBetweenLines() { After = "200", Line = "276", LineRule = LineSpacingRuleValues.Auto }); ParagraphPropertiesDefault default_ParagraphProperties = new ParagraphPropertiesDefault(); default_ParagraphProperties.Append(paragraphBaseStyle); document_defaults.Append(default_ParagraphProperties); LatentStyles latentStyles1 = new LatentStyles() { DefaultLockedState = false, DefaultUiPriority = 99, DefaultSemiHidden = true, DefaultUnhideWhenUsed = true, DefaultPrimaryStyle = false, Count = 267 }; latentStyles1.Append(new LatentStyleExceptionInfo() { Name = "Normal", UiPriority = 0, SemiHidden = false, UnhideWhenUsed = false, PrimaryStyle = true }); Style Normal = GenerateNormal(); doc_styles.Append(document_defaults); doc_styles.Append(latentStyles1); doc_styles.Append(Normal); for (int i = 1; i <= 7; i++) { latentStyles1.Append(new LatentStyleExceptionInfo() { Name = "Heading " + i, UiPriority = 9, SemiHidden = false, UnhideWhenUsed = false, PrimaryStyle = true }); Style Header = GenerateHeader(i); doc_styles.Append(Header); } part.Styles = doc_styles; }
// Create a new paragraph style with the specified style ID, primary style name, and aliases and // add it to the specified style definitions part. public static void CreateAndAddParagraphStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename, string aliases = "") { // Access the root element of the styles part. Styles styles = styleDefinitionsPart.Styles; if (styles == null) { styleDefinitionsPart.Styles = new Styles(); styleDefinitionsPart.Styles.Save(); } // Create a new paragraph style element and specify some of the attributes. Style style = new Style() { Type = StyleValues.Paragraph, StyleId = styleid, CustomStyle = true, Default = false }; // Create and add the child elements (properties of the style). Aliases aliases1 = new Aliases() { Val = aliases }; AutoRedefine autoredefine1 = new AutoRedefine() { Val = OnOffOnlyValues.Off }; BasedOn basedon1 = new BasedOn() { Val = "Normal" }; LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "OverdueAmountChar" }; Locked locked1 = new Locked() { Val = OnOffOnlyValues.Off }; PrimaryStyle primarystyle1 = new PrimaryStyle() { Val = OnOffOnlyValues.On }; StyleHidden stylehidden1 = new StyleHidden() { Val = OnOffOnlyValues.Off }; SemiHidden semihidden1 = new SemiHidden() { Val = OnOffOnlyValues.Off }; StyleName styleName1 = new StyleName() { Val = stylename }; NextParagraphStyle nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" }; UIPriority uipriority1 = new UIPriority() { Val = 1 }; UnhideWhenUsed unhidewhenused1 = new UnhideWhenUsed() { Val = OnOffOnlyValues.On }; if (aliases != "") { style.Append(aliases1); } style.Append(autoredefine1); style.Append(basedon1); style.Append(linkedStyle1); style.Append(locked1); style.Append(primarystyle1); style.Append(stylehidden1); style.Append(semihidden1); style.Append(styleName1); style.Append(nextParagraphStyle1); style.Append(uipriority1); style.Append(unhidewhenused1); // Create the StyleRunProperties object and specify some of the run properties. StyleRunProperties styleRunProperties1 = new StyleRunProperties(); Bold bold1 = new Bold(); Color color1 = new 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); }
public string Post([FromBody] Models.Presentation.AgendaPostModel agenda) { string filePath = @"\\844dc2\Residential Incidents\"; string fileName = string.Format("{0}{1}.docx", filePath, agenda.agendaTitle); //MemoryStream documentStream = new MemoryStream(); using (WordprocessingDocument agendaDocument = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)) //using (WordprocessingDocument agendaDocument = WordprocessingDocument.Create(documentStream, WordprocessingDocumentType.Document)) { var reports = from r in this._db.IncidentReports join u in this._db.Users on r.userId equals u.userId join p in this._db.IncidentPrograms on r.programId equals p.incidentProgramId join s in this._db.ReportStatuses on r.statusId equals s.reportStatusId from d in this._db.IncidentDetails.Where(d => d.incidentId == r.incidentId).DefaultIfEmpty() where r.incidentReportTypeId == 1 && r.incidentDate >= agenda.fromDate && r.incidentDate <= agenda.toDate select new { incidentId = r.incidentId, clientName = r.clientName, clientDob = r.clientDob, programTitle = p.programTitle, reportingAgency = r.reportingAgency, incidentDate = r.incidentDate, createdStamp = r.createdStamp, lastModified = r.lastModified, createdByName = u.firstName + " " + u.lastName, statusId = r.statusId, currentStatus = s.reportStatusText, incidentDetails = d.incidentDetails, staffs = ( from st in this._db.IncidentStaffs join e in this._db.Users on st.userId equals e.userId where st.incidentId == r.incidentId select new { staffName = e.firstName + " " + e.lastName } ) }; MainDocumentPart root = agendaDocument.AddMainDocumentPart(); root.Document = new Document(); Body body = root.Document.AppendChild(new Body()); // PAGE MARGINS SectionProperties sectionProperties = new SectionProperties(); PageMargin pageMargin = new PageMargin() { Top = 720, Right = (UInt32Value)720U, Bottom = 720, Left = (UInt32Value)720U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }; sectionProperties.Append(pageMargin); root.Document.Body.Append(sectionProperties); // DOCUMENT STYLES StyleDefinitionsPart styleDefinitionsPart = root.AddNewPart <StyleDefinitionsPart>(); Styles styles = new Styles(); styles.Save(styleDefinitionsPart); Style style = new Style() { Type = StyleValues.Paragraph, StyleId = "AgendaStyle", CustomStyle = true }; StyleName styleHeading1 = new StyleName() { Val = "Heading1" }; style.Append(styleHeading1); StyleRunProperties styleRunPropertiesHeading1 = new StyleRunProperties(); styleRunPropertiesHeading1.Append(new Bold()); styleRunPropertiesHeading1.Append(new RunFonts() { Ascii = "Calibri" }); styleRunPropertiesHeading1.Append(new FontSize() { Val = "24" }); // Sizes are in half-points. Oy! style.Append(styleRunPropertiesHeading1); styles.Append(style); foreach (var report in reports) { //Paragraph para = body.AppendChild(new Paragraph()); //Run run = para.AppendChild(new Run()); // CLIENT NAME HEADER Paragraph clientNamePara = body.AppendChild(new Paragraph()); clientNamePara.AppendChild(new Run(new Text(report.clientName))); clientNamePara.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val = "Heading1" }); // REPORT DETAILS Paragraph infoPara = body.AppendChild(new Paragraph()); Run infoRun = infoPara.AppendChild(new Run()); infoRun.AppendChild(new Text(string.Format("Program: {0}", report.programTitle))); infoRun.AppendChild(new Break()); infoRun.AppendChild(new Text(string.Format("Reporting Agency: {0}", report.reportingAgency))); infoRun.AppendChild(new Break()); infoRun.AppendChild(new Text(string.Format("Date of Incident: {0}", report.incidentDate.ToShortDateString()))); infoRun.AppendChild(new Break()); infoRun.AppendChild(new Text(string.Format("Staff: {0}", report.createdByName))); // INCIDENT DETAILS Paragraph detailsPara = body.AppendChild(new Paragraph()); Run detailsRun = detailsPara.AppendChild(new Run()); detailsRun.AppendChild(new Text("Details of Incident: ")); detailsRun.AppendChild(new Break()); if (report.incidentDetails == string.Empty) { detailsRun.AppendChild(new Text("<No details given. Report incomplete.>")); } else { detailsRun.AppendChild(new Text(report.incidentDetails)); } // ADDITIONAL STAFF INVOLVED Paragraph staffPara = body.AppendChild(new Paragraph()); Run staffRun = staffPara.AppendChild(new Run()); staffRun.AppendChild(new Text("Additional Staff Involved:")); staffRun.AppendChild(new Break()); if (report.staffs.ToList().Any()) { foreach (var staff in report.staffs) { staffRun.AppendChild(new Text(staff.staffName)); staffRun.AppendChild(new Break()); } } else { staffRun.AppendChild(new Text("No additional staff identified.")); staffRun.AppendChild(new Break()); } // ACTIONS TAKEN Paragraph actionsPara = body.AppendChild(new Paragraph()); Run actionsRun = actionsPara.AppendChild(new Run(new Text("Actions Taken"))); // PATTERNS Paragraph patternsPara = body.AppendChild(new Paragraph()); Run patternsRun = patternsPara.AppendChild(new Run(new Text("Pattern Behavior/Recommendation"))); // PAGE BREAK body.AppendChild(new Paragraph( new Run( new Break() { Type = BreakValues.Page }))); } //run.AppendChild(new Text("From Date: " + agenda.fromDate.ToShortDateString() + " to " + agenda.toDate.ToShortDateString())); } SessionController session = new SessionController(); var user = session.Get(); session.Dispose(); FileStream documentStream = new FileStream(fileName, FileMode.Open); MailMessage msg = new MailMessage(); msg.To.Add(new MailAddress(user.userEmail)); //msg.Bcc.Add(new MailAddress("*****@*****.**")); msg.From = new MailAddress("*****@*****.**"); msg.Subject = "CFS Incident Reports: Agenda Document"; msg.IsBodyHtml = true; msg.Attachments.Add(new System.Net.Mail.Attachment(documentStream, agenda.agendaTitle + ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")); StringBuilder messageBody = new StringBuilder(); messageBody.Append("<h1>Incident Reports Agenda</h1>"); messageBody.Append("<p>An agenda document has been created and is attached.</p>"); messageBody.Append("<p>A copy has been saved <a href=\"\\\\844dc2\\Residential Incidents\\\">here</a>.</p>"); msg.Body = messageBody.ToString(); SmtpClient smtp = new SmtpClient("cfs-mailserv"); smtp.Send(msg); smtp.Dispose(); msg.Dispose(); documentStream.Close(); documentStream.Dispose(); return(fileName); }
public static void Test_Header_01(bool header = false, bool footer = false, bool pageNumber = false) { SetDirectory(); string file = "test_header_01.docx"; Trace.WriteLine("create docx \"{0}\" using OXmlDoc", file); using (WordprocessingDocument doc = WordprocessingDocument.Create(zPath.Combine(_directory, file), WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = doc.AddMainDocumentPart(); if (header || footer) { // activate even and odd headers DocumentSettingsPart documentSettingsPart = mainPart.AddNewPart <DocumentSettingsPart>(); Settings settings = new Settings(); // <w:evenAndOddHeaders /> settings.AppendChild(new EvenAndOddHeaders()); documentSettingsPart.Settings = settings; } mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Styles styles = Test_OpenXml_Creator.CreateStyles(mainPart); //styles.DocDefaults = Test_OXmlCreator.CreateDocDefaults(); string headerStyleId = null; if (header) { headerStyleId = "Header"; styles.Append(Test_OpenXml_Creator.CreateHeaderFooterStyle(headerStyleId)); } string footerStyleId = null; if (footer) { footerStyleId = "Footer"; styles.Append(Test_OpenXml_Creator.CreateHeaderFooterStyle(footerStyleId)); } string defaultHeaderPartId = null; string firstHeaderPartId = null; string evenHeaderPartId = null; if (header) { defaultHeaderPartId = Test_OpenXml_Creator.CreateHeader(mainPart, Test_OpenXml_Creator.CreateParagraph_01(headerStyleId, "Default header", tab: 1)); firstHeaderPartId = Test_OpenXml_Creator.CreateHeader(mainPart, Test_OpenXml_Creator.CreateParagraph_01(headerStyleId, "First header", tab: 1)); evenHeaderPartId = Test_OpenXml_Creator.CreateHeader(mainPart, Test_OpenXml_Creator.CreateParagraph_01(headerStyleId, "Even header", tab: 1)); } string defaultFooterPartId = null; string firstFooterPartId = null; string evenFooterPartId = null; if (footer) { OpenXmlCompositeElement element; if (pageNumber) { element = Test_OpenXml_Creator.CreateParagraph_PageNumber(footerStyleId, "Default footer"); } else { element = Test_OpenXml_Creator.CreateParagraph_01(footerStyleId, "Default footer", tab: 1); } defaultFooterPartId = Test_OpenXml_Creator.CreateFooter(mainPart, element); if (pageNumber) { element = Test_OpenXml_Creator.CreateParagraph_PageNumber(footerStyleId, "First footer"); } else { element = Test_OpenXml_Creator.CreateParagraph_01(footerStyleId, "First footer", tab: 1); } firstFooterPartId = Test_OpenXml_Creator.CreateFooter(mainPart, element); if (pageNumber) { element = Test_OpenXml_Creator.CreateParagraph_PageNumber(footerStyleId, "Even footer"); } else { element = Test_OpenXml_Creator.CreateParagraph_01(footerStyleId, "Even footer", tab: 1); } evenFooterPartId = Test_OpenXml_Creator.CreateFooter(mainPart, element); } // for SectionProperties mainPart.Document.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); //AddSection(body, defaultHeaderPartId, defaultFooterPartId, firstHeaderPartId, firstFooterPartId); SectionProperties sectionProperties = body.AppendChild(new SectionProperties()); Test_OpenXml_Creator.SetSectionPage(sectionProperties); Test_OpenXml_Creator.SetSectionHeaders(sectionProperties, defaultHeaderPartId, defaultFooterPartId, firstHeaderPartId, firstFooterPartId, evenHeaderPartId, evenFooterPartId); //Test_OXmlCreator.SetSectionPageNumberType(sectionProperties, start: 1); //AddText(body, 200); body.Append(Test_OpenXml_Creator.CreateText_02(200)); } }
private static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename, Color colour, String fontName, int fontSize, bool italic, bool bold) { // 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(); Bold bold1 = new Bold(); RunFonts font1 = new RunFonts() { Ascii = fontName }; FontSize fontSize1 = new FontSize() { Val = (fontSize * 2).ToString() }; if (bold) { styleRunProperties1.Append(new Bold()); } styleRunProperties1.Append(colour); styleRunProperties1.Append(font1); styleRunProperties1.Append(fontSize1); if (italic) { styleRunProperties1.Append(new Italic()); } // Add the run properties to the style. style.Append(styleRunProperties1); // Add the style to the styles part. styles.Append(style); }
private void CreateStyle(WordprocessingDocument doc, string styleId, string styleName) { // 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); } // Access the root element of the styles part. Styles styles = part.Styles; if (styles == null) { part.Styles = new Styles(); part.Styles.Save(); } // Create a new paragraph style element and specify some of the attributes. Style style = new Style() { Type = StyleValues.Paragraph, StyleId = styleId, CustomStyle = true, Default = false }; // Create and add the child elements (properties of the style). AutoRedefine autoredefine1 = new AutoRedefine() { Val = OnOffOnlyValues.Off }; BasedOn basedon1 = new BasedOn() { Val = "Normal" }; LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "OverdueAmountChar" }; Locked locked1 = new Locked() { Val = OnOffOnlyValues.Off }; PrimaryStyle primarystyle1 = new PrimaryStyle() { Val = OnOffOnlyValues.On }; StyleHidden stylehidden1 = new StyleHidden() { Val = OnOffOnlyValues.Off }; SemiHidden semihidden1 = new SemiHidden() { Val = OnOffOnlyValues.Off }; StyleName styleName1 = new StyleName() { Val = styleName }; NextParagraphStyle nextParagraphStyle1 = new NextParagraphStyle() { Val = "Normal" }; UIPriority uipriority1 = new UIPriority() { Val = 1 }; UnhideWhenUsed unhidewhenused1 = new UnhideWhenUsed() { Val = OnOffOnlyValues.On }; style.Append(autoredefine1); style.Append(basedon1); style.Append(linkedStyle1); style.Append(locked1); style.Append(primarystyle1); style.Append(stylehidden1); style.Append(semihidden1); style.Append(styleName1); style.Append(nextParagraphStyle1); style.Append(uipriority1); style.Append(unhidewhenused1); // Create the StyleRunProperties object and specify some of the run properties. StyleRunProperties styleRunProperties1 = new StyleRunProperties(); Bold bold1 = new Bold(); Color color1 = new Color() { ThemeColor = ThemeColorValues.Hyperlink }; RunFonts font1 = new RunFonts() { Ascii = "Lucida Console" }; Italic italic1 = new Italic(); // Specify a 12 point size. FontSize fontSize1 = new FontSize() { Val = "30" }; 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); }
private void AddStyle(OXmlStyleElement element) { Styles styles = CreateStyles(); Style style = new Style(); style.StyleId = element.Id; style.StyleName = new StyleName() { Val = element.Name }; style.Type = element.StyleType; if (element.Aliases != null) { style.Aliases = new Aliases { Val = element.Aliases } } ; style.CustomStyle = element.CustomStyle; style.Default = element.DefaultStyle; if (element.Locked != null) { style.Locked = new Locked { Val = GetOnOffValue((bool)element.Locked) } } ; if (element.SemiHidden != null) { style.SemiHidden = new SemiHidden { Val = GetOnOffValue((bool)element.SemiHidden) } } ; if (element.StyleHidden != null) { style.StyleHidden = new StyleHidden { Val = GetOnOffValue((bool)element.StyleHidden) } } ; if (element.UnhideWhenUsed != null) { style.UnhideWhenUsed = new UnhideWhenUsed { Val = GetOnOffValue((bool)element.UnhideWhenUsed) } } ; if (element.UIPriority != null) { style.UIPriority = new UIPriority { Val = element.UIPriority } } ; if (element.LinkedStyle != null) { style.LinkedStyle = new LinkedStyle { Val = element.LinkedStyle } } ; if (element.BasedOn != null) { style.BasedOn = new BasedOn { Val = element.BasedOn } } ; if (element.NextParagraphStyle != null) { style.NextParagraphStyle = new NextParagraphStyle { Val = element.NextParagraphStyle } } ; if (element.StyleParagraphProperties != null) { style.StyleParagraphProperties = element.StyleParagraphProperties.ToStyleParagraphProperties(); } if (element.StyleRunProperties != null) { style.StyleRunProperties = element.StyleRunProperties.ToStyleRunProperties(); } styles.Append(style); }
// Create a new character style with the specified style id, style name and aliases and // add it to the specified style definitions part. /// <summary> /// Creates a character style and adds it to the document. /// </summary> /// <param name="styleid">The style Id of the new style. This usually should be the same as the style name, but without spaces.</param> /// <param name="stylename">The style name of the new character style.</param> /// <param name="aliases">The aliases.</param> /// <returns>The style Id of the newly created character style.</returns> public string CreateAndAddCharacterStyle( string styleid, string stylename, string aliases = "") { // Get access to the root element of the styles part. Styles styles = _mainDocumentPart.StyleDefinitionsPart.Styles; // Create a new character style and specify some of the attributes. var style = new Style() { Type = StyleValues.Character, StyleId = styleid, CustomStyle = true }; // Create and add the child elements (properties of the style). var aliases1 = new Aliases() { Val = aliases }; var styleName1 = new StyleName() { Val = stylename }; if (aliases != "") { style.Append(aliases1); } style.Append(styleName1); //LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "OverdueAmountPara" }; //style.Append(linkedStyle1); // Create the StyleRunProperties object and specify some of the run properties. var styleRunProperties1 = new StyleRunProperties(); switch (styleid) { case "CodeInline": SetCodeInlineProperties(styleRunProperties1); break; } /* // Here are some more possibilities how to style * Bold bold1 = new Bold(); * Color color1 = new Color() { ThemeColor = ThemeColorValues.Accent2 }; * RunFonts font1 = new RunFonts() { Ascii = "Tahoma" }; * Italic italic1 = new Italic(); * // Specify a 24 point size. * FontSize fontSize1 = new FontSize() { Val = "48" }; * styleRunProperties1.Append(font1); * styleRunProperties1.Append(fontSize1); * styleRunProperties1.Append(color1); * styleRunProperties1.Append(bold1); * styleRunProperties1.Append(italic1); */ // Add the run properties to the style. style.Append(styleRunProperties1); // Add the style to the styles part. styles.Append(style); return(style.StyleId); }
//..................................................................... /// <summary> /// /// https://docs.microsoft.com/en-us/office/open-xml/how-to-apply-a-style-to-a-paragraph-in-a-word-processing-docxname /// /// Create a new style with the specified styleid and stylename and add it to the specified /// style definitions wbookpart. /// /// 追加一个新的额 STYLE , 样式。 /// </summary> /// <param name="styleDefinitionsPart"></param> /// <param name="styleid"></param> /// <param name="stylename"></param> private void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename) { // Get access to the root element of the styles wbookpart. // Styles styles = styleDefinitionsPart.Styles; // test test test test test // foreach (Style stt in styles) { Console.WriteLine(stt.StyleName + "; " + stt.StyleId); } Console.WriteLine("styles.Count = " + styles.Count( )); // 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); //--------------------------------------------- Bold bold1 = new Bold( ); Color color1 = new 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" }; // Create the StyleRunProperties object and specify some of the run properties. StyleRunProperties styleRunProperties1 = new StyleRunProperties( ); 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 wbookpart. styles.Append(style); return; }
private static void AddNewStyle(StyleDefinitionsPart styleDefinitionsPart, string styleid, string stylename) { Styles styles = styleDefinitionsPart.Styles; 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); StyleRunProperties styleRunProperties1 = new StyleRunProperties(); Bold bold1 = new Bold(); Color color1 = new Color() { Val = "000000" }; RunFonts font1 = new RunFonts() { Ascii = "Times New Roman" }; // Specify a 12 point size. FontSize fontSize1 = new FontSize() { Val = "30" }; styleRunProperties1.Append(bold1); styleRunProperties1.Append(color1); styleRunProperties1.Append(font1); styleRunProperties1.Append(fontSize1); ParagraphProperties paragraphProperties = new ParagraphProperties(); Justification centerHeading = new Justification() { Val = JustificationValues.Center }; OnOffValue hyphenOff = new OnOffValue(false); AutoHyphenation hyphenation = new AutoHyphenation() { Val = hyphenOff }; paragraphProperties.Append(centerHeading); paragraphProperties.Append(hyphenation); style.Append(styleRunProperties1); styles.Append(style); styles.Append(paragraphProperties); }
// Generates content of part. private static Styles GeneratePartContent() { Styles styles = new Styles() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 w15 w16se w16cid" } }; styles.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); styles.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); styles.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); styles.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); styles.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); styles.AddNamespaceDeclaration("w16cid", "http://schemas.microsoft.com/office/word/2016/wordml/cid"); styles.AddNamespaceDeclaration("w16se", "http://schemas.microsoft.com/office/word/2015/wordml/symex"); DocDefaults docDefaults1 = new DocDefaults(); RunPropertiesDefault runPropertiesDefault1 = new RunPropertiesDefault(); RunPropertiesBaseStyle runPropertiesBaseStyle1 = new RunPropertiesBaseStyle(); RunFonts runFonts1 = new RunFonts() { AsciiTheme = ThemeFontValues.MinorHighAnsi, HighAnsiTheme = ThemeFontValues.MinorHighAnsi, EastAsiaTheme = ThemeFontValues.MinorHighAnsi, ComplexScriptTheme = ThemeFontValues.MinorBidi }; FontSize fontSize1 = new FontSize() { Val = "22" }; FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript() { Val = "22" }; Languages languages1 = new Languages() { Val = "en-CA", EastAsia = "en-US", Bidi = "ar-SA" }; runPropertiesBaseStyle1.Append(runFonts1); runPropertiesBaseStyle1.Append(fontSize1); runPropertiesBaseStyle1.Append(fontSizeComplexScript1); runPropertiesBaseStyle1.Append(languages1); runPropertiesDefault1.Append(runPropertiesBaseStyle1); ParagraphPropertiesDefault paragraphPropertiesDefault1 = new ParagraphPropertiesDefault(); ParagraphPropertiesBaseStyle paragraphPropertiesBaseStyle1 = new ParagraphPropertiesBaseStyle(); SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines() { After = "160", Line = "259", LineRule = LineSpacingRuleValues.Auto }; paragraphPropertiesBaseStyle1.Append(spacingBetweenLines1); paragraphPropertiesDefault1.Append(paragraphPropertiesBaseStyle1); docDefaults1.Append(runPropertiesDefault1); docDefaults1.Append(paragraphPropertiesDefault1); Style style1 = new Style() { Type = StyleValues.Paragraph, StyleId = "Normal", Default = true }; StyleName styleName1 = new StyleName() { Val = "Normal" }; PrimaryStyle primaryStyle1 = new PrimaryStyle(); style1.Append(styleName1); style1.Append(primaryStyle1); Style style2 = new Style() { Type = StyleValues.Character, StyleId = "DefaultParagraphFont", Default = true }; StyleName styleName2 = new StyleName() { Val = "Default Paragraph Font" }; UIPriority uIPriority1 = new UIPriority() { Val = 1 }; SemiHidden semiHidden1 = new SemiHidden(); UnhideWhenUsed unhideWhenUsed1 = new UnhideWhenUsed(); style2.Append(styleName2); style2.Append(uIPriority1); style2.Append(semiHidden1); style2.Append(unhideWhenUsed1); Style style3 = new Style() { Type = StyleValues.Table, StyleId = "TableNormal", Default = true }; StyleName styleName3 = new StyleName() { Val = "Normal Table" }; UIPriority uIPriority2 = new UIPriority() { Val = 99 }; SemiHidden semiHidden2 = new SemiHidden(); UnhideWhenUsed unhideWhenUsed2 = new UnhideWhenUsed(); StyleTableProperties styleTableProperties1 = new StyleTableProperties(); TableIndentation tableIndentation1 = new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa }; TableCellMarginDefault tableCellMarginDefault1 = new TableCellMarginDefault(); TopMargin topMargin1 = new TopMargin() { Width = "0", Type = TableWidthUnitValues.Dxa }; TableCellLeftMargin tableCellLeftMargin1 = new TableCellLeftMargin() { Width = 108, Type = TableWidthValues.Dxa }; BottomMargin bottomMargin1 = new BottomMargin() { Width = "0", Type = TableWidthUnitValues.Dxa }; TableCellRightMargin tableCellRightMargin1 = new TableCellRightMargin() { Width = 108, Type = TableWidthValues.Dxa }; tableCellMarginDefault1.Append(topMargin1); tableCellMarginDefault1.Append(tableCellLeftMargin1); tableCellMarginDefault1.Append(bottomMargin1); tableCellMarginDefault1.Append(tableCellRightMargin1); styleTableProperties1.Append(tableIndentation1); styleTableProperties1.Append(tableCellMarginDefault1); style3.Append(styleName3); style3.Append(uIPriority2); style3.Append(semiHidden2); style3.Append(unhideWhenUsed2); style3.Append(styleTableProperties1); Style style4 = new Style() { Type = StyleValues.Numbering, StyleId = "NoList", Default = true }; StyleName styleName4 = new StyleName() { Val = "No List" }; UIPriority uIPriority3 = new UIPriority() { Val = 99 }; SemiHidden semiHidden3 = new SemiHidden(); UnhideWhenUsed unhideWhenUsed3 = new UnhideWhenUsed(); style4.Append(styleName4); style4.Append(uIPriority3); style4.Append(semiHidden3); style4.Append(unhideWhenUsed3); Style style5 = new Style() { Type = StyleValues.Character, StyleId = "RunStyle1", CustomStyle = true }; StyleName styleName5 = new StyleName() { Val = "RunStyle1" }; BasedOn basedOn1 = new BasedOn() { Val = "DefaultParagraphFont" }; UIPriority uIPriority4 = new UIPriority() { Val = 1 }; PrimaryStyle primaryStyle2 = new PrimaryStyle(); Rsid rsid1 = new Rsid() { Val = "00856DAD" }; StyleRunProperties styleRunProperties1 = new StyleRunProperties(); Color color1 = new Color() { Val = "4472C4", ThemeColor = ThemeColorValues.Accent1 }; styleRunProperties1.Append(color1); style5.Append(styleName5); style5.Append(basedOn1); style5.Append(uIPriority4); style5.Append(primaryStyle2); style5.Append(rsid1); style5.Append(styleRunProperties1); Style style6 = new Style() { Type = StyleValues.Character, StyleId = "RunStyle2", CustomStyle = true }; StyleName styleName6 = new StyleName() { Val = "RunStyle2" }; BasedOn basedOn2 = new BasedOn() { Val = "RunStyle1" }; UIPriority uIPriority5 = new UIPriority() { Val = 1 }; PrimaryStyle primaryStyle3 = new PrimaryStyle(); Rsid rsid2 = new Rsid() { Val = "00856DAD" }; StyleRunProperties styleRunProperties2 = new StyleRunProperties(); Bold bold1 = new Bold(); Color color2 = new Color() { Val = "4472C4", ThemeColor = ThemeColorValues.Accent1 }; Languages languages2 = new Languages() { Val = "en-US" }; styleRunProperties2.Append(bold1); styleRunProperties2.Append(color2); styleRunProperties2.Append(languages2); style6.Append(styleName6); style6.Append(basedOn2); style6.Append(uIPriority5); style6.Append(primaryStyle3); style6.Append(rsid2); style6.Append(styleRunProperties2); Style style7 = new Style() { Type = StyleValues.Paragraph, StyleId = "ParagraphStyle1", CustomStyle = true }; StyleName styleName7 = new StyleName() { Val = "ParagraphStyle1" }; BasedOn basedOn3 = new BasedOn() { Val = "Normal" }; PrimaryStyle primaryStyle4 = new PrimaryStyle(); Rsid rsid3 = new Rsid() { Val = "00856DAD" }; StyleParagraphProperties styleParagraphProperties1 = new StyleParagraphProperties(); ParagraphBorders paragraphBorders1 = new ParagraphBorders(); TopBorder topBorder1 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)1U }; LeftBorder leftBorder1 = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)4U }; BottomBorder bottomBorder1 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)1U }; RightBorder rightBorder1 = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)4U }; paragraphBorders1.Append(topBorder1); paragraphBorders1.Append(leftBorder1); paragraphBorders1.Append(bottomBorder1); paragraphBorders1.Append(rightBorder1); styleParagraphProperties1.Append(paragraphBorders1); StyleRunProperties styleRunProperties3 = new StyleRunProperties(); Color color3 = new Color() { Val = "4472C4", ThemeColor = ThemeColorValues.Accent1 }; Languages languages3 = new Languages() { Val = "en-US" }; styleRunProperties3.Append(color3); styleRunProperties3.Append(languages3); style7.Append(styleName7); style7.Append(basedOn3); style7.Append(primaryStyle4); style7.Append(rsid3); style7.Append(styleParagraphProperties1); style7.Append(styleRunProperties3); Style style8 = new Style() { Type = StyleValues.Paragraph, StyleId = "ParagraphStyle2", CustomStyle = true }; StyleName styleName8 = new StyleName() { Val = "ParagraphStyle2" }; BasedOn basedOn4 = new BasedOn() { Val = "ParagraphStyle1" }; PrimaryStyle primaryStyle5 = new PrimaryStyle(); Rsid rsid4 = new Rsid() { Val = "00856DAD" }; StyleParagraphProperties styleParagraphProperties2 = new StyleParagraphProperties(); ParagraphBorders paragraphBorders2 = new ParagraphBorders(); TopBorder topBorder2 = new TopBorder() { Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; LeftBorder leftBorder2 = new LeftBorder() { Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; BottomBorder bottomBorder2 = new BottomBorder() { Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; RightBorder rightBorder2 = new RightBorder() { Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; paragraphBorders2.Append(topBorder2); paragraphBorders2.Append(leftBorder2); paragraphBorders2.Append(bottomBorder2); paragraphBorders2.Append(rightBorder2); styleParagraphProperties2.Append(paragraphBorders2); StyleRunProperties styleRunProperties4 = new StyleRunProperties(); Bold bold2 = new Bold(); Color color4 = new Color() { Val = "auto" }; styleRunProperties4.Append(bold2); styleRunProperties4.Append(color4); style8.Append(styleName8); style8.Append(basedOn4); style8.Append(primaryStyle5); style8.Append(rsid4); style8.Append(styleParagraphProperties2); style8.Append(styleRunProperties4); styles.Append(docDefaults1); styles.Append(style1); styles.Append(style2); styles.Append(style3); styles.Append(style4); styles.Append(style5); styles.Append(style6); styles.Append(style7); styles.Append(style8); return(styles); }