/// <summary> /// Appends a function at the end of the document. /// </summary> /// <param name="element">The function to be written as an Element.</param> /// <param name="paragraph">The paragraph where this function will be written.</param> private void AppendFunction(BasicContentBlock element, Paragraph paragraph) { FunctionContentBlock function = (FunctionContentBlock)element; // Initializing the paragraph and the properties for input_range and title. Paragraph para = new Paragraph(); RunProperties title_rPr = new RunProperties(new Bold()); RunProperties FunctionID_rPr = new RunProperties(new Underline()); RunProperties inputRange_rPr = new RunProperties(new Italic()); Run titleRun = new Run(title_rPr); Run functionIdRun = new Run(FunctionID_rPr); Run inputRangeRun = new Run(inputRange_rPr); titleRun.Append(new Word.Text(function.FunctionName)); titleRun.Append(new Break()); functionIdRun.Append(new Word.Text("Function ID:" + function.FunctionID)); functionIdRun.Append(new Break()); inputRangeRun.Append(new Word.Text("Input Range:" + function.InputRange)); inputRangeRun.Append(new Break()); para.Append(titleRun); para.Append(functionIdRun); para.Append(inputRangeRun); Document.MainDocumentPart.Document.Body.Append(para); // Prints the inner elements of this function to the word file. foreach (var item in function.Content) { AppendElement(item, new Paragraph()); } Document.Save(); }
/// <summary> /// Appends a path at the end of the document. /// </summary> /// <param name="element">The path to be written as an Element.</param> /// <param name="paragraph">The paragraph where this path will be written.</param> private void AppendPath(BasicContentBlock element, Paragraph paragraph) { PathContentBlock path = (PathContentBlock)element; // The Following code Runs The Path in Bold // Case 1 //Paragraph para = new Paragraph(); //RunProperties Path_rPr = new RunProperties(new Bold()); //Run PathRun = new Run(Path_rPr); //PathRun.Append(new Word.Text("Path:" + path.ToString())); //para.Append(PathRun); //The following Code Runs the path in normal //Case 2 Run run = new Run(new Word.Text(path.ToString())); Paragraph para = new Paragraph(run); Document.MainDocumentPart.Document.Body.Append(para); Document.Save(); }
/// <summary> /// This generic method is responsible for managing the printing of elements to the word file. /// </summary> /// <param name="element">The element to be printed.</param> /// <param name="paragraph">The paragraph in which the element will be placed. Not yet implemented.</param> public void AppendElement(BasicContentBlock element, Paragraph paragraph) { switch (element.Type) { case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.function: AppendFunction(element, paragraph); break; case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.image: AppendImage(element, paragraph); break; case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.matrix: AppendMatrix(element, paragraph); break; case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.path: AppendPath(element, paragraph); break; case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.Remark: AppendRemark(element, paragraph); break; case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.text: AppendText(element, paragraph); break; case ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.StaticPage: AppendImage(element, paragraph); break; default: Document.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(new Word.Text(element.ToString())))); break; } }
/// <summary> /// Appends a remark element at the end of the document. /// </summary> /// <param name="element">The remark to be written as an Element.</param> /// <param name="paragraph">The paragraph where this remark will be written.</param> private void AppendRemark(BasicContentBlock element, Paragraph paragraph) { // The following code was copy-pasted from the OpenXml SDK Productivity Tool, with some small changes. // It creates a 1 row, 2 columns table, appends the remark image in the first cell and the text in the second. // Then appends the whole table at the end of the document. // Because the image and the table have standatd dimensions, we can supply the various properties with absolute values where possible. RemarkContentBlock remark = (RemarkContentBlock)element; Table table1 = new Table(); TableProperties tableProperties1 = new TableProperties(); // new TableBorders( // new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, // new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, // new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, // new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, // new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 }, // new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Dashed), Size = 24 } //)); // // Create a row. // TableRow tr = new TableRow(); // // Create a cell. // TableCell tc1 = new TableCell(); // // Specify the width property of the table cell. // tc1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" })); // // Specify the table cell content. // tc1.Append(new Paragraph(new Run(new Text(remark.Content)))); // // Append the table cell to the table row. // tr.Append(tc1); TableStyle tableStyle1 = new TableStyle() { Val = "TableGrid" }; TableWidth tableWidth1 = new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }; TableBorders tableBorders1 = new TableBorders(); InsideVerticalBorder insideVerticalBorder1 = new InsideVerticalBorder() { Val = BorderValues.None, Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U }; tableBorders1.Append(insideVerticalBorder1); TableLook tableLook1 = new TableLook() { Val = "04A0", FirstRow = true, LastRow = true, FirstColumn = true, LastColumn = true, NoHorizontalBand = false, NoVerticalBand = true }; Justification justification1 = new Justification() { Val = JustificationValues.Center }; tableProperties1.Append(tableStyle1); tableProperties1.Append(tableWidth1); tableProperties1.Append(tableBorders1); tableProperties1.Append(tableLook1); tableProperties1.Append(justification1); TableGrid tableGrid1 = new TableGrid(); GridColumn gridColumn1 = new GridColumn() { Width = "846" }; GridColumn gridColumn2 = new GridColumn() { Width = "8170" }; tableGrid1.Append(gridColumn1); tableGrid1.Append(gridColumn2); TableRow tableRow1 = new TableRow() { RsidTableRowAddition = "00156C35", RsidTableRowProperties = "0092224D" }; TableCell tableCell1 = new TableCell(); TableCellProperties tableCellProperties1 = new TableCellProperties(); TableCellWidth tableCellWidth1 = new TableCellWidth() { Width = "846", Type = TableWidthUnitValues.Dxa }; TableCellBorders tableCellBorders1 = new TableCellBorders(); BottomBorder bottomBorder1 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U }; LeftBorder leftBorder1 = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U }; TopBorder topBorder1 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U }; tableCellBorders1.Append(bottomBorder1); tableCellBorders1.Append(leftBorder1); tableCellBorders1.Append(topBorder1); tableCellProperties1.Append(tableCellWidth1); tableCellProperties1.Append(tableCellBorders1); Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00156C35", RsidParagraphProperties = "00156C35", RsidRunAdditionDefault = "00156C35" }; Run run1 = new Run(); RunProperties runProperties1 = new RunProperties(); NoProof noProof1 = new NoProof(); runProperties1.Append(noProof1); ImagePart imagePart = Document.MainDocumentPart.AddImagePart(ImagePartType.Jpeg); using (FileStream fs = new FileStream(Path.Combine(imagesPath, remark.IconUrl), FileMode.Open)) { imagePart.FeedData(fs); } run1.Append(runProperties1); Drawing drawing1 = AddImageToBody(Document.MainDocumentPart.GetIdOfPart(imagePart), 361604L, 361604L); run1.Append(drawing1); paragraph1.Append(run1); tableCell1.Append(tableCellProperties1); tableCell1.Append(paragraph1); TableCell tableCell2 = new TableCell(); TableCellProperties tableCellProperties2 = new TableCellProperties(); TableCellWidth tableCellWidth2 = new TableCellWidth() { Width = "8170", Type = TableWidthUnitValues.Dxa }; TableCellBorders tableCellBorders2 = new TableCellBorders(); BottomBorder bottomBorder2 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U }; RightBorder rightBorder2 = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U }; TopBorder topBorder2 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U }; tableCellProperties2.Append(tableCellWidth2); tableCellBorders2.Append(bottomBorder2); tableCellBorders2.Append(rightBorder2); tableCellBorders2.Append(topBorder2); tableCellProperties2.Append(tableCellBorders2); Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00156C35", RsidParagraphProperties = "00156C35", RsidRunAdditionDefault = "00156C35" }; Run ContentRun = new Run(); ContentRun.Append(new Word.Text(remark.Content)); paragraph2.Append(ContentRun); //Run run2 = new Run(); //Word.Text text1 = new Word.Text(); //text1.Text = remark.Content; //run2.Append(text1); //paragraph2.Append(run2); tableCell2.Append(tableCellProperties2); tableCell2.Append(paragraph2); tableRow1.Append(tableCell1); tableRow1.Append(tableCell2); table1.Append(tableProperties1); table1.Append(tableGrid1); table1.Append(tableRow1); Document.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(table1))); Document.Save(); }
/// <summary> /// Appends an image or static page at the end of the document. /// </summary> /// <param name="element">The text to be written as an Element.</param> /// <param name="paragraph">The paragraph where this path will be written.</param> private void AppendImage(BasicContentBlock element, Paragraph paragraph) { MainDocumentPart mainPart = this.Document.MainDocumentPart; ImageContentBlock eImg; string imagePath = null; // The following 'if' statement is mandatory to distinguish between an image and a static page. // An image will be printed in its original dimensions if possible or resized to fit the width of the page (keeping its original ratio between X and Y dimensions). // A static page is treated like an image that is printed to a whole page. if (element.Type == ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.image) { eImg = (ImageContentBlock)element; imagePath = System.IO.Path.Combine(imagesPath, eImg.Source); } else if (element.Type == ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.StaticPage) { imagePath = System.IO.Path.Combine(imagesPath); } // Get the EMUs of this image's dimensions. Dictionary <string, long> emusDict = GetEmus(imagePath); ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg); using (FileStream fs = new FileStream(imagePath, FileMode.Open)) { imagePart.FeedData(fs); } // The following lines generate a paragraph and its properties and assign the alignment attibute to "center" . Justification justification = new Justification() { Val = JustificationValues.Center }; ParagraphProperties paragraphProperties = new ParagraphProperties() { Justification = justification }; Paragraph imageParagraph = new Paragraph() { ParagraphProperties = paragraphProperties }; Run run = new Run(); // This statement is here to distinguish between an image or a static page to be placed at the end of the file. if (element.Type == ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.StaticPage) { run.Append(AddImageToBody(mainPart.GetIdOfPart(imagePart), emusDict["maxWidthEmus"], emusDict["maxHeightEmus"])); } else { run.Append(AddImageToBody(mainPart.GetIdOfPart(imagePart), emusDict["widthEmus"], emusDict["heightEmus"])); } imageParagraph.Append(run); Document.MainDocumentPart.Document.Body.Append(imageParagraph); Document.Save(); ImageContentBlock image = (ImageContentBlock)element; Paragraph para = new Paragraph(); Run contentRun = new Run(); contentRun.Append(new Word.Text(image.Content)); contentRun.Append(new Break()); para.Append(contentRun); Document.MainDocumentPart.Document.Body.Append(para); }
/// <summary> /// Appends a table at the end of the document. /// </summary> /// <param name="element">The table to be written as an Element.</param> /// <param name="paragraph">The paragraph where this table will be written.</param> private void AppendMatrix(BasicContentBlock element, Paragraph paragraph) { MatrixContentBlock matrix = (MatrixContentBlock)element; Table table = new Table(); TableRow tr; //Initialize the table's borders and their properties and align it to the center of the page. TableProperties tableProperties = new TableProperties(new TableBorders( new TopBorder() { Val = new EnumValue <BorderValues>(BorderValues.Thick), Size = 7 }, new BottomBorder() { Val = new EnumValue <BorderValues>(BorderValues.Thick), Size = 7 }, new LeftBorder() { Val = new EnumValue <BorderValues>(BorderValues.Thick), Size = 7 }, new RightBorder() { Val = new EnumValue <BorderValues>(BorderValues.Thick), Size = 7 }, new InsideVerticalBorder() { Val = new EnumValue <BorderValues>(BorderValues.Thick), Size = 7 }, new InsideHorizontalBorder() { Val = new EnumValue <BorderValues>(BorderValues.Thick), Size = 7 } ), new Justification() { Val = JustificationValues.Center }); table.Append(tableProperties); // Append the table headers and style them. if (matrix.Headers.Length > 0) { // Initialize, format and populate the header row. tr = new TableRow(); TableCell tc = new TableCell(); foreach (var header in matrix.Headers) { tc = new TableCell(); tc.Append(new Paragraph(new Run(new Word.Text(header)))); TableCellProperties tcp = new TableCellProperties(); // Shading is the property needed to fill the background of a cell. Shading shading = new Shading() { Color = "auto", Fill = "0033A1", Val = ShadingPatternValues.Clear }; tcp.Append(shading); tc.Append(tcp); tr.Append(tc); } table.Append(tr); } // END of headers // Append the table body. for (int i = 0; i < matrix.Body.Count; i++) { tr = new TableRow(); for (int j = 0; j < matrix.Body[i].ContentBlocks.Count; j++) { TableCell tc = new TableCell(); // Get the element to be printed in this cell. BasicContentBlock innerElement = matrix.Body[i].ContentBlocks[j]; // This if statement is here to distinguish between an image or a text to be placed in a cell. // If it's neither an image nor a text defaults to the ToString() method of the specific element. if (innerElement.Type == ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.image) { ImageContentBlock eImage = (ImageContentBlock)innerElement; ImagePart imagePart = Document.MainDocumentPart.AddImagePart(ImagePartType.Jpeg); string imagePath = System.IO.Path.Combine(imagesPath, eImage.Source); Dictionary <string, long> dictEmus = GetEmus(imagePath); using (FileStream fs = new FileStream(imagePath, FileMode.Open)) { imagePart.FeedData(fs); } // The following lines centers the image inside the table cell. Then it's assigned to a run and a paragraph. Justification justification1 = new Justification() { Val = JustificationValues.Center }; ParagraphProperties paragraphProperties = new ParagraphProperties() { Justification = justification1 }; Paragraph paragraph1 = new Paragraph() { ParagraphProperties = paragraphProperties }; Run run = new Run(); run.Append(AddImageToBody(Document.MainDocumentPart.GetIdOfPart(imagePart), dictEmus["widthEmus"], dictEmus["heightEmus"])); paragraph1.Append(run); tc.Append(paragraph1); } else if (innerElement.Type == ContentBlock.Models.ContentBlocks.Enum.ContentBlockType.text) { TextContentBlock text = (TextContentBlock)innerElement; RunProperties rPr = new RunProperties(); foreach (var fs in text.Font) { switch (fs) { case Pattern.Bold: rPr.Bold = new Bold(); break; case Pattern.Italic: rPr.Italic = new Italic(); break; case Pattern.Underline: rPr.Underline = new Underline(); rPr.Underline.Val = UnderlineValues.Single; break; default: break; } } Run run = new Run(); run.RunProperties = rPr; run.Append(new Word.Text(text.Content)); tc.Append(new Paragraph(run)); } else { // If we omit the Paragraph or the Run from the following statement, the Word file gets corrupted. tc.Append(new Paragraph(new Run(new Word.Text(matrix.Body[i].ContentBlocks[j].ToString())))); } // Assume you want columns that are automatically sized. //tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto })); tr.Append(tc); } table.Append(tr); } // END of body. Document.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(table))); Document.Save(); }
private void AppendText(BasicContentBlock element, Paragraph paragraph) { TextContentBlock Text = (TextContentBlock)element; ParagraphProperties pPr = new ParagraphProperties(); RunProperties rPr = new RunProperties(); Ranges <int> Tokens = new Ranges <int>(); Italic = new PatternMatcher(ContentBlock.Models.ContentBlocks.Pattern.Italic); Italic.FindMatches(Text.Content, ref Tokens); Bold = new PatternMatcher(ContentBlock.Models.ContentBlocks.Pattern.Bold); Bold.FindMatches(Text.Content, ref Tokens); Underline = new PatternMatcher(ContentBlock.Models.ContentBlocks.Pattern.Underline); Underline.FindMatches(Text.Content, ref Tokens); BulletList = new PatternMatcher(Pattern.BulletList); BulletList.FindMatches(Text.Content, ref Tokens); if (!PatternsHaveMatches()) { run = new Run(); run.Append(new Text(Text.Content) { Space = SpaceProcessingModeValues.Preserve }); paragraph.Append(run); } else { int pos = 0; string buffer = ""; while (pos < Text.Content.Length) { if (!Tokens.ContainsValue(pos)) { buffer += Text.Content.Substring(pos, 1); } else if (buffer.Length > 0) { run = new Run(); Bold.SetFlagFor(pos - 1); Italic.SetFlagFor(pos - 1); Underline.SetFlagFor(pos - 1); BulletList.SetFlagFor(pos - 1); // this is the place where the code should be Refactored to catch the pattern and pass it properly if (Bold.Flag) { rPr.Append(new Bold() { Val = new OnOffValue(true) }); } if (Italic.Flag) { rPr.Append(new Italic()); } if (Underline.Flag) { rPr.Append(new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }); } if (BulletList.Flag) { rPr.Append(new NumberingFormat() { Val = NumberFormatValues.Bullet }); } run.Append(new Text(buffer) { Space = SpaceProcessingModeValues.Preserve }); //Need to do Some code changes to cath the text after Bold //run.Append(rPr); paragraph.Append(run); buffer = ""; } pos++; } ; } ////Assign the properties to the paragraph and the run. //Paragraph para = new Paragraph() //{ // ParagraphProperties = pPr //}; //Run runs = new Run(new Word.Text(Text.Content)) //{ // RunProperties = rPr //}; //paragraph.Append(runs); run.Append(rPr); Document.MainDocumentPart.Document.Body.Append(paragraph); Document.Save(); }
// The following bool variable indicates if a static page in the document is before or after the main body of the doc. /// <summary> /// Gets the JSON file provided and parses every object to C# object. /// Refactoring needed as it's pretty big for one method. /// </summary> /// <returns>A string that holds all the elements of the JSON file after parsing.</returns> /// public string ParseJsonToObj() { // Initializations writer = new WriteToFile(); // Gets the whole string of the file (root object). JObject jDocument = JObject.Parse(File.ReadAllText(JSONFilePath)); // Gets the attribute's values of the above object (document). JObject JTag = (JObject)jDocument["Tags"]; JValue jTitle = (JValue)JTag["title"]; JValue jVersion = (JValue)JTag["version"]; JValue jLanguage = (JValue)JTag["language"]; JValue jType = (JValue)JTag["type"]; JValue jHMI_Version = (JValue)JTag["HMI_Version"]; // Create the C# Document instance and pass the title, version and HMI version from the JDocument. Document document = new Document { Title = (string)jTitle, Version = (string)jVersion, HMI_Version = (string)jHMI_Version, }; if (Enum.TryParse <Language>(((string)jLanguage).ToLower(), out Language lan)) { document.Language = lan; } if (Enum.TryParse <DocumentType>(((string)jType).ToLower(), out DocumentType res)) { document.Type = res; } JArray jDocumentParrts = (JArray)jDocument["documentParts"]; foreach (JObject jChapterLists in jDocumentParrts) { JArray ChapterList = (JArray)jChapterLists["chapters"]; //Get the Chapter foreach (JObject JPart in ChapterList) { Chapter chapter = null; chapter = new Chapter((string)JPart.GetValue("title")); //gets title string Type = (string)JPart.GetValue("type"); //gets the type of ContentBlocks JArray chapterContentBlocks = (JArray)JPart.GetValue("contentBlocks"); //Getting the contebt Block ContentBlocksList currentBlock = new ContentBlocksList(); foreach (JObject jElement in chapterContentBlocks) // passes the each elements to content block { BasicContentBlock element = ContentBlock_Parser.ParseContentBlock(jElement); currentBlock.ContentBlocks.Add(element); } chapter.AddContents(currentBlock); document.ChaptersList.Add(chapter); // Need some codeWork to catch the chapters inside the chapter //JArray SubChapter = (JArray)JPart["chapters"]; //Get the Chapter //foreach (JObject jChapter in SubChapter)//gets all chapters from chapter list in json //{ // Chapter Sub_Chapter = null; // Sub_Chapter = new Chapter((string)jChapter.GetValue("title")); //gets title // string Types = (string)jChapter.GetValue("type");//gets the type of ContentBlocks // JArray Sub_chapterContentBlocks = (JArray)jChapter.GetValue("contentBlocks");//Getting the contebt Block // ContentBlocksList Sub_currentBlock = new ContentBlocksList(); // foreach (JObject jElement in Sub_chapterContentBlocks) // passes the each elements to content block // { // BasicContentBlock element = ContentBlock_Parser.ParseContentBlock(jElement); // Sub_currentBlock.ContentBlocks.Add(element); // Sub_Chapter.AddContents(Sub_currentBlock); // document.ChaptersList.Add(Sub_Chapter); // } //} } } // Call to WriteToFIle.cs for writing the elements to word. writer.ToFile(document); return("OK!"); }