From d8e0263ec70a7a5e0d5562887ed1ed87f693a730 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Tue, 17 Nov 2020 17:00:13 -0400 Subject: [PATCH] #215 support filling rectangles on pdf builder --- .../Writer/PdfDocumentBuilderTests.cs | 15 +++++++++++++++ src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs | 13 +++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs index 8dc9cb89..adcc1112 100644 --- a/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs +++ b/src/UglyToad.PdfPig.Tests/Writer/PdfDocumentBuilderTests.cs @@ -588,6 +588,21 @@ } } + [Fact] + public void CanCreateDocumentWithFilledRectangle() + { + var builder = new PdfDocumentBuilder(); + var page = builder.AddPage(PageSize.A4); + + page.SetTextAndFillColor(255, 0, 0); + page.SetStrokeColor(0, 0, 255); + + page.DrawRectangle(new PdfPoint(20, 100), 200, 100, 1.5m, true); + + var file = builder.Build(); + WriteFile(nameof(CanCreateDocumentWithFilledRectangle), file); + } + private static void WriteFile(string name, byte[] bytes, string extension = "pdf") { try diff --git a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs index 669ac2b2..d1680c39 100644 --- a/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs +++ b/src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs @@ -91,7 +91,8 @@ /// The width of the rectangle. /// The height of the rectangle. /// The width of the line border of the rectangle. - public void DrawRectangle(PdfPoint position, decimal width, decimal height, decimal lineWidth = 1) + /// Whether to fill with the color set by . + public void DrawRectangle(PdfPoint position, decimal width, decimal height, decimal lineWidth = 1, bool fill = false) { if (lineWidth != 1) { @@ -99,7 +100,15 @@ } operations.Add(new AppendRectangle((decimal)position.X, (decimal)position.Y, width, height)); - operations.Add(StrokePath.Value); + + if (fill) + { + operations.Add(FillPathEvenOddRuleAndStroke.Value); + } + else + { + operations.Add(StrokePath.Value); + } if (lineWidth != 1) {