/// <summary> /// Create bullet numbering part /// </summary> public void CreateBulletNumberingPart(MainDocumentPart mainPart, string bulletChar = "-") { NumberingDefinitionsPart numberingPart = mainPart.AddNewPart <NumberingDefinitionsPart>("NDPBullet"); Numbering element = new Numbering( new AbstractNum( new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = bulletChar } ) { LevelIndex = 0 } ) { AbstractNumberId = 1 }, new NumberingInstance( new AbstractNumId() { Val = 1 } ) { NumberID = 1 }); element.Save(numberingPart); }
public NumberingProperties CreateNewNumberingSequence(WordprocessingDocument document) { // The numbering part API does not work correctly. // Any Numbering instance that becomes associated with the part as its property does not produce numbering in the final document var definitions = document.MainDocumentPart.NumberingDefinitionsPart ?? document.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>(); definitions.Numbering ??= new Numbering(); // The property must be accessed but cannot be used. It is null even on a new instance where this is the root part. // Using Numbering.Load produces the same result. var baseNumbering = new AbstractNum ( new Level { NumberingFormat = new NumberingFormat { Val = NumberFormatValues.Decimal }, LevelIndex = 0 } ) { AbstractNumberId = _numbering .Descendants <AbstractNum>() .Select <AbstractNum, int>(an => an.AbstractNumberId) .DefaultIfEmpty(0) .Max() + 1, MultiLevelType = new MultiLevelType { Val = MultiLevelValues.SingleLevel }, }; var numberingInstance = new NumberingInstance { NumberID = _numbering .Descendants <NumberingInstance>() .Select <NumberingInstance, int>(an => an.NumberID) .DefaultIfEmpty(0) .Max() + 1, AbstractNumId = new AbstractNumId { Val = baseNumbering.AbstractNumberId } }; _numbering.AppendChild(baseNumbering); _numbering.AppendChild(numberingInstance); _numbering.Save(definitions); // Assigning a new Numbering to the property has the same result as using one accessed from the property, even if it was saved first. return(new NumberingProperties { NumberingId = new NumberingId { Val = numberingInstance.NumberID }, NumberingLevelReference = new NumberingLevelReference { Val = 0 } }); }
internal NumberingDefinitionsPart GetNumberingPart() { if (numberingPart == null) { numberingPart = docxFile.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>(); Numbering numberingRoot = new Numbering(); numberingRoot.Save(numberingPart); } return numberingPart; }
public static NumberingDefinitionsPart GetOrCreateNumberingDefinitionsPart(WordprocessingDocument doc) { // Introduce bulleted numbering in case it will be needed at some point var numberingPart = doc.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = doc.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("numbDef001"); Numbering element = new Numbering(); element.Save(numberingPart); } return(numberingPart); }
public int InsertNewNumbering() { int result = this.m_NumberingCounter++; NumberingDefinitionsPart numberingPart = this.m_Document.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = this.m_Document.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("NumberingDefinitionsPart"); } Numbering element = new Numbering( new AbstractNum( new Level() { LevelIndex = 0, NumberingFormat = new NumberingFormat() { Val = NumberFormatValues.Bullet }, LevelText = new LevelText() { Val = "" } } ) { AbstractNumberId = m_NumberingCounter, MultiLevelType = new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel }, }, new NumberingInstance() { NumberID = result, AbstractNumId = new AbstractNumId() { Val = m_NumberingCounter } } ); numberingPart.Numbering = element; element.Save(numberingPart); return(result); }
public void SetNumberingFromDocument(string stylesFile) { XDocument numbering = WordDocUtilities.ExtractNumberingPart(stylesFile); // Get the Styles part for this document. NumberingDefinitionsPart part = WordDocument.MainDocumentPart.NumberingDefinitionsPart; Numbering root = new Numbering(numbering.ToString()); // If the Styles part does not exist, add it. if (part == null) { WordDocUtilities.AddNumberingPartToPackage(WordDocument, root); } else { root.Save(part); } }
/// <summary> /// Adds the abstract numbering definition. The abstract numbering definition can be thought of as the list class, which can have multiple instances. /// In this definition the styles of the different list levels will be defined, but here we add the definitions part only. /// The level definitions will be added to this on demand later. /// </summary> /// <param name="renderer">The renderer.</param> /// <returns>The abstract numbering definition.</returns> private AbstractNum AddAbstractNumberingDefinition(OpenXMLRenderer renderer) { var _wordDocument = renderer._wordDocument; // Introduce bulleted numbering in case it will be needed at some point NumberingDefinitionsPart numberingPart = _wordDocument.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = _wordDocument.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("NumberingDefinitionsPart001"); var element = new Numbering(); element.Save(numberingPart); } // Insert an AbstractNum into the numbering part numbering list. // The order seems to matter or it will not pass the // Open XML SDK Productity Tools validation test. // AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last AbstractNum and BEFORE the first NumberingInstance or we will get a validation error. var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var abstractNum1 = new AbstractNum() { AbstractNumberId = abstractNumberId }; abstractNum1.AppendChild(new MultiLevelType() { Val = MultiLevelValues.HybridMultilevel }); if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum1); } else { AbstractNum lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum1, lastAbstractNum); } return(abstractNum1); }
private void NumberingPart(MainDocumentPart mainDocumentPart) { NumberingDefinitionsPart numberingPart = mainDocumentPart.AddNewPart <NumberingDefinitionsPart>("defaultNumberingDefinition"); Numbering element = new Numbering( new AbstractNum( new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "🎈" } ) { LevelIndex = 0 } ) { AbstractNumberId = 1 }, new NumberingInstance( new AbstractNumId() { Val = 1 } ) { NumberID = 1 }); element.Save(numberingPart); }
public int ProcessItem(HtmlEnumerator en) { if (!firstItem) { return(this.InstanceID); } firstItem = false; // in case a margin has been specifically specified, we need to create a new list template // on the fly with a different AbsNumId, in order to let Word doesn't merge the style with its predecessor. Margin margin = en.StyleAttributes.GetAsMargin("margin"); if (margin.Left.Value > 0 && margin.Left.Type == UnitMetric.Pixel) { Numbering numbering = mainPart.NumberingDefinitionsPart.Numbering; foreach (AbstractNum absNum in numbering.Elements <AbstractNum>()) { if (absNum.AbstractNumberId == absNumId) { Level lvl = absNum.GetFirstChild <Level>(); Int32 currentNumId = ++nextInstanceID; numbering.Append( new AbstractNum( new MultiLevelType() { Val = MultiLevelValues.SingleLevel }, new Level { StartNumberingValue = new StartNumberingValue() { Val = 1 }, NumberingFormat = new NumberingFormat() { Val = lvl.NumberingFormat.Val }, LevelIndex = 0, LevelText = new LevelText() { Val = lvl.LevelText.Val } } ) { AbstractNumberId = currentNumId }); numbering.Save(mainPart.NumberingDefinitionsPart); numbering.Append( new NumberingInstance( new AbstractNumId() { Val = currentNumId } ) { NumberID = currentNumId }); numbering.Save(mainPart.NumberingDefinitionsPart); mainPart.NumberingDefinitionsPart.Numbering.Reload(); break; } } } return(this.InstanceID); }
public IActionResult AddList() { using (var stream = new MemoryStream()) { using (var wordDocument = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true)) { wordDocument.AddMainDocumentPart(); var document = new Document(); var body = new Body(); var spacing = new SpacingBetweenLines() { After = "0" }; var indentation = new Indentation() { Left = "5", Hanging = "360" }; var numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 1 }, //ordered list new NumberingId() { Val = 2 } //ordered list ); var paragraphProperties = new ParagraphProperties(numberingProperties, spacing, indentation); paragraphProperties.ParagraphStyleId = new ParagraphStyleId() { Val = "ListParagraph" }; var paragraph0 = new Paragraph(new Run(new Text("Ordered List."))); var paragraph1 = new Paragraph(); paragraph1.ParagraphProperties = new ParagraphProperties(paragraphProperties.OuterXml); paragraph1.Append(new Run(new Text("Soccer"))); Paragraph paragraph2 = new Paragraph(); paragraph2.ParagraphProperties = new ParagraphProperties(paragraphProperties.OuterXml); paragraph2.Append(new Run(new Text("Basketball"))); Paragraph paragraph3 = new Paragraph(); paragraph3.ParagraphProperties = new ParagraphProperties(paragraphProperties.OuterXml); paragraph3.Append(new Run(new Text("Tennis"))); body.Append(paragraph0); body.Append(paragraph1); body.Append(paragraph2); body.Append(paragraph3); ///////////////////////////// NumberingDefinitionsPart numberingPart = wordDocument.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = wordDocument.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("NumberingDefinitionsPart001"); } //var numberingPart = wordDocument.MainDocumentPart.AddNewPart<NumberingDefinitionsPart>("NumberingDefinitionsPart001"); //unique ID var numbering = new Numbering( new AbstractNum( new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "·" } //♦ ) { LevelIndex = 0 } ) { AbstractNumberId = 1 }, new NumberingInstance( new AbstractNumId() { Val = 1 } ) { NumberID = 1 }); numbering.Save(numberingPart); var spacing2 = new SpacingBetweenLines() { After = "5" }; var indentation2 = new Indentation() { Left = "5", Hanging = "360" }; var numberingProperties2 = new NumberingProperties( new NumberingLevelReference() { Val = 0 }, //unordered list new NumberingId() { Val = 1 } //unordered list ); var paragraphProperties2 = new ParagraphProperties(numberingProperties2, spacing2, indentation2); paragraphProperties2.ParagraphStyleId = new ParagraphStyleId() { Val = "ListParagraph" }; var paragraph20 = new Paragraph(new Run(new Text("Unordered List."))); body.Append(paragraph20); var countryList = new List <string>() { "Mexico", "Czech Republic", "Nicaragua", "Italy" }; foreach (var item in countryList) { var paragraph2n = new Paragraph(); paragraph2n.ParagraphProperties = new ParagraphProperties(paragraphProperties2.OuterXml); paragraph2n.Append(new Run(new Text(item))); body.Append(paragraph2n); } document.Append(body); wordDocument.MainDocumentPart.Document = document; wordDocument.Close(); } return(File(stream.ToArray(), docxMIMEType, "Word Document List Example.docx")); } }
public static void AppendList( WordprocessingDocument wordDoc, IEnumerable <MarkGeneralDataPoint> markGeneralDataPoints) { NumberingDefinitionsPart numberingPart = wordDoc.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = wordDoc.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>( "NumberingDefinitionsPart1"); Numbering element = new Numbering(); element.Save(numberingPart); } var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var abstractLevel = new Level( new NumberingFormat() { Val = NumberFormatValues.Decimal }, new LevelText() { Val = "%1" }, new StartNumberingValue() { Val = 1, }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "GOST type B", HighAnsi = "GOST type B", ComplexScript = "GOST type B" }, Italic = new Italic() { Val = OnOffValue.FromBoolean(true) }, FontSize = new FontSize() { Val = 26.ToString(), } }) { LevelIndex = 0 }; var abstractLevel2 = new Level( new NumberingFormat() { Val = NumberFormatValues.Decimal }, new LevelText() { Val = "%1.%2" }, new StartNumberingValue() { Val = 1, }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "GOST type B", HighAnsi = "GOST type B", ComplexScript = "GOST type B" }, Italic = new Italic() { Val = OnOffValue.FromBoolean(true) }, FontSize = new FontSize() { Val = 26.ToString(), } }) { LevelIndex = 1 }; var abstractLevel3 = new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "–" }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "Calibri", HighAnsi = "Calibri", ComplexScript = "Calibri" }, }) { LevelIndex = 2 }; var abstractLevel4 = new Level( new LevelSuffix() { Val = LevelSuffixValues.Space }) { LevelIndex = 3 }; var abstractNum = new AbstractNum( abstractLevel, abstractLevel2, abstractLevel3, abstractLevel4) { AbstractNumberId = abstractNumberId }; if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum); } else { AbstractNum lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum, lastAbstractNum); } var numberId = numberingPart.Numbering.Elements <NumberingInstance>().Count() + 1; NumberingInstance numberingInstance = new NumberingInstance() { NumberID = numberId }; AbstractNumId abstractNumId = new AbstractNumId() { Val = abstractNumberId }; numberingInstance.Append(abstractNumId); if (numberId == 1) { numberingPart.Numbering.Append(numberingInstance); } else { var lastNumberingInstance = numberingPart.Numbering.Elements <NumberingInstance>().Last(); numberingPart.Numbering.InsertAfter(numberingInstance, lastNumberingInstance); } Body body = wordDoc.MainDocumentPart.Document.Body; var markGeneralDataPointsList = markGeneralDataPoints.ToList(); for (var i = 0; i < markGeneralDataPoints.Count(); i++) { var item = markGeneralDataPointsList[i]; var spacingBetweenLines = new SpacingBetweenLines() { After = "120", Line = "240" }; var indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "720" }; NumberingProperties numberingProperties; var pointText = item.Text; if (item.OrderNum == 1) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = numberId }); } else if (item.Text[0] == '#' && item.Text[1] == ' ') { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 1 }, new NumberingId() { Val = numberId }); pointText = pointText.Substring(2) + "."; } else if (item.Text[0] == '-' && item.Text[1] == ' ') { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 2 }, new NumberingId() { Val = numberId }); if (i == 0) { pointText = pointText.Substring(2) + "."; } else if (markGeneralDataPointsList[i - 1].OrderNum == 1) { pointText = pointText.Substring(2) + "."; } else if (markGeneralDataPointsList[i - 1].Text[0] == '#' && markGeneralDataPointsList[i - 1].Text[1] == ' ') { pointText = pointText.Substring(2) + "."; } else { pointText = pointText.Substring(2) + ";"; } } else { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 3 }, new NumberingId() { Val = numberId }); pointText = pointText + "."; indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "640" }; } var paragraphProperties = new ParagraphProperties( numberingProperties, spacingBetweenLines, indentation); var newPara = new Paragraph(paragraphProperties); if (pointText.Contains('^')) { var split = pointText.Split('^'); if (split.Count() > 1) { for (int k = 0; k < split.Count(); k++) { if (k > 0) { newPara.AppendChild(GetWordTextElement(split[k][0].ToString(), 26, false, true)); } if (k == 0) { newPara.AppendChild(GetWordTextElement(split[k], 26)); } else if (split[k].Length > 1) { newPara.AppendChild(GetWordTextElement(split[k].Substring(1), 26)); } } } else { newPara.AppendChild(GetWordTextElement(pointText, 26)); } } else { newPara.AppendChild(GetWordTextElement(pointText, 26)); } // if (pointText.Contains('^')) // { // var split = pointText.Split("^2"); // if (split.Count() > 1) // { // for (int k = 0; k < split.Count(); k++) // { // if (k > 0) // { // newPara.AppendChild(GetWordTextElement("2", 26, false, true)); // } // newPara.AppendChild(GetWordTextElement(split[k], 26)); // } // // var split2 = s.Split("^3"); // // if (s.Count() > 1) // // { // // } // } // else // { // split = pointText.Split("^3"); // if (split.Count() > 1) // { // for (int k = 0; k < split.Count(); k++) // { // if (k > 0) // { // newPara.AppendChild(GetWordTextElement("3", 26, false, true)); // } // newPara.AppendChild(GetWordTextElement(split[k], 26)); // } // } // else // newPara.AppendChild(GetWordTextElement(pointText, 26)); // } // } // else // newPara.AppendChild(GetWordTextElement(pointText, 26)); body.PrependChild(newPara); } }
public string Save(ITest test, string path) { try { using (WordprocessingDocument doc = WordprocessingDocument.Create( path, DocumentFormat.OpenXml.WordprocessingDocumentType.Document)) { //Doc structure mainPart = doc.AddMainDocumentPart(); mainPart.Document = new Document(); body = mainPart.Document.AppendChild(new Body()); NumberingDefinitionsPart numberingPart = mainPart.AddNewPart <NumberingDefinitionsPart>("numberpart1"); Numbering element = new Numbering( new AbstractNum( new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "·" }) { LevelIndex = 0 }) { AbstractNumberId = 1 }, new NumberingInstance( new AbstractNumId() { Val = 1 }) { NumberID = 1 }); element.Save(numberingPart); foreach (var question in test.Questions) { Paragraph para = body.AppendChild(new Paragraph()); var info = new StringBuilder(question.QuestionAnswer.GetQuestionTaskInfo()); info.Append(" ("); info.Append(question.QuestionAnswer.QuestionScore); info.Append(" баллов)"); Run run = para.AppendChild(new Run()); run.PrependChild(GetStyle(true)); run.AppendChild(new Text(info.ToString())); run = para.AppendChild(new Run()); run.PrependChild(GetStyle(false)); run.AppendChild(new Break()); run.AppendChild(new Text(question.QuestionInfo.GetShortDescription())); question.QuestionAnswer.ToWord(this as IWordAnswerPrinter); } } } catch (Exception e) { return(e.Message); } return(null); }
private void AppendList( WordprocessingDocument wordDoc, List <ListText> arr) { NumberingDefinitionsPart numberingPart = wordDoc.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = wordDoc.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>( "NumberingDefinitionsPart1"); Numbering element = new Numbering(); element.Save(numberingPart); } var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var abstractLevel = new Level( new NumberingFormat() { Val = NumberFormatValues.UpperRoman }, new LevelText() { Val = "%1." }, new StartNumberingValue() { Val = 1, }, new RunProperties() { Italic = new Italic() { Val = OnOffValue.FromBoolean(true) }, FontSize = new FontSize() { Val = 26.ToString(), }, Bold = new Bold() { }, }) { LevelIndex = 0 }; var abstractLevel1 = new Level( new NumberingFormat() { Val = NumberFormatValues.Decimal }, new LevelText() { Val = "%2." }, new StartNumberingValue() { Val = 1, }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "GOST type B", HighAnsi = "GOST type B", ComplexScript = "GOST type B" }, Italic = new Italic() { Val = OnOffValue.FromBoolean(true) }, FontSize = new FontSize() { Val = 26.ToString(), } }) { LevelIndex = 1 }; var abstractLevel2 = new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "–" }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "Calibri", HighAnsi = "Calibri", ComplexScript = "Calibri" }, }) { LevelIndex = 2 }; var abstractLevel3 = new Level( new LevelSuffix() { Val = LevelSuffixValues.Space }) { LevelIndex = 3 }; var abstractLevel4 = new Level( new LevelSuffix() { Val = LevelSuffixValues.Space }) { LevelIndex = 4 }; var abstractLevel5 = new Level( new LevelSuffix() { Val = LevelSuffixValues.Space }) { LevelIndex = 5 }; var abstractLevel6 = new Level( new LevelSuffix() { Val = LevelSuffixValues.Space }) { LevelIndex = 6 }; var abstractNum = new AbstractNum( abstractLevel, abstractLevel1, abstractLevel2, abstractLevel3, abstractLevel4, abstractLevel5, abstractLevel6) { AbstractNumberId = abstractNumberId }; if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum); } else { AbstractNum lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum, lastAbstractNum); } var numberId = numberingPart.Numbering.Elements <NumberingInstance>().Count() + 1; NumberingInstance numberingInstance = new NumberingInstance() { NumberID = numberId }; AbstractNumId abstractNumId = new AbstractNumId() { Val = abstractNumberId }; numberingInstance.Append(abstractNumId); if (numberId == 1) { numberingPart.Numbering.Append(numberingInstance); } else { var lastNumberingInstance = numberingPart.Numbering.Elements <NumberingInstance>().Last(); numberingPart.Numbering.InsertAfter(numberingInstance, lastNumberingInstance); } Body body = wordDoc.MainDocumentPart.Document.Body; for (var i = 0; i < arr.Count(); i++) { var spacingBetweenLines = new SpacingBetweenLines() { After = "120", Line = "240" }; var indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "1160" }; NumberingProperties numberingProperties; if (arr[i].LevelNum == 0) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = numberId }); indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "720" }; } else if (arr[i].LevelNum == 1) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 1 }, new NumberingId() { Val = numberId }); } else if (arr[i].LevelNum == 2) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 2 }, new NumberingId() { Val = numberId }); } else if (arr[i].LevelNum == 3) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 3 }, new NumberingId() { Val = numberId }); indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "640" }; } else if (arr[i].LevelNum == 4) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 4 }, new NumberingId() { Val = numberId }); indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "1080" }; } else if (arr[i].LevelNum == 5) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 5 }, new NumberingId() { Val = numberId }); indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "1800" }; } else { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 6 }, new NumberingId() { Val = numberId }); indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "2400" }; } var paragraphProperties = new ParagraphProperties( numberingProperties, spacingBetweenLines, indentation); var newPara = new Paragraph(paragraphProperties); if (arr[i].WithSuperscript) { var split = arr[i].Text.Split('^'); if (split.Count() > 1) { for (int k = 0; k < split.Count(); k++) { if (k > 0) { newPara.AppendChild( Word.GetTextElement(split[k][0].ToString(), 26, false, true)); } if (k == 0) { newPara.AppendChild(Word.GetTextElement(split[k], 26)); } else if (split[k].Length > 1) { newPara.AppendChild( Word.GetTextElement(split[k].Substring(1), 26)); } } } else { newPara.AppendChild( Word.GetTextElement(arr[i].Text, 26, false, false, arr[i].IsBold)); } } else { newPara.AppendChild( Word.GetTextElement(arr[i].Text, 26, false, false, arr[i].IsBold)); } body.AppendChild(newPara); } }
public int CreateBulletList() { NumberingDefinitionsPart numberingPart = wdMainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = wdMainDocumentPart.AddNewPart <NumberingDefinitionsPart>(); Numbering element = new Numbering(); element.Save(numberingPart); } // Insert an AbstractNum into the numbering part numbering list. The order seems to matter or it will not pass the // Open XML SDK Productity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last AbstractNum and BEFORE the first NumberingInstance or we will get a validation error. var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var lvls = new List <Level>(); for (int i = 0; i < 6; i++) { var abstractLevel = new Level(new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "" }, new ParagraphProperties() { Indentation = new Indentation() { Left = (720 * (i + 1)).ToString(), Hanging = "360" } }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "Symbol", HighAnsi = "Symbol" } }) { LevelIndex = i }; lvls.Add(abstractLevel); } var abstractNum1 = new AbstractNum(lvls) { AbstractNumberId = abstractNumberId }; if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum1); } else { AbstractNum lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum1, lastAbstractNum); } // Insert an NumberingInstance into the numbering part numbering list. The order seems to matter or it will not pass the // Open XML SDK Productity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last NumberingInstance and AFTER all the AbstractNum entries or we will get a validation error. var numberId = numberingPart.Numbering.Elements <NumberingInstance>().Count() + 1; NumberingInstance numberingInstance1 = new NumberingInstance() { NumberID = numberId }; AbstractNumId abstractNumId1 = new AbstractNumId() { Val = abstractNumberId }; numberingInstance1.Append(abstractNumId1); if (numberId == 1) { numberingPart.Numbering.Append(numberingInstance1); } else { var lastNumberingInstance = numberingPart.Numbering.Elements <NumberingInstance>().Last(); numberingPart.Numbering.InsertAfter(numberingInstance1, lastNumberingInstance); } return(numberId); }
public static void ProcessTranslate(WordprocessingDocument wordprocessingDocument, MainDocumentPart m) { int begintag = 0; int endtag = 0; int listcount = 1; while (begintag < txtPatentlist.Count()) { endtag = GetInterval(begintag); if (txtPatentlist[begintag][0] == "img") { InsertAPicture(wordprocessingDocument, m, txtPatentlist[begintag][1]); } //else if (txtPatentlist[begintag][0] == "br") //{ // WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true); // Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // Paragraph para = new Paragraph(); // Run run = para.AppendChild(new Run()); // run.Append(new Break()); // body.AppendChild(para); // wordprocessingDocument.MainDocumentPart.Document.Save(); // wordprocessingDocument.Close(); //} else if (txtPatentlist[begintag].Contains("table")) { int trindex = txtPatentlist[begintag].IndexOf("tr") + 1; int colcount = Int32.Parse(txtPatentlist[begintag][trindex]); int rowcount = (endtag - begintag) / colcount; CreateTable(wordprocessingDocument, colcount, rowcount, begintag); } else if (txtPatentlist[begintag].Contains("ul")) { //WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true); //MainDocumentPart m = wordprocessingDocument.MainDocumentPart; //Body body = m.Document.Body; NumberingDefinitionsPart numberingPart = m.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = m.AddNewPart <NumberingDefinitionsPart>(); } Numbering element = new Numbering( new AbstractNum( new MultiLevelType() { Val = MultiLevelValues.Multilevel }, new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "●" } ) { LevelIndex = 0 }, new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "o" } ) { LevelIndex = 1 }, new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "■" } ) { LevelIndex = 2 } ) { AbstractNumberId = 1 }, new NumberingInstance( new AbstractNumId() { Val = 1 } ) { NumberID = 1 } ); element.Save(numberingPart); bool isenter = false; for (; begintag < endtag; begintag++) { if (txtPatentlist[begintag][0] == "br") { begintag++; isenter = true; } int level = -1; string content = txtPatentlist[begintag][0]; foreach (string s in txtPatentlist[begintag]) { if (s == "ul") { level += 1; } } AppendListItem(wordprocessingDocument, content, level, listcount, 0, isenter); wordprocessingDocument.MainDocumentPart.Document.Save(); isenter = false; } listcount++; //wordprocessingDocument.Close(); } else if (txtPatentlist[begintag].Contains("pre")) { //WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true); //Body body = wordprocessingDocument.MainDocumentPart.Document.Body; Paragraph para = new Paragraph(); ParagraphProperties paraProperties = new ParagraphProperties(); ParagraphBorders paraBorders = new ParagraphBorders(); TopBorder top = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)9U, Space = (UInt32Value)5U }; BottomBorder bottom = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)9U, Space = (UInt32Value)5U }; LeftBorder left = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)9U, Space = (UInt32Value)5U }; RightBorder right = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)9U, Space = (UInt32Value)5U }; paraBorders.Append(bottom); paraBorders.Append(top); paraBorders.Append(left); paraBorders.Append(right); paraProperties.Append(paraBorders); para.Append(paraProperties); Run run = para.AppendChild(new Run()); string t = txtPatentlist[begintag][0]; string[] s = t.Split('\n'); foreach (string str in s) { if (str.Contains(" ")) { run.Append(new TabChar()); } run.AppendChild(new Text(str)); run.Append(new Break()); } body.AppendChild(para); wordprocessingDocument.MainDocumentPart.Document.Save(); //wordprocessingDocument.Close(); } else if (txtPatentlist[begintag].Contains("h1")) { SetTitle(begintag, endtag, 1.ToString(), wordprocessingDocument); //string path = @"demo.docx"; //CreateWordDocumentUsingMSWordStyles(begintag, endtag, path, "E:\\Microsoft Office\\Office16\\2052\\QuickStyles\\Default.dotx"); } else if (txtPatentlist[begintag].Contains("h2")) { SetTitle2(begintag, endtag, 2.ToString(), wordprocessingDocument); } else if (txtPatentlist[begintag].Contains("h3")) { SetTitle3(begintag, endtag, 3.ToString(), wordprocessingDocument); } else { //WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true); //Body body = wordprocessingDocument.MainDocumentPart.Document.Body; Paragraph para = body.AppendChild(new Paragraph()); for (; begintag < endtag; begintag++) { Run run = para.AppendChild(new Run()); if (txtPatentlist[begintag][0] == "br") { run.Append(new Break()); begintag++; } if (txtPatentlist[begintag][0] != "endtag") { Text t = new Text(ToHexString(txtPatentlist[begintag][0])); t.Space = t.Space = new EnumValue <SpaceProcessingModeValues>(SpaceProcessingModeValues.Preserve); run.AppendChild(t); } foreach (string tag in txtPatentlist[begintag]) { switch (tag) { case "b": SetBoldFont(run, wordprocessingDocument); break; case "u": SetItalic(run, wordprocessingDocument); break; case "strong": SetBoldFont(run, wordprocessingDocument); break; default: break; } } } //wordprocessingDocument.Close(); } begintag = endtag + 1; } m.Document.Body = body; wordprocessingDocument.MainDocumentPart.Document.Save(); wordprocessingDocument.Close(); }
private void AppendList( WordprocessingDocument wordDoc, IEnumerable <MarkGeneralDataPoint> markGeneralDataPoints, MarkOperatingConditions markOperatingConditions) { NumberingDefinitionsPart numberingPart = wordDoc.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = wordDoc.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>( "NumberingDefinitionsPart1"); Numbering element = new Numbering(); element.Save(numberingPart); } var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var abstractLevel = new Level( new NumberingFormat() { Val = NumberFormatValues.Decimal }, new LevelText() { Val = "%1" }, new StartNumberingValue() { Val = 1, }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "GOST type B", HighAnsi = "GOST type B", ComplexScript = "GOST type B" }, Italic = new Italic() { Val = OnOffValue.FromBoolean(true) }, FontSize = new FontSize() { Val = 26.ToString(), } }) { LevelIndex = 0 }; var abstractLevel2 = new Level( new NumberingFormat() { Val = NumberFormatValues.Decimal }, new LevelText() { Val = "%1.%2" }, new StartNumberingValue() { Val = 1, }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "GOST type B", HighAnsi = "GOST type B", ComplexScript = "GOST type B" }, Italic = new Italic() { Val = OnOffValue.FromBoolean(true) }, FontSize = new FontSize() { Val = 26.ToString(), } }) { LevelIndex = 1 }; var abstractLevel3 = new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "–" }, new RunProperties() { RunFonts = new RunFonts() { Ascii = "Calibri", HighAnsi = "Calibri", ComplexScript = "Calibri" }, }) { LevelIndex = 2 }; var abstractLevel4 = new Level( new LevelSuffix() { Val = LevelSuffixValues.Space }) { LevelIndex = 3 }; var abstractNum = new AbstractNum( abstractLevel, abstractLevel2, abstractLevel3, abstractLevel4) { AbstractNumberId = abstractNumberId }; if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum); } else { AbstractNum lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum, lastAbstractNum); } var numberId = numberingPart.Numbering.Elements <NumberingInstance>().Count() + 1; NumberingInstance numberingInstance = new NumberingInstance() { NumberID = numberId }; AbstractNumId abstractNumId = new AbstractNumId() { Val = abstractNumberId }; numberingInstance.Append(abstractNumId); if (numberId == 1) { numberingPart.Numbering.Append(numberingInstance); } else { var lastNumberingInstance = numberingPart.Numbering.Elements <NumberingInstance>().Last(); numberingPart.Numbering.InsertAfter(numberingInstance, lastNumberingInstance); } Body body = wordDoc.MainDocumentPart.Document.Body; var markGeneralDataPointsList = markGeneralDataPoints.ToList(); for (var i = 0; i < markGeneralDataPoints.Count(); i++) { var item = markGeneralDataPointsList[i]; var spacingBetweenLines = new SpacingBetweenLines() { After = "120", Line = "240" }; var indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "720" }; NumberingProperties numberingProperties; var pointText = item.Text; if (item.OrderNum == 1) { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = numberId }); } else if (item.Text[0] == '#' && item.Text[1] == ' ') { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 1 }, new NumberingId() { Val = numberId }); pointText = pointText.Substring(2) + "."; } else if (item.Text[0] == '-' && item.Text[1] == ' ') { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 2 }, new NumberingId() { Val = numberId }); if (i == 0) { pointText = pointText.Substring(2) + "."; } else if (markGeneralDataPointsList[i - 1].OrderNum == 1) { pointText = pointText.Substring(2) + "."; } else if (markGeneralDataPointsList[i - 1].Text[0] == '#' && markGeneralDataPointsList[i - 1].Text[1] == ' ') { pointText = pointText.Substring(2) + "."; } else { pointText = pointText.Substring(2) + ";"; } } else { numberingProperties = new NumberingProperties( new NumberingLevelReference() { Val = 3 }, new NumberingId() { Val = numberId }); pointText = pointText + "."; indentation = new Indentation() { Left = "360", Right = "360", FirstLine = "640" }; } var paragraphProperties = new ParagraphProperties( numberingProperties, spacingBetweenLines, indentation); var newPara = new Paragraph(paragraphProperties); if (item.Section.Id == 7) { if (pointText.Contains("коэффициент надежности по ответственности")) { pointText = pointText.Replace("{}", markOperatingConditions.SafetyCoeff.ToString()); } else if (pointText.Contains("степень агрессивного воздействия среды")) { pointText = pointText.Replace("{}", markOperatingConditions.EnvAggressiveness.Name); } else if (pointText.Contains("расчетная температура эксплуатации")) { pointText = pointText.Replace("{}", $"{(markOperatingConditions.Temperature < 0 ? ("минус " + -markOperatingConditions.Temperature) : markOperatingConditions.Temperature)}"); } } if (pointText.Contains('^')) { var split = pointText.Split('^'); if (split.Count() > 1) { for (int k = 0; k < split.Count(); k++) { if (k > 0) { newPara.AppendChild(Word.GetTextElement(split[k][0].ToString(), 26, false, true)); } if (k == 0) { newPara.AppendChild(Word.GetTextElement(split[k], 26)); } else if (split[k].Length > 1) { newPara.AppendChild(Word.GetTextElement(split[k].Substring(1), 26)); } } } else { newPara.AppendChild(Word.GetTextElement(pointText, 26)); } } else { newPara.AppendChild(Word.GetTextElement(pointText, 26)); } body.PrependChild(newPara); } }
private static void GenerateNumbersPartContent(NumberingDefinitionsPart numbersPart) { var element = new Numbering( new AbstractNum( new Level( new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "·" }) { LevelIndex = 0 }) { AbstractNumberId = 1 }); element.Save(numbersPart); }
public static void AddBulletList(WordprocessingDocument document, IEnumerable <Run> runList) { var numberingPart = document.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = document.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>( "NumberingDefinitionsPart001"); var element = new Numbering(); element.Save(numberingPart); } // Insert an AbstractNum into the numbering part numbering list. The order seems to matter or it will not pass the // Open XML SDK Productivity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last AbstractNum and BEFORE the first NumberingInstance or we will get a validation error. var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var abstractLevel = new Level(new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "·" }) { LevelIndex = 0 }; var abstractNum1 = new AbstractNum(abstractLevel) { AbstractNumberId = abstractNumberId }; if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum1); } else { var lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum1, lastAbstractNum); } // Insert an NumberingInstance into the numbering part numbering list. The order seems to matter or it will not pass the // Open XML SDK Productivity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last NumberingInstance and AFTER all the AbstractNum entries or we will get a validation error. var numberId = numberingPart.Numbering.Elements <NumberingInstance>().Count() + 1; var numberingInstance1 = new NumberingInstance() { NumberID = numberId }; var abstractNumId1 = new AbstractNumId() { Val = abstractNumberId }; numberingInstance1.Append(abstractNumId1); if (numberId == 1) { numberingPart.Numbering.Append(numberingInstance1); } else { var lastNumberingInstance = numberingPart.Numbering.Elements <NumberingInstance>().Last(); numberingPart.Numbering.InsertAfter(numberingInstance1, lastNumberingInstance); } var body = document.MainDocumentPart.Document.Body; foreach (var runItem in runList) { // Create items for paragraph properties var numberingProperties = new NumberingProperties(new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = numberId }); var spacingBetweenLines1 = new SpacingBetweenLines() { After = "0" }; // Get rid of space between bullets var indentation = new Indentation() { Left = "720", Hanging = "360" }; // correct indentation var paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); var runFonts1 = new RunFonts() { Ascii = "Symbol", HighAnsi = "Symbol" }; paragraphMarkRunProperties1.Append(runFonts1); // create paragraph properties var paragraphProperties = new ParagraphProperties(numberingProperties, spacingBetweenLines1, indentation, paragraphMarkRunProperties1); // Create paragraph var newPara = new Paragraph(paragraphProperties); // Add run to the paragraph newPara.AppendChild(runItem); // Add one bullet item to the body body.AppendChild(newPara); } }
public Paragraph CreateParagraphForBullets(string conversationTxt, WordprocessingDocument _wordprocessingDocument) { // Introduce bulleted numbering in case it will be needed at some point NumberingDefinitionsPart numberingPart = _wordprocessingDocument.MainDocumentPart.NumberingDefinitionsPart; if (numberingPart == null) { numberingPart = _wordprocessingDocument.MainDocumentPart.AddNewPart <NumberingDefinitionsPart>("NumberingDefinitionsPart001"); Numbering element = new Numbering(); element.Save(numberingPart); } // Insert an AbstractNum into the numbering part numbering list. The order seems to matter or it will not pass the // Open XML SDK Productity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last AbstractNum and BEFORE the first NumberingInstance or we will get a validation error. var abstractNumberId = numberingPart.Numbering.Elements <AbstractNum>().Count() + 1; var abstractLevel = new Level(new NumberingFormat() { Val = NumberFormatValues.Bullet }, new LevelText() { Val = "·" }) { LevelIndex = 0 }; var abstractNum1 = new AbstractNum(abstractLevel) { AbstractNumberId = abstractNumberId }; if (abstractNumberId == 1) { numberingPart.Numbering.Append(abstractNum1); } else { AbstractNum lastAbstractNum = numberingPart.Numbering.Elements <AbstractNum>().Last(); numberingPart.Numbering.InsertAfter(abstractNum1, lastAbstractNum); } // Insert an NumberingInstance into the numbering part numbering list. The order seems to matter or it will not pass the // Open XML SDK Productity Tools validation test. AbstractNum comes first and then NumberingInstance and we want to // insert this AFTER the last NumberingInstance and AFTER all the AbstractNum entries or we will get a validation error. var numberId = numberingPart.Numbering.Elements <NumberingInstance>().Count() + 1; NumberingInstance numberingInstance1 = new NumberingInstance() { NumberID = numberId }; AbstractNumId abstractNumId1 = new AbstractNumId() { Val = abstractNumberId }; numberingInstance1.Append(abstractNumId1); if (numberId == 1) { numberingPart.Numbering.Append(numberingInstance1); } else { var lastNumberingInstance = numberingPart.Numbering.Elements <NumberingInstance>().Last(); numberingPart.Numbering.InsertAfter(numberingInstance1, lastNumberingInstance); } Run runlist = new Run(new Text(conversationTxt)); // Create items for paragraph properties var numberingProperties = new NumberingProperties(new NumberingLevelReference() { Val = 0 }, new NumberingId() { Val = numberId }); var spacingBetweenLines1 = new SpacingBetweenLines() { After = "0" }; // Get rid of space between bullets var indentation = new Indentation() { Left = "720", Hanging = "360" }; // correct indentation ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); RunFonts runFonts1 = new RunFonts() { Ascii = "Symbol", HighAnsi = "Symbol" }; paragraphMarkRunProperties1.Append(runFonts1); // create paragraph properties var paragraphProperties = new ParagraphProperties(numberingProperties, spacingBetweenLines1, indentation, paragraphMarkRunProperties1); // Create paragraph var newPara = new Paragraph(paragraphProperties); // Add run to the paragraph newPara.Append(runlist); return(newPara); }