/// <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(); }