|
| 1 | +# Word Documents Manipulation In C# |
| 2 | +## Word Paragraphs |
| 3 | +```csharp |
| 4 | +var doc = new Openize.Words.Document(); |
| 5 | +var body = new Openize.Words.Body(doc); |
| 6 | +var paragraph = new Openize.Words.IElements.Paragraph(); |
| 7 | +paragraph.AddRun(new Openize.Words.IElements.Run { |
| 8 | + Text = "Word Document Created by Openize.OpenXML-SDK", |
| 9 | + FontFamily="Arial", |
| 10 | + FontSize = 15, |
| 11 | + Bold = true |
| 12 | + }); |
| 13 | +body.AppendChild(paragraph); |
| 14 | +doc.Save("WordOpenize.docx"); |
| 15 | +``` |
| 16 | +## Word Tables |
| 17 | +```csharp |
| 18 | +// Initialize a new word document with the default template |
| 19 | +var doc = new Openize.Words.Document(); |
| 20 | +System.Console.WriteLine("Word Document with default template initialized"); |
| 21 | + |
| 22 | +// Initialize the body with the new document |
| 23 | +var body = new Openize.Words.Body(doc); |
| 24 | +System.Console.WriteLine("Body of the Word Document initialized"); |
| 25 | + |
| 26 | +// Get all table styles |
| 27 | +var tableStyles = doc.GetElementStyles().TableStyles; |
| 28 | +System.Console.WriteLine("Table styles loaded"); |
| 29 | + |
| 30 | +// Create Headings Paragraph and append to the body. |
| 31 | +foreach (var tableStyle in tableStyles) |
| 32 | +{ |
| 33 | + var table = new Openize.Words.IElements.Table(5,3); |
| 34 | + table.Style = tableStyle; |
| 35 | + |
| 36 | + table.Column.Width = 2500; |
| 37 | + |
| 38 | + var rowNumber = 0; |
| 39 | + var columnNumber = 0; |
| 40 | + |
| 41 | + var para = new Openize.Words.IElements.Paragraph(); |
| 42 | + para.Style = Openize.Words.IElements.Headings.Heading1; |
| 43 | + para.AddRun(new Openize.Words.IElements.Run { |
| 44 | + Text = $"Table With Style '{tableStyle}' : " |
| 45 | + }); |
| 46 | + |
| 47 | + body.AppendChild(para); |
| 48 | + |
| 49 | + foreach (var row in table.Rows) |
| 50 | + { |
| 51 | + rowNumber++; |
| 52 | + foreach(var cell in row.Cells) |
| 53 | + { |
| 54 | + columnNumber++; |
| 55 | + para = new Openize.Words.IElements.Paragraph(); |
| 56 | + para.AddRun(new Openize.Words.IElements.Run { |
| 57 | + Text = $"Row {rowNumber} Column {columnNumber}" |
| 58 | + }); |
| 59 | + cell.Paragraphs.Add(para); |
| 60 | + } |
| 61 | + columnNumber = 0; |
| 62 | + } |
| 63 | + body.AppendChild(table); |
| 64 | + System.Console.WriteLine($"Table with style {tableStyle} created and appended"); |
| 65 | +} |
| 66 | + |
| 67 | +// Save the newly created Word Document. |
| 68 | +doc.Save($"WordTables.docx"); |
| 69 | +``` |
| 70 | +## Word Images |
| 71 | +```csharp |
| 72 | +using (System.Net.WebClient webClient = new System.Net.WebClient()) |
| 73 | +{ |
| 74 | + webClient.DownloadFile("https://i.imgur.com/V8aRisV.jpg", |
| 75 | + $"Images/image1.jpg"); |
| 76 | + System.Console.WriteLine($"First image downloaded..."); |
| 77 | + webClient.DownloadFile("https://i.imgur.com/xrbdI7n.png", |
| 78 | + $"Images/image2.png"); |
| 79 | + System.Console.WriteLine($"Second image downloaded..."); |
| 80 | + webClient.DownloadFile("https://i.imgur.com/bqzDqUZ.png", |
| 81 | + $"Images/image3.png"); |
| 82 | + System.Console.WriteLine($"Third image downloaded..."); |
| 83 | +} |
| 84 | +// Initialize a new word document with the default template |
| 85 | +var doc = new Openize.Words.Document(); |
| 86 | + |
| 87 | +// Initialize the body with the new document |
| 88 | +var body = new Openize.Words.Body(doc); |
| 89 | + |
| 90 | +// Load images from the specified directory |
| 91 | +var imageFiles = System.IO.Directory.GetFiles("Images"); |
| 92 | +foreach (var imageFile in imageFiles) |
| 93 | +{ |
| 94 | + // Decode the image with SkiaSharp |
| 95 | + using (var skBMP = SkiaSharp.SKBitmap.Decode(imageFile)) |
| 96 | + { |
| 97 | + using (var skIMG = SkiaSharp.SKImage.FromBitmap(skBMP)) |
| 98 | + { |
| 99 | + var encoded = skIMG.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100); |
| 100 | + // Initialize the word document image element |
| 101 | + var img = new Openize.Words.IElements.Image(); |
| 102 | + // Load data for the word document image element |
| 103 | + img.ImageData = encoded.ToArray(); |
| 104 | + img.Height = 350; |
| 105 | + img.Width = 300; |
| 106 | + // Append image element to the word document |
| 107 | + body.AppendChild(img); |
| 108 | + System.Console.WriteLine($"Image {System.IO.Path.GetFullPath(imageFile)} " + |
| 109 | + $"added to the word document."); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +// Save the newly created Word Document. |
| 114 | +doc.Save($"WordImages.docx"); |
| 115 | +``` |
| 116 | +## Word Shapes |
| 117 | +```csharp |
| 118 | +// Initialize a new word document with the default template |
| 119 | +var doc = new Openize.Words.Document(); |
| 120 | +System.Console.WriteLine("Word Document with default template initialized"); |
| 121 | + |
| 122 | +// Initialize the body with the new document |
| 123 | +var body = new Openize.Words.Body(doc); |
| 124 | +System.Console.WriteLine("Body of the Word Document initialized"); |
| 125 | + |
| 126 | +// Instantiate shape element with hexagone and coordinates/size. |
| 127 | +var shape = new Openize.Words.IElements.Shape(100, 100, 400, 400, |
| 128 | + Openize.Words.IElements.ShapeType.Hexagone); |
| 129 | +// Add hexagone shape to the word document. |
| 130 | +body.AppendChild(shape); |
| 131 | +System.Console.WriteLine("Hexagone shape added"); |
| 132 | + |
| 133 | +// Save the newly created Word Document. |
| 134 | +doc.Save($"WordShape.docx"); |
| 135 | +``` |
| 136 | +## Word Groupshapes |
| 137 | +```csharp |
| 138 | +var doc = new Openize.Words.Document(); |
| 139 | +var body = new Openize.Words.Body(doc); |
| 140 | + |
| 141 | +var diamond = new Openize.Words.IElements.Shape(0, 0, 200, 200, |
| 142 | + Openize.Words.IElements.ShapeType.Diamond, |
| 143 | + Openize.Words.IElements.ShapeFillType.Gradient, |
| 144 | + new Openize.Words.IElements.ShapeFillColors()); |
| 145 | + |
| 146 | +var oval = new Openize.Words.IElements.Shape(300, 0, 200, 200, |
| 147 | + Openize.Words.IElements.ShapeType.Ellipse, |
| 148 | + Openize.Words.IElements.ShapeFillType.Pattern, |
| 149 | + new Openize.Words.IElements.ShapeFillColors()); |
| 150 | + |
| 151 | +var groupShape = new Openize.Words.IElements.GroupShape(diamond, oval); |
| 152 | +body.AppendChild(groupShape); |
| 153 | +doc.Save("WordGroupShape.docx"); |
| 154 | +``` |
0 commit comments