public void GetBordersEnumerator() { //ExStart //ExFor:BorderCollection.GetEnumerator //ExSummary:Shows how to enumerate all borders in a collection. Document doc = new Document(MyDir + "Borders.docx"); DocumentBuilder builder = new DocumentBuilder(doc); BorderCollection borders = builder.ParagraphFormat.Borders; using (IEnumerator <Border> enumerator = borders.GetEnumerator()) { while (enumerator.MoveNext()) { // Do something useful. Border b = enumerator.Current; b.Color = Color.RoyalBlue; b.LineStyle = LineStyle.Double; } } doc.Save(ArtifactsDir + "BorderCollection.GetBordersEnumerator.docx"); //ExEnd doc = new Document(ArtifactsDir + "BorderCollection.GetBordersEnumerator.docx"); foreach (Border border in doc.FirstSection.Body.FirstParagraph.ParagraphFormat.Borders) { Assert.AreEqual(Color.RoyalBlue.ToArgb(), border.Color.ToArgb()); Assert.AreEqual(LineStyle.Double, border.LineStyle); } }
public static void ApplyBordersAndShadingToParagraph(string dataDir) { // ExStart:DocumentBuilderApplyBordersAndShadingToParagraph Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set paragraph borders BorderCollection borders = builder.ParagraphFormat.Borders; borders.DistanceFromText = 20; borders[BorderType.Left].LineStyle = LineStyle.Double; borders[BorderType.Right].LineStyle = LineStyle.Double; borders[BorderType.Top].LineStyle = LineStyle.Double; borders[BorderType.Bottom].LineStyle = LineStyle.Double; // Set paragraph shading Shading shading = builder.ParagraphFormat.Shading; shading.Texture = TextureIndex.TextureDiagonalCross; shading.BackgroundPatternColor = System.Drawing.Color.LightCoral; shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon; builder.Write("I'm a formatted paragraph with double border and nice shading."); dataDir = dataDir + "DocumentBuilderApplyBordersAndShadingToParagraph_out.doc"; doc.Save(dataDir); // ExEnd:DocumentBuilderApplyBordersAndShadingToParagraph Console.WriteLine("\nBorders and shading using DocumentBuilder applied successfully to paragraph.\nFile saved at " + dataDir); }
public void HorizontalBorders() { //ExStart //ExFor:BorderCollection.Horizontal //ExSummary:Shows how to apply settings to horizontal borders to a paragraph's format. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Create a red horizontal border for the paragraph. Any paragraphs created afterwards will inherit these border settings. BorderCollection borders = doc.FirstSection.Body.FirstParagraph.ParagraphFormat.Borders; borders.Horizontal.Color = Color.Red; borders.Horizontal.LineStyle = LineStyle.DashSmallGap; borders.Horizontal.LineWidth = 3; // Write text to the document without creating a new paragraph afterward. // Since there is no paragraph underneath, the horizontal border will not be visible. builder.Write("Paragraph above horizontal border."); // Once we add a second paragraph, the border of the first paragraph will become visible. builder.InsertParagraph(); builder.Write("Paragraph below horizontal border."); doc.Save(ArtifactsDir + "Border.HorizontalBorders.docx"); //ExEnd doc = new Document(ArtifactsDir + "Border.HorizontalBorders.docx"); ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs; Assert.AreEqual(LineStyle.DashSmallGap, paragraphs[0].ParagraphFormat.Borders[BorderType.Horizontal].LineStyle); Assert.AreEqual(LineStyle.DashSmallGap, paragraphs[1].ParagraphFormat.Borders[BorderType.Horizontal].LineStyle); }
/// <summary> /// Creates Stylesheet from collections of border styles, fontstyles, color styles etc... /// </summary> public static Stylesheet GetStyleSheet(ExcelType scheme) { var stylesheet = new Stylesheet(); var fontCollection = new FontCollection(); var fillsCollection = new FillsCollection(); var borderCollection = new BorderCollection(); var cellStyleFormats = new CellStyleFormats(); var cellStyle = new CellFormat(); cellStyle.NumberFormatId = 0; cellStyle.FontId = 0; cellStyle.FillId = 0; cellStyle.BorderId = 0; cellStyleFormats.Append(cellStyle); cellStyleFormats.Count = UInt32Value.FromUInt32((uint)cellStyleFormats.ChildElements.Count); var numberingFormats = new CellContentFormat(); var cellFormats = new DocumentFormat.OpenXml.Spreadsheet.CellFormats(); cellFormats.Append(GetStyleSheetScheme(scheme)); cellFormats.Count = UInt32Value.FromUInt32((uint)cellFormats.ChildElements.Count); stylesheet.Append(numberingFormats); stylesheet.Append(fontCollection); stylesheet.Append(fillsCollection); stylesheet.Append(borderCollection); stylesheet.Append(cellStyleFormats); stylesheet.Append(cellFormats); var css = new DocumentFormat.OpenXml.Spreadsheet.CellStyles(); var cs = new CellStyle(); cs.Name = StringValue.FromString("Normal"); cs.FormatId = 0; cs.BuiltinId = 0; css.Append(cs); css.Count = UInt32Value.FromUInt32((uint)css.ChildElements.Count); stylesheet.Append(css); var dfs = new DifferentialFormats(); dfs.Count = 0; stylesheet.Append(dfs); var tss = new TableStyles(); tss.Count = 0; tss.DefaultTableStyle = StringValue.FromString("TableStyleMedium9"); tss.DefaultPivotStyle = StringValue.FromString("PivotStyleLight16"); stylesheet.Append(tss); return stylesheet; }
public void RemoveAllBorders() { //ExStart //ExFor:BorderCollection.ClearFormatting //ExSummary:Shows how to remove all borders from a paragraph at once. Document doc = new Document(MyDir + "Borders.docx"); DocumentBuilder builder = new DocumentBuilder(doc); BorderCollection borders = builder.ParagraphFormat.Borders; borders.ClearFormatting(); doc.Save(ArtifactsDir + "BorderCollection.RemoveAllBorders.docx"); //ExEnd doc = new Document(ArtifactsDir + "BorderCollection.RemoveAllBorders.docx"); foreach (Border border in doc.FirstSection.Body.FirstParagraph.ParagraphFormat.Borders) { Assert.AreEqual(Color.Empty.ToArgb(), border.Color.ToArgb()); Assert.AreEqual(LineStyle.None, border.LineStyle); } }
public void SharedElements() { //ExStart //ExFor:Border.Equals(Object) //ExFor:Border.Equals(Border) //ExFor:Border.GetHashCode //ExFor:BorderCollection.Count //ExFor:BorderCollection.Equals(BorderCollection) //ExFor:BorderCollection.Item(Int32) //ExSummary:Shows how border collections can share elements. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Writeln("Paragraph 1."); builder.Write("Paragraph 2."); // Since both paragraphs were created with the same border configuration, // the border collections of the two paragraphs share the same elements. BorderCollection firstParagraphBorders = doc.FirstSection.Body.FirstParagraph.ParagraphFormat.Borders; BorderCollection secondParagraphBorders = builder.CurrentParagraph.ParagraphFormat.Borders; Assert.AreEqual(6, firstParagraphBorders.Count); //ExSkip for (int i = 0; i < firstParagraphBorders.Count; i++) { Assert.IsTrue(firstParagraphBorders[i].Equals(secondParagraphBorders[i])); Assert.AreEqual(firstParagraphBorders[i].GetHashCode(), secondParagraphBorders[i].GetHashCode()); Assert.False(firstParagraphBorders[i].IsVisible); } foreach (Border border in secondParagraphBorders) { border.LineStyle = LineStyle.DotDash; } // After changing the line style of the borders in just the second paragraph, // the border collections no longer share the same elements. for (int i = 0; i < firstParagraphBorders.Count; i++) { Assert.IsFalse(firstParagraphBorders[i].Equals(secondParagraphBorders[i])); Assert.AreNotEqual(firstParagraphBorders[i].GetHashCode(), secondParagraphBorders[i].GetHashCode()); // Changing the appearance of an empty border makes it visible. Assert.True(secondParagraphBorders[i].IsVisible); } doc.Save(ArtifactsDir + "Border.SharedElements.docx"); //ExEnd doc = new Document(ArtifactsDir + "Border.SharedElements.docx"); ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs; foreach (Border testBorder in paragraphs[0].ParagraphFormat.Borders) { Assert.AreEqual(LineStyle.None, testBorder.LineStyle); } foreach (Border testBorder in paragraphs[1].ParagraphFormat.Borders) { Assert.AreEqual(LineStyle.DotDash, testBorder.LineStyle); } }
public void BuildBookingTable(DocumentBuilder builder) { //Reset to default font and paragraph formatting. builder.Font.ClearFormatting(); builder.ParagraphFormat.ClearFormatting(); builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; builder.InsertParagraph(); builder.Writeln(confirmationLabel.Text); builder.Font.Color = System.Drawing.Color.Black; builder.Font.Size = 10; builder.Font.Name = "Tahoma"; builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; BorderCollection borders = builder.CellFormat.Borders; borders.LineStyle = LineStyle.Single; borders.Color = System.Drawing.Color.Black; builder.StartTable(); builder.Font.Bold = true; builder.InsertCell(); builder.Write(GetMessage("flightNumber")); builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray; builder.InsertCell(); builder.Write(GetMessage("departureDate")); builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray; builder.InsertCell(); builder.Write(GetMessage("departureAirport")); builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray; builder.InsertCell(); builder.Write(GetMessage("destinationAirport")); builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray; builder.InsertCell(); builder.Write(GetMessage("aircraft")); builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.LightGray; builder.EndRow(); builder.CellFormat.ClearFormatting(); builder.Font.ClearFormatting(); foreach (Flight flight in confirmation.Reservation.Itinerary.Flights) { builder.InsertCell(); builder.Write(flight.FlightNumber); builder.InsertCell(); builder.Write(flight.DepartureTime.ToString()); builder.InsertCell(); builder.Write(flight.DepartureAirport.Description); builder.InsertCell(); builder.Write(flight.DestinationAirport.Description); builder.InsertCell(); builder.Write(flight.Aircraft.Model); builder.EndRow(); } builder.EndTable(); }