|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace DocIOBlazor.Data |
| 5 | +{ |
| 6 | + public class WordService |
| 7 | + { |
| 8 | + private readonly IWebHostEnvironment _hostingEnvironment; |
| 9 | + public WordService(IWebHostEnvironment hostingEnvironment) |
| 10 | + { |
| 11 | + _hostingEnvironment = hostingEnvironment; |
| 12 | + } |
| 13 | + public MemoryStream CreateWord() |
| 14 | + { |
| 15 | + WordDocument document = new WordDocument(); |
| 16 | + WSection section = document.AddSection() as WSection; |
| 17 | + section.PageSetup.Margins.All = 72; |
| 18 | + section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792); |
| 19 | + |
| 20 | + WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle; |
| 21 | + style.CharacterFormat.FontName = "Calibri"; |
| 22 | + style.CharacterFormat.FontSize = 11f; |
| 23 | + style.ParagraphFormat.BeforeSpacing = 0; |
| 24 | + style.ParagraphFormat.AfterSpacing = 8; |
| 25 | + style.ParagraphFormat.LineSpacing = 13.8f; |
| 26 | + |
| 27 | + |
| 28 | + style = document.AddParagraphStyle("Heading 1") as WParagraphStyle; |
| 29 | + style.ApplyBaseStyle("Normal"); |
| 30 | + style.CharacterFormat.FontName = "Calibri Light"; |
| 31 | + style.CharacterFormat.FontSize = 16f; |
| 32 | + style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181); |
| 33 | + style.ParagraphFormat.BeforeSpacing = 12; |
| 34 | + style.ParagraphFormat.AfterSpacing = 0; |
| 35 | + |
| 36 | + IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph(); |
| 37 | + paragraph.ApplyStyle("Normal"); |
| 38 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; |
| 39 | + |
| 40 | + string basePath = _hostingEnvironment.WebRootPath + @"/images/word/"; |
| 41 | + FileStream imageStream = new FileStream(basePath + @"adventurecycle.jpg", FileMode.Open, FileAccess.Read); |
| 42 | + WPicture picture = paragraph.AppendPicture(imageStream) as WPicture; |
| 43 | + picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText; |
| 44 | + picture.VerticalOrigin = VerticalOrigin.Margin; |
| 45 | + picture.VerticalPosition = -45; |
| 46 | + picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 47 | + picture.HorizontalPosition = 263.5f; |
| 48 | + picture.WidthScale = 20; |
| 49 | + picture.HeightScale = 15; |
| 50 | + |
| 51 | + WTextRange textRange = paragraph.AppendText("Adventure Work cycles") as WTextRange; |
| 52 | + textRange.CharacterFormat.FontName = "Calibri"; |
| 53 | + textRange.CharacterFormat.FontSize = 12f; |
| 54 | + textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red; |
| 55 | + |
| 56 | + paragraph = section.AddParagraph(); |
| 57 | + paragraph.ApplyStyle("Heading 1"); |
| 58 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 59 | + textRange = paragraph.AppendText("Adventure Work cycles") as WTextRange; |
| 60 | + textRange.CharacterFormat.FontName = "Calibri"; |
| 61 | + textRange.CharacterFormat.FontSize = 18f; |
| 62 | + |
| 63 | + paragraph = section.AddParagraph(); |
| 64 | + paragraph.ParagraphFormat.FirstLineIndent = 36; |
| 65 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 66 | + textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange; |
| 67 | + textRange.CharacterFormat.FontSize = 12f; |
| 68 | + |
| 69 | + paragraph = section.AddParagraph(); |
| 70 | + paragraph.ApplyStyle("Heading 1"); |
| 71 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; |
| 72 | + textRange = paragraph.AppendText("Product Overview") as WTextRange; |
| 73 | + textRange.CharacterFormat.FontName = "Calibri"; |
| 74 | + textRange.CharacterFormat.FontSize = 18f; |
| 75 | + |
| 76 | + IWTable table = section.AddTable(); |
| 77 | + table.ResetCells(2, 2); |
| 78 | + table.TableFormat.Borders.BorderType = BorderStyle.None; |
| 79 | + table.TableFormat.IsAutoResized = true; |
| 80 | + |
| 81 | + paragraph = table[0, 0].AddParagraph(); |
| 82 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 83 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 84 | + |
| 85 | + imageStream = new FileStream(basePath + @"mountain-200.jpg", FileMode.Open, FileAccess.Read); |
| 86 | + picture = paragraph.AppendPicture(imageStream) as WPicture; |
| 87 | + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; |
| 88 | + picture.VerticalOrigin = VerticalOrigin.Paragraph; |
| 89 | + picture.VerticalPosition = 4.5f; |
| 90 | + picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 91 | + picture.HorizontalPosition = -2.15f; |
| 92 | + picture.WidthScale = 79; |
| 93 | + picture.HeightScale = 79; |
| 94 | + |
| 95 | + paragraph = table[0, 1].AddParagraph(); |
| 96 | + paragraph.ApplyStyle("Heading 1"); |
| 97 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 98 | + paragraph.ParagraphFormat.LineSpacing = 12f; |
| 99 | + paragraph.AppendText("Mountain-200"); |
| 100 | + |
| 101 | + paragraph = table[0, 1].AddParagraph(); |
| 102 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 103 | + paragraph.ParagraphFormat.LineSpacing = 12f; |
| 104 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 105 | + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; |
| 106 | + textRange = paragraph.AppendText("Product No: BK-M68B-38\rSize: 38\rWeight: 25\rPrice: $2,294.99\r") as WTextRange; |
| 107 | + textRange.CharacterFormat.FontSize = 12f; |
| 108 | + textRange.CharacterFormat.FontName = "Times New Roman"; |
| 109 | + paragraph = table[0, 1].AddParagraph(); |
| 110 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 111 | + paragraph.ParagraphFormat.LineSpacing = 12f; |
| 112 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 113 | + |
| 114 | + paragraph = table[1, 0].AddParagraph(); |
| 115 | + paragraph.ApplyStyle("Heading 1"); |
| 116 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 117 | + paragraph.ParagraphFormat.LineSpacing = 12f; |
| 118 | + paragraph.AppendText("Mountain-300"); |
| 119 | + |
| 120 | + paragraph = table[1,0].AddParagraph(); |
| 121 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 122 | + paragraph.ParagraphFormat.LineSpacing = 12f; |
| 123 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 124 | + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; |
| 125 | + textRange = paragraph.AppendText("Product No: BK-M47B-38\rSize: 35\rWeight: 22\rPrice: $1,079.99\r") as WTextRange; |
| 126 | + textRange.CharacterFormat.FontSize = 12f; |
| 127 | + textRange.CharacterFormat.FontName = "Times New Roman"; |
| 128 | + paragraph = table[1,0].AddParagraph(); |
| 129 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 130 | + paragraph.ParagraphFormat.LineSpacing = 12f; |
| 131 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 132 | + |
| 133 | + paragraph = table[1,1].AddParagraph(); |
| 134 | + paragraph.ParagraphFormat.AfterSpacing = 0; |
| 135 | + paragraph.BreakCharacterFormat.FontSize = 12f; |
| 136 | + |
| 137 | + imageStream = new FileStream(basePath + @"mountain-300.jpg", FileMode.Open, FileAccess.Read); |
| 138 | + picture = paragraph.AppendPicture(imageStream) as WPicture; |
| 139 | + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; |
| 140 | + picture.VerticalOrigin = VerticalOrigin.Paragraph; |
| 141 | + picture.VerticalPosition = 8.2f; |
| 142 | + picture.HorizontalOrigin = HorizontalOrigin.Column; |
| 143 | + picture.HorizontalPosition = -14.95f; |
| 144 | + picture.WidthScale = 75; |
| 145 | + picture.HeightScale = 75; |
| 146 | + |
| 147 | + MemoryStream stream = new MemoryStream(); |
| 148 | + document.Save(stream, FormatType.Docx); |
| 149 | + document.Close(); |
| 150 | + stream.Position = 0; |
| 151 | + |
| 152 | + return stream; |
| 153 | + |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments