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